Spaces:
No application file
No application file
File size: 7,164 Bytes
d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 d56525e 16c3393 |
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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
'''Condition clause
Implement the function
execute(code: str) -> str
in addition to the properties of the previous functions:
JUMP place IF variable==value
Code execution will continue from the named location after the command if the value of the given variable satisfies the condition.
You can assume that the value is a CONSTANT VALUE (not another variable).
Also, the program does not need to understand anything other than the equality operator.
Example programs for testing instructions:
Program:
LET a=13
JUMP end IF a==13 # if variable matches the condition, JUMP to the 'end' LABEL
INC a 10 # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines
INC a 15 # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines
LABEL end # JUMP here
PRINT a
Execution of the program returns:
13
Program:
LET a=27
LABEL start # from below
PRINT a
INC a 1
JUMP end IF a==32 # if variable matches the condition, JUMP to the 'end' LABEL
GOTO start # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines. If not JUMPing, go up to 'start' LABEL
LABEL end
Program execution returns:
27
28
29
30
31
Programme:
LET c=12
LET d=1
LABEL loop
PRINT c
DEC c d
JUMP continue IF c==1 # if variable matches the condition, JUMP to the 'continue' LABEL
GOTO loop # SKIP THIS LINE if JUMP. It is in between the JUMP and LABEL end lines. If not JUMPing, go up to 'loop' LABEL
LABEL continue
Program execution returns:
12
11
10
9
8
7
6
5
4
3
2
'''
def execute(code: str) -> str:
variables = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
output = []
# Split the code into lines and strip whitespace
lines = [line.strip() for line in code.split('\n')]
# Track labels and their corresponding line numbers
labels = {}
for i, line in enumerate(lines):
if line.startswith('LABEL'):
_, location = line.split()
labels[location] = i # Store the line number of the label
i = 0
while i < len(lines):
line = lines[i]
# Skip empty lines
if not line:
i += 1
continue
if line.startswith('LET'):
_, assignment = line.split(' ', 1)
var, value = assignment.split('=')
if var in variables:
variables[var] = int(value)
i += 1
else:
raise ValueError(f"Invalid variable name: {var}")
elif line.startswith('PRINT'):
_, var = line.split()
if var in variables:
output.append(str(variables[var]))
i += 1
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
i += 1
# 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]
i += 1
else:
raise ValueError(f"Invalid command format: {line}")
##########################################
##########################################
# 20. part 3
##########################################
elif line.startswith('LABEL'):
# Labels are already processed, so just 'skip' the line
i += 1
elif line.startswith('GOTO'):
_, location = line.split()
if location in labels:
i = labels[location] # Jump to the line number of the label
else:
raise ValueError(f"Undefined label: {location}")
##########################################
##########################################
# 21. part 4
##########################################
elif line.startswith('JUMP'):
parts = line.split()
# eg JUMP end IF a==13 --becomes--> ['JUMP','end','IF','a==13']
# Format: JUMP jump_to_locationname IF variable==value
jump_to_locationname = parts[1]
condition = parts[3]
var, value = condition.split('==')
# Check if the variable exists
if var not in variables:
raise ValueError(f"Undefined variable: {var}")
# Check the condition
if variables[var] == int(value):
# Jump to the specified label
if jump_to_locationname in labels:
i = labels[jump_to_locationname]
else:
raise ValueError(f"Undefined label: {jump_to_locationname}")
else:
# Continue to the next line
i += 1
##########################################
else:
raise ValueError(f"Invalid command: {line}")
joined_output = '\n'.join(output)
joined_output+='\n'
return joined_output
eg1='''LET a=13
JUMP end IF a==13
INC a 10
INC a 15
LABEL end
PRINT a'''
print(execute(eg1))
13
eg2='''LET a=27
LABEL start
PRINT a
INC a 1
JUMP end IF a==32
GOTO start
LABEL end'''
print(execute(eg2))
27
28
29
30
31
eg3='''LET c=12
LET d=1
LABEL loop
PRINT c
DEC c d
JUMP continue IF c==1
GOTO loop
LABEL continue'''
print(execute(eg3))
12
11
10
9
8
7
6
5
4
3
2
'''Autograder actual, expected output (53 lines)
Testing code:
LET a=25
JUMP end IF a==25
INC a 10
INC a 15
LABEL end
PRINT a
Program outputs:
25
Testing code:
LET a=62
LABEL start
PRINT a
INC a 1
JUMP end IF a==67
GOTO start
LABEL end
Program outputs:
62
63
64
65
66
Testing code:
LET c=13
LET d=1
LABEL loop
PRINT c
DEC c d
JUMP continue IF c==1
GOTO loop
LABEL continue
Program outputs:
13
12
11
10
9
8
7
6
5
4
3
2
''' |