'''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 '''