Pythonでズンドコキヨシ(ジェネレータ) ref: http://qiita.com/ques0942/items/b3762287d8962d489bbf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from collections import deque | |
import random | |
z = u'ズン' | |
d = u'ドコ' | |
zzzzd = (z, z, z, z, d, ) | |
kys = u'キ・ヨ・シ!' | |
# 無限ズンドコジェネレータ | |
def zd(): | |
while True: | |
w = random.choice((z, d, )) | |
print w | |
yield w | |
def zndkkys(): | |
q = deque() | |
# 絶対に一致しない4文字目まで読み飛ばす | |
for i, w in enumerate(zd()): | |
q.append(w) | |
if i == 3: | |
break | |
# 5文字目以降は都度チェックしつつ不要な文字は捨てる | |
for w in zd(): | |
q.append(w) | |
if all((zzzzd[i] == p for i, p in enumerate(q))): | |
print kys | |
return | |
q.popleft() | |
if __name__ == '__main__': | |
zndkkys() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from collections import deque | |
import random | |
z = u'ズン' | |
d = u'ドコ' | |
kys = u'キ・ヨ・シ!' | |
def zndkkys2(): | |
state = 0 | |
while True: | |
num = random.choice((0, 1, )) | |
# ズンならカウントアップ | |
if num: | |
yield z | |
state += 1 | |
else: | |
yield d | |
# ドコの前に4回以上ズンが連続していたらキ・ヨ・シ! | |
if state >= 4: | |
yield kys | |
return | |
# ドコの前のズンが3回以下ならカウントを初期化 | |
else: | |
state = 0 | |
if __name__ == '__main__': | |
print ''.join(zndkkys2()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment