File size: 5,246 Bytes
144c362
 
 
aebc510
144c362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'''Add, subtract

Implement the function
execute(code: str) -> str 
in addition to the properties of the previous functions, the following additional commands:


INC variable number
The command adds the given integer to the given variable. The value of the variable is thus incremented by the given number.

DEC variable number
This command decreases the given variable by the given integer. The value of the variable is therefore decreased by the given number.

INC variable1 variable2
The command adds the value of variable 2 to the value of variable 1. The value of variable 1 is thus increased by the value of variable 2.

DEC variable1 variable2
The command decreases the value of variable 1 by the value of variable 2. The value of variable 1 is therefore decreased by the value of variable 2.



Example programs for testing:

Program:
LET a=20
INC a 10
PRINT a
The program returns the output:
30




Program:
LET a=100
LET b=150
DEC a 50
DEC b 200
PRINT a
PRINT b
The program returns the result:
50
-50




Program:
LET a=100
LET b=50
DEC a b
PRINT a
PRINT b
INC a b
PRINT a
The program returns the result:
50
50
100




Program:
LET d=1000
LET a=100
LET b=10
LET c=1
INC d a
INC d b
INC d c
PRINT d
DEC d a
DEC d b
DEC d c
PRINT d
The program returns the result:
1111
1000
'''






def execute(code: str) -> str:
    variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
    output = []
    
    for line in code.split('\n'):
        line = line.strip()

        #########################
        # round 2 - Skip empty lines
        if not line:
            continue
        #########################
        
        if line.startswith('LET'):
            _, assignment = line.split(' ', 1)
            var, value = assignment.split('=')
            if var in variables:
                variables[var] = int(value)
            else:
                raise ValueError(f"Invalid variable name: {var}")
        elif line.startswith('PRINT'):
            _, var = line.split()
            if var in variables:
                output.append(str(variables[var]))
            else:
                raise ValueError(f"Undefined variable: {var}")




        ##########################################
        # 19. part 2
        ##########################################
        elif line.startswith('INC') or line.startswith('DEC'):
            # Handle INC and DEC commands
            parts = line.split()
            command = parts[0]
            var1 = parts[1]
            
            if var1 not in variables:
                raise ValueError(f"Invalid variable name: {var1}")




            if len(parts) == 3:
                # Check if parts[2] is an integer
                try:
                    value = int(parts[2])  # Try to convert parts[2] to an integer
                    
                    # If conversion succeeds
                    # INC/DEC variable number
                    if command == 'INC':
                        variables[var1] += value
                    elif command == 'DEC':
                        variables[var1] -= value



                        
                # parts[2] is NOT an integer
                except ValueError:
                    # INC/DEC variable1 variable2
                    var2 = parts[2]
                    if var2 not in variables:
                        raise ValueError(f"Invalid variable name: {var2}")
                    
                    if command == 'INC':
                        variables[var1] += variables[var2]
                    elif command == 'DEC':
                        variables[var1] -= variables[var2]        
            
            else:
                raise ValueError(f"Invalid command format: {line}")
        ##########################################


                
        else:
            raise ValueError(f"Invalid command: {line}")

            

    
    joined_output = '\n'.join(output)
    joined_output+='\n'
    return joined_output






eg1='''LET a=20
INC a 10
PRINT a'''

print(execute(eg1))
30





eg2='''LET a=100
LET b=150
DEC a 50
DEC b 200
PRINT a
PRINT b'''

print(execute(eg2))
50
-50




eg3='''LET a=100
LET b=50
DEC a b
PRINT a
PRINT b
INC a b
PRINT a'''

print(execute(eg3))
50
50
100





eg4='''LET d=1000
LET a=100
LET b=10
LET c=1
INC d a
INC d b
INC d c
PRINT d
DEC d a
DEC d b
DEC d c
PRINT d'''

print(execute(eg4))
1111
1000









'''round 1 - autograder error

Error in program execution
line 110, in
testaa(koodi)
File "/tmp/untrusted/test056323216-d11f-433b-8fcd-7f9f59117650/test.py", line 98, in testaa
lopputulos = execute(koodi)
File "/tmp/untrusted/test056323216-d11f-433b-8fcd-7f9f59117650/test.py", line 82, in execute
raise ValueError(f"Invalid command: {line}")
ValueError: Invalid command:
'''





'''round 2 - autograder 20/20. actual vs expected output (62 lines)
Testing code:

LET a=72
INC a 19
PRINT a

Program outputs:
91

Testing code:

LET a=76
LET b=380

DEC a 13
DEC b 65

PRINT a
PRINT b

Program outputs:
63
315

Testing code:

LET a=72
LET b=10

DEC a b
PRINT a
PRINT b

INC a b
PRINT a

Program outputs:
62
10
72

Testing code:

LET d=3000
LET a=300
LET b=30
LET c=3

INC d a
INC d b
INC d c
PRINT d

DEC d a
DEC d b
DEC d c
PRINT d


Program outputs:
3333
3000
'''