言語処理100本ノック 2020「09. Typoglycemia」
問題文
問題の概要
少し処理が煩雑ですが、大体は今まで扱った記法が利用できます。
ランダムに並び替える部分は「random.shuffle()」や「random.sample()」が選択肢となります*1。
リストを文字列として半角スペース区切りで結合するために「join」を利用しました。
import random def shuffle_word(word): if len(word) <= 4: return word else: start = word[0] end = word[-1] others = random.sample(list(word[1:-1]), len(word[1:-1])) return ''.join([start] + others + [end]) text = 'I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind .' ans = [shuffle_word(w) for w in text.split()] print(' '.join(ans))