KaiquanMah commited on
Commit
144c362
·
verified ·
1 Parent(s): 05f08c7

[FIX] Skip empty lines

Browse files
Week 7 Libraries and More Python/[FIXED] 19. Own programming language, Part 2 Add, subtract ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Add, subtract
2
+
3
+ Implement the function
4
+ execute(code: str) -> str
5
+ in addition to the properties of the previous functions, the following additional commands:
6
+
7
+
8
+ INC variable number
9
+ The command adds the given integer to the given variable. The value of the variable is thus incremented by the given number.
10
+
11
+ DEC variable number
12
+ This command decreases the given variable by the given integer. The value of the variable is therefore decreased by the given number.
13
+
14
+ INC variable1 variable2
15
+ 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.
16
+
17
+ DEC variable1 variable2
18
+ 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.
19
+
20
+
21
+
22
+ Example programs for testing:
23
+
24
+ Program:
25
+ LET a=20
26
+ INC a 10
27
+ PRINT a
28
+ The program returns the output:
29
+ 30
30
+
31
+
32
+
33
+
34
+ Program:
35
+ LET a=100
36
+ LET b=150
37
+ DEC a 50
38
+ DEC b 200
39
+ PRINT a
40
+ PRINT b
41
+ The program returns the result:
42
+ 50
43
+ -50
44
+
45
+
46
+
47
+
48
+ Program:
49
+ LET a=100
50
+ LET b=50
51
+ DEC a b
52
+ PRINT a
53
+ PRINT b
54
+ INC a b
55
+ PRINT a
56
+ The program returns the result:
57
+ 50
58
+ 50
59
+ 100
60
+
61
+
62
+
63
+
64
+ Program:
65
+ LET d=1000
66
+ LET a=100
67
+ LET b=10
68
+ LET c=1
69
+ INC d a
70
+ INC d b
71
+ INC d c
72
+ PRINT d
73
+ DEC d a
74
+ DEC d b
75
+ DEC d c
76
+ PRINT d
77
+ The program returns the result:
78
+ 1111
79
+ 1000
80
+ '''
81
+
82
+
83
+
84
+
85
+
86
+
87
+ def execute(code: str) -> str:
88
+ variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
89
+ output = []
90
+
91
+ for line in code.split('\n'):
92
+ line = line.strip()
93
+
94
+ #########################
95
+ # round 2 - Skip empty lines
96
+ if not line:
97
+ continue
98
+ #########################
99
+
100
+ if line.startswith('LET'):
101
+ _, assignment = line.split(' ', 1)
102
+ var, value = assignment.split('=')
103
+ if var in variables:
104
+ variables[var] = int(value)
105
+ else:
106
+ raise ValueError(f"Invalid variable name: {var}")
107
+ elif line.startswith('PRINT'):
108
+ _, var = line.split()
109
+ if var in variables:
110
+ output.append(str(variables[var]))
111
+ else:
112
+ raise ValueError(f"Undefined variable: {var}")
113
+
114
+
115
+
116
+
117
+ ##########################################
118
+ # 19. part 2
119
+ ##########################################
120
+ elif line.startswith('INC') or line.startswith('DEC'):
121
+ # Handle INC and DEC commands
122
+ parts = line.split()
123
+ command = parts[0]
124
+ var1 = parts[1]
125
+
126
+ if var1 not in variables:
127
+ raise ValueError(f"Invalid variable name: {var1}")
128
+
129
+
130
+
131
+
132
+ if len(parts) == 3:
133
+ # Check if parts[2] is an integer
134
+ try:
135
+ value = int(parts[2]) # Try to convert parts[2] to an integer
136
+
137
+ # If conversion succeeds
138
+ # INC/DEC variable number
139
+ if command == 'INC':
140
+ variables[var1] += value
141
+ elif command == 'DEC':
142
+ variables[var1] -= value
143
+
144
+
145
+
146
+
147
+ # parts[2] is NOT an integer
148
+ except ValueError:
149
+ # INC/DEC variable1 variable2
150
+ var2 = parts[2]
151
+ if var2 not in variables:
152
+ raise ValueError(f"Invalid variable name: {var2}")
153
+
154
+ if command == 'INC':
155
+ variables[var1] += variables[var2]
156
+ elif command == 'DEC':
157
+ variables[var1] -= variables[var2]
158
+
159
+ else:
160
+ raise ValueError(f"Invalid command format: {line}")
161
+ ##########################################
162
+
163
+
164
+
165
+ else:
166
+ raise ValueError(f"Invalid command: {line}")
167
+
168
+
169
+
170
+
171
+ joined_output = '\n'.join(output)
172
+ joined_output+='\n'
173
+ return joined_output
174
+
175
+
176
+
177
+
178
+
179
+
180
+ eg1='''LET a=20
181
+ INC a 10
182
+ PRINT a'''
183
+
184
+ print(execute(eg1))
185
+ 30
186
+
187
+
188
+
189
+
190
+
191
+ eg2='''LET a=100
192
+ LET b=150
193
+ DEC a 50
194
+ DEC b 200
195
+ PRINT a
196
+ PRINT b'''
197
+
198
+ print(execute(eg2))
199
+ 50
200
+ -50
201
+
202
+
203
+
204
+
205
+ eg3='''LET a=100
206
+ LET b=50
207
+ DEC a b
208
+ PRINT a
209
+ PRINT b
210
+ INC a b
211
+ PRINT a'''
212
+
213
+ print(execute(eg3))
214
+ 50
215
+ 50
216
+ 100
217
+
218
+
219
+
220
+
221
+
222
+ eg4='''LET d=1000
223
+ LET a=100
224
+ LET b=10
225
+ LET c=1
226
+ INC d a
227
+ INC d b
228
+ INC d c
229
+ PRINT d
230
+ DEC d a
231
+ DEC d b
232
+ DEC d c
233
+ PRINT d'''
234
+
235
+ print(execute(eg4))
236
+ 1111
237
+ 1000
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+ '''round 1 - autograder error
248
+
249
+ Error in program execution
250
+ line 110, in
251
+ testaa(koodi)
252
+ File "/tmp/untrusted/test056323216-d11f-433b-8fcd-7f9f59117650/test.py", line 98, in testaa
253
+ lopputulos = execute(koodi)
254
+ File "/tmp/untrusted/test056323216-d11f-433b-8fcd-7f9f59117650/test.py", line 82, in execute
255
+ raise ValueError(f"Invalid command: {line}")
256
+ ValueError: Invalid command:
257
+ '''
258
+
259
+
260
+
261
+
262
+
263
+ '''round 2 - autograder 20/20. actual vs expected output (62 lines)
264
+ Testing code:
265
+
266
+ LET a=72
267
+ INC a 19
268
+ PRINT a
269
+
270
+ Program outputs:
271
+ 91
272
+
273
+ Testing code:
274
+
275
+ LET a=76
276
+ LET b=380
277
+
278
+ DEC a 13
279
+ DEC b 65
280
+
281
+ PRINT a
282
+ PRINT b
283
+
284
+ Program outputs:
285
+ 63
286
+ 315
287
+
288
+ Testing code:
289
+
290
+ LET a=72
291
+ LET b=10
292
+
293
+ DEC a b
294
+ PRINT a
295
+ PRINT b
296
+
297
+ INC a b
298
+ PRINT a
299
+
300
+ Program outputs:
301
+ 62
302
+ 10
303
+ 72
304
+
305
+ Testing code:
306
+
307
+ LET d=3000
308
+ LET a=300
309
+ LET b=30
310
+ LET c=3
311
+
312
+ INC d a
313
+ INC d b
314
+ INC d c
315
+ PRINT d
316
+
317
+ DEC d a
318
+ DEC d b
319
+ DEC d c
320
+ PRINT d
321
+
322
+
323
+ Program outputs:
324
+ 3333
325
+ 3000
326
+ '''