LoginSignup
0
0

More than 5 years have passed since last update.

kiyoshi.py

Last updated at Posted at 2016-03-15

はやりのアレ

あんまりpythonで書いてる人がいなかったのでとりあえず。


# -*- coding: utf-8 -*-
from doctest import testmod
from random import getrandbits
RULE = ['zun','zun','zun','zun','doko',]


def execute():
    '''
    >>> r = execute()
    >>> r[-1]
    'kiyoshi'
    >>> r[-2]
    ['zun', 'zun', 'zun', 'zun', 'doko']
    '''
    result = []
    n = None
    while n != RULE:
        n = ['zun' if int(x)==0 else 'doko' for x in list(str('{0:05b}'.format(getrandbits(5))))]
        result.append(n)
    else:
        result.append('kiyoshi')
    return result


if __name__ == "__main__":
    testmod()
    for o in execute():
        print(o)


追記

問題を読み違えていたので書き直し

# -*- coding: utf-8 -*-
from doctest import testmod
from random import getrandbits
RULE = [True,True,True,True,False]


def execute(rule):
    '''
    >>> r = execute([True,True,True,True,False])
    >>> r[-5:]
    [True, True, True, True, False]
    '''
    res = []
    is_loop = True;
    while is_loop:
        n = [x == '1' for x in list(str('{0:0100b}'.format(getrandbits(100))))]
        for i in range(100):
            if not n[i:i+5] == rule:
                continue
            is_loop = False
            res.extend(n[:i+5])
            break
        else:
            if is_loop:
                res.extend(n)
    return res


if __name__ == "__main__":
    testmod()
    for o in execute(RULE):
        print(("zun" if o else "doko"))
    else:
        print('kiyoshi')
0
0
2

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