[PYTHON] Study at Zundoko

I was messed up

zun.py


import random
def r(a, b):
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
    r(4, 0)
except:
    print('Ki yo shi!')

This will give you the desired result, but what if you set except to finally?

zun_2.py


import random
def r(a, b):
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
    r(4, 0)
finally:
    print('Ki yo shi!')

Message when finally


Dung
Doco
Dung
Dung
Dung
Dung
Doco
Ki yo shi!
Traceback (most recent call last):
  File "zun_2.py", line 5, in <module>
    r(4, 0)
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 3, in r
    r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
ZeroDivisionError: division by zero

This is a bit silly. .. .. It doesn't change even if I do it with lambda.

zun_2.py


import random
r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
    r(4, 0)
finally:
    print('Ki yo shi!')

Part of the result


Traceback (most recent call last):
  File "zun_2.py", line 4, in <module>
    r(4, 0)
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
  File "zun_2.py", line 2, in <lambda>
    r = lambda a,b: r(a-1, print('Dung')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
ZeroDivisionError: division by zero

Hmmm. .. .. Speaking of which, I wondered if I could recurse anonymous functions. http://fj.hatenablog.jp/entry/2014/06/15/032135 I saw a story like this, but it seems that the performance is not good.

Various clean versions:

zun_3.py


import random
r=lambda a: print('Dung') or r(a-1) if random.random() < 0.5 else print('Doco') or a <= 0 or r(4)
r(4) or print('Ki yo shi!')

This is an excellent thing that if <= is changed to ==, it will pass through except when there are just four dunes. There should be a debate as to whether it is okay to admit five or more dunes.

Recommended Posts

Study at Zundoko
At Coder (2020/09/08)
Matters examined at the August 2017 study session
Matters examined at the study session in February
Matters examined at the study session in May