0
0

NLP100本ノックのtypoglycemiaをできるだけ短く書いてみた。

Posted at

はじめに

最近言語処理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の引数に入れてみました。
非常に見にくいですね。
みなさんちゃんと関数化しましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0