Zundokokiyoshi (generator) in Python

Overview

It seems to be popular, so I tried it with Python as the Nth brew.

Code (version 1)

# -*- coding: utf-8 -*-
from collections import deque
import random


z = u'Dung'
d = u'Doco'
zzzzd = (z, z, z, z, d, )
kys = u'Ki yo shi!'


#Infinite Zundco Generator
def zd():
    while True:
        w = random.choice((z, d, ))
        print w
        yield w


def zndkkys():
    q = deque()
    #Skip to the 4th character that never matches
    for i, w in enumerate(zd()):
        q.append(w)
        if i == 3:
            break

    #Check each time after the 5th character and discard unnecessary characters
    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()

Feature

However

You can go with this generator alone, right? That is version 2 below

Code (version 2)

# -*- coding: utf-8 -*-
from collections import deque
import random


z = u'Dung'
d = u'Doco'
kys = u'Ki yo shi!'

def zndkkys2():
    state = 0
    while True:
        num = random.choice((0, 1, ))
        #Count up for Zun
        if num:
            yield z
            state += 1
        else:
            yield d
            #If there are four or more consecutive dungs in front of the doko, it's ki yo shi!
            if state >= 4:
                yield kys
                return
            #Initialize the count if the dung in front of the doco is 3 times or less
            else:
                state = 0

if __name__ == '__main__':
    print ''.join(zndkkys2())

Overview

Summary

Recommended Posts

Zundokokiyoshi (generator) in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Discord in Python
[Python] Generator function
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
Sqlite in python
StepAIC in Python
Zundokokiyoshi with python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Python beginner Zundokokiyoshi
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python