1. Qiita
  2. 投稿
  3. python3

kiyoshi.py

  • 0
    いいね
  • 2
    コメント

    はやりのアレ

    あんまり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')
    
    Comments Loading...