LoginSignup
6
6

More than 5 years have passed since last update.

Pythonでモルダー案件

Last updated at Posted at 2016-03-30

まえがき

Ruby でモルダー、あなた疲れてるのよを出力する方法
Python でも「モルダー、あなた疲れてるのよ」を出力したり、出力しなかったりする機能を求められることがあると思ったので書きました。

コード

# -*- coding: utf-8
import random
from more_itertools import chunked
from math import ceil

def mulder():
    s = u"モルダー あなた 疲れてる のよ".split()
    s2 = sum([darui(x) for x in chunked(random.sample(s, len(s)), 2)], [])
    s2.insert(2, u"、")
    return "".join(s2)

def darui(l):
    a1, a2, b1, b2 = sum([divide_rough(x) for x in l], [])
    return [a1, a2, b1, b2] if random.randint(0, 1) else [b1, a2, a1, b2]

def divide_rough(l):
    " 真ん中らへんで分割"
    return _join(chunked(l, int(ceil(len(l)/2.0))))

def _join(l):
    return map(lambda x:"".join(x), l)

i = 0
s = None
while s != u"モルダー、あなた疲れてるのよ":
    s = mulder()
    print(u"{0}: {1}".format(i, s))
    i += 1

実行例

0: 疲れてる、のよあなダーモルた
1: モルダー、疲れてるあなたのよ
2: のよ、疲れてるモルダーあなた
3: 疲れてる、のよモルダーあなた
4: モルてる、疲れダーあなたのよ
5: あなよ、のた疲れてるモルダー
6: あなてる、疲れたモルよのダー
7: モルた、あなダーのよ疲れてる
8: のてる、疲れよモルたあなダー
9: あなてる、疲れたのダーモルよ
10: 疲れてる、のよあなたモルダー
11: 疲れてる、モルダーあなよのた
12: あなた、モルダー疲れよのてる
13: あなた、モルダー疲れてるのよ
14: モルダー、疲れてるのよあなた
15: モルダー、あなた疲れよのてる
16: 疲れてる、あなたモルダーのよ
17: 疲れた、あなてるモルよのダー
18: のた、あなよ疲れダーモルてる
19: のた、あなよ疲れてるモルダー
20: のよ、モルダー疲れてるあなた
21: あなた、のよモルダー疲れてる
22: 疲れダー、モルてるのよあなた
23: あなよ、のたモルてる疲れダー
24: あなてる、疲れたモルよのダー
25: のよ、疲れてるモルダーあなた
26: 疲れてる、あなたのダーモルよ
27: 疲れてる、のよあなダーモルた
28: あなよ、のた疲れてるモルダー
29: モルダー、あなた疲れてるのよ
  • more-itertoolsは標準ライブラリではない
  • sum(l, [])はflattenの代用(2次ネストのみ)
  • もっと綺麗に書けると思うけどだるくなってしまった
6
6
1

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
6
6