はじめに
最近言語処理100本ノックとか競プロとかやっている中で、typoglycemiaコードを短く書いてみたくなったので書いてみました。
この記事は完全に自己満足なので、ご容赦ください。
コードはこちら
q09.py
import random
s = 'I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind .'
print(' '.join(map(lambda word: word[0] + ''.join(random.sample(word[1:-1], len(word[1:-1]))) + word[-1] if len(word) > 4 else word, s.split())))
書いてあることはシンプルですが、、、、見た目がきついですね・・・。
さらに短くしてみます。
q09-2.py
import random
print(' '.join(map(lambda word: word[0] + ''.join(random.sample(word[1:-1], len(word[1:-1]))) + word[-1] if len(word) > 4 else word, 'I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind .'.split())))
文章を変数に切り出さすに直接mapの引数に入れてみました。
非常に見にくいですね。
みなさんちゃんと関数化しましょう。