09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに 並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする. 適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .") を与え,その実行結果を確認せよ.
import random
def typoglycemia(sentence):
random.seed()
sentence = sentence.split()
for index in range(len(sentence)):
if len(sentence[index]) > 4:
result = []
sp = [s for s in sentence[index]][1:-1]
random.shuffle(sp)
result = list(sentence[index][0]) + sp + list(sentence[index][-1])
sentence[index] = "".join(result)
return sentence
str = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
print(" ".join(typoglycemia(str)))
<結果>
I cnu'ldot bevleie that I cuold aucllaty usnratnedd what I was rnaedig : the poeenhanml power of the haumn mind .