-
Notifications
You must be signed in to change notification settings - Fork 0
/
1d-spreadsheet.py
75 lines (57 loc) · 1.86 KB
/
1d-spreadsheet.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
# https://www.codingame.com/training/easy/1d-spreadsheet
import sys
import math
import functools
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
line=0
class Spreadsheet:
sheet = []
@classmethod
def add(cls, cell):
cls.sheet.append(cell)
@classmethod
def lookup(cls, cell):
return cls.sheet[cell].value()
@classmethod
def get_sheet(cls):
return cls.sheet
class Cell:
def __init__(self, op, value1=None, value2=None):
self.op = op
self.value1 = value1
self.value2 = value2
#global line
#line += 1
#print("Debug messages...", line, self.op, self.value1, self.value2, file=sys.stderr, flush=True)
''' Used to memoize / speedup values already calculated '''
@functools.cache
def value(self):
if self.op == "VALUE":
if '$' in str(self.value1):
val1 = Spreadsheet.lookup(int(str(self.value1).replace('$', '')))
return int(val1)
else:
return self.value1
val1 = None
val2 = None
if '$' in str(self.value1):
val1 = Spreadsheet.lookup(int(str(self.value1).replace('$', '')))
else:
val1 = self.value1
if '$' in str(self.value2):
val2 = Spreadsheet.lookup(int(str(self.value2).replace('$', '')))
else:
val2 = self.value2
if self.op == "MULT":
return int(val1) * int(val2)
elif self.op == "ADD":
return int(val1) + int(val2)
elif self.op == "SUB":
return int(val1) - int(val2)
n = int(input())
for i in range(n):
operation, arg_1, arg_2 = input().split()
Spreadsheet.add(Cell(operation, arg_1, arg_2))
for cell in Spreadsheet.get_sheet():
print(cell.value())