-
Notifications
You must be signed in to change notification settings - Fork 0
/
1287.py
78 lines (65 loc) · 1.63 KB
/
1287.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
input = lambda: sys.stdin.readline().strip()
U = lambda: map(int, input().split())
def printflush(x): print(x); sys.stdout.flush()
#from math import *
#from itertools import *
#from random import *
#from functools import *
isnum = lambda x: 47 < ord(x) < 58
binop = ['*', '/', '+', '-']
sapling = lambda x: [None, x, None, None]
def f(tree):
try:
val, prc, lft, rgt = tree
if val in binop:
if val == '/':
val = "//"
return eval(f"{f(lft)}{val}{f(rgt)}")
return int(val)
except(ZeroDivisionError, TypeError):
print("ROCK")
exit()
l = list(input())
prc = 2
tree = sapling(2)
while l:
c = l[0]
target = tree
if isnum(c):
while len(l) > 1 and isnum(l[1]):
c += l[1]
del l[0]
while isinstance(target[3], list):
target = target[3]
if target[0] == None:
target[0] = c
else:
print("ROCK")
exit()
elif c == '(':
if len(l) > 1 and l[1] == ')':
print("ROCK")
exit()
while isinstance(target[3], list):
target = target[3]
prc += 3
target[1] = prc
elif c == ')':
prc -= 3
if prc < 0:
print("ROCK")
exit()
elif c in binop:
k = prc - binop.index(c)//2 - 1
def grow(t):
if k <= t[1]:
return [c, k, t, sapling(prc)]
else:
return t[:3] + [grow(t[3])]
tree = grow(tree)
del l[0]
if prc != 2:
print("ROCK")
else:
print(f(tree))