[Python] MOOC - Own_language exercise

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on this assignment now and it seems to work with the tests MOOC provides. You can find the assignment here: https://programming-24.mooc.fi/part-7/6-more-features

    The problem is that I get an error when I try to submit it, but I do not understand what is happening.

    My code:

    import string

    def run(program: list):
    commands_list = format_commands(program)
    locations = {}
    for commands in commands_list:
    action = commands[0]
    if action[-1] == ":":
    locations[action[:-1]] = commands_list.index(commands)
    length_commands_list = len(commands_list)
    from_index = 0
    while from_index < length_commands_list:
    command = commands_list[from_index]
    action = command[0]
    length_command = len(command)
    if length_command == 6:
    variabel = command[1]
    comparison = command[2]
    variabel2 = command[3]
    action2 = command[4]
    location = command[5]
    if comparison == ">=":
    if variabel2.isdigit():
    if variables[variabel] >= int(variabel2):
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    elif variables[variabel] <= variables[variabel2]:
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    if comparison == "<=":
    if variabel2.isdigit():
    if variables[variabel] <= int(variabel2):
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    else:
    if variables[variabel] <= variables[variabel2]:
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    if comparison == "==":
    if variabel2.isdigit():
    if variables[variabel] == int(variabel2):
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    else:
    if variables[variabel] == variables[variabel2]:
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    if comparison == ">":
    if variabel2.isdigit():
    if variables[variabel] > int(variabel2):
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    else:
    if variables[variabel] > variables[variabel2]:
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    if comparison == "<":
    if variabel2.isdigit():
    if variables[variabel] < int(variabel2):
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    else:
    if variables[variabel] < variables[variabel2]:
    if action2 == "JUMP":
    if command[-1] == "quit":
    break
    else:
    from_index = locations[location]
    from_index += 1
    elif length_command == 3:
    variabel = command[1]
    value = command[2]
    if action == "MOV":
    assignment(variables, variabel, value)
    elif action == "ADD":
    add_value(variables, variabel, value)
    elif action == "SUB":
    sub_value(variables, variabel, value)
    elif action == "MUL":
    multiply_value(variables, variabel, value)
    from_index += 1
    elif length_command == 2:
    location = command[1]
    if action == "PRINT":
    result = print_result(command, variables)
    elif action == "JUMP":
    from_index = locations[location]
    from_index += 1
    elif length_command == 1:
    if action == "END":
    break
    from_index += 1
    return result


    def format_commands(program: list):
    commands_list = []
    for command in program:
    command_list = command.split(" ")
    commands_list.append(command_list)
    return commands_list


    def print_result(command: str, variables_dict: dict):
    variabel = command[1]
    if variabel in variables_dict:
    printed.append(variables_dict[variabel])
    else:
    printed.append(int(variabel))
    return printed


    def assignment(variables_dict: dict, variabel: str, value: str):
    if value.isdigit():
    variables_dict[variabel] = int(value)
    else:
    variables_dict[variabel] = variables_dict[value]


    def add_value(variables_dict: dict, variabel: str, value: str):
    if value.isdigit():
    variables_dict[variabel] += int(value)
    else:
    variables_dict[variabel] += variables_dict[value]


    def sub_value(variables_dict: dict, variabel: str, value: str):
    if value.isdigit():
    variables_dict[variabel] -= int(value)
    else:
    variables_dict[variabel] -= variables_dict[value]


    def multiply_value(variables_dict: dict, variabel: str, value: str):
    if value.isdigit():
    variables_dict[variabel] *= int(value)
    else:
    variables_dict[variabel] *= variables_dict[value]



    if __name__ == "__main__":

    variables = {}
    for letter in string.ascii_uppercase:
    variables[letter] = 0

    printed = []


    I expect to be able to deliver the above task, since all the tests work. But I get this error instead:

    [​IMG]

    I've tried removing constants (I had constants before), running tests with the program they say is failing, etc.

    Test code:

    import unittest
    from unittest.mock import patch

    from tmc import points
    from tmc.utils import load, load_module, reload_module, get_stdout, check_source
    from functools import reduce
    import os
    import os.path
    import textwrap
    from random import choice, randint

    exercise = 'src.own_language'
    function = "run"

    class OwnLanguageTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
    with patch('builtins.input', side_effect=[AssertionError("Asking input from the user was not expected")]):
    cls.module = load_module(exercise, 'en')

    def test_0a_main_program_ok(self):
    ok, line = check_source(self.module)
    message = """The code for testng the functions should be placed inside
    if __name__ == "__main__":
    block. The following row should be moved:
    """
    self.assertTrue(ok, message+line)

    @points('7.own_programming_language-part1')
    def test1_function_exists(self):
    try:
    from src.own_language import run
    except:
    self.assertTrue(False, "Your program should contain function named as run")

    @points('7.own_programming_language-part1')
    def test2_no_loop(self):
    tests = []
    program1 = ["PRINT A","END"]
    result1 = [0]
    tests.append((program1,result1))
    program2 = ["MOV A 5","PRINT A"]
    result2 = [5]
    tests.append((program2,result2))
    program3 = ["MOV A 1","MOV B 1","ADD A B","ADD B A","ADD A B","ADD B A","PRINT A","PRINT B"]
    result3 = [5,8]
    tests.append((program3,result3))
    program4 = ["MOV A 2","MUL A A","MUL A A","MUL A A","MUL A A","PRINT A"]
    result4 = [65536]
    tests.append((program4,result4))
    program5 = ["MOV A 10","PRINT A","MOV B A","SUB B 8","PRINT B","SUB A B","PRINT A"]
    result5 = [10,2,8]
    tests.append((program5,result5))
    for test in tests:
    with patch('builtins.input', side_effect=[AssertionError("Asking input from the user was not expected")]):
    reload_module(self.module)
    run = load(exercise, function, 'en')
    try:
    result = run(test[0])
    except:
    self.assertFalse(True, "Program "+str(test[0])+" caused an error")
    self.assertEqual(result, test[1], "Program "+str(test[0])+" returns an incorrect result "+str(result)+", the correct result would be "+str(test[1]))

    @points('7.own_programming_language-part2')
    def test3_all_commands(self):
    tests = []
    program1 = ["PRINT A","END"]
    result1 = [0]
    tests.append((program1,result1))
    program2 = []
    result2 = []
    tests.append((program2,result2))
    program3 = ["MOV A 10","start:","PRINT A","SUB A 1","IF A > 0 JUMP start","END"]
    result3 = [10,9,8,7,6,5,4,3,2,1]
    tests.append((program3,result3))
    program4 = ["MOV A 1","MOV B 1","start:","MUL A 2","ADD B 1","IF B != 101 JUMP start","PRINT A"]
    result4 = [1267650600228229401496703205376]
    tests.append((program4,result4))
    program5 = ["MOV A 1","MOV B 999","start:","ADD A 1","SUB B 1","ADD C 1","IF A == B JUMP end","JUMP start","end:","PRINT C"]
    result5 = [499]
    tests.append((program5,result5))
    program6 = ["MOV N 100","PRINT 2","MOV A 3","start:","MOV B 2","MOV Z 0","test:","MOV C B","new:","IF C == A JUMP virhe","IF C > A JUMP pass_by","ADD C B","JUMP new","virhe:","MOV Z 1","JUMP pass_by2","pass_by:","ADD B 1","IF B < A JUMP test","pass_by2:","IF Z == 1 JUMP pass_by3","PRINT A","pass_by3:","ADD A 1","IF A <= N JUMP start"]
    result6 = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
    tests.append((program6,result6))
    for test in tests:
    with patch('builtins.input', side_effect=[AssertionError("Asking input from the user was not expected")]):
    reload_module(self.module)
    run = load(exercise, function, 'en')
    try:
    result = run(test[0])
    except:
    self.assertFalse(True, "Program "+str(test[0])+" causes on error")
    self.assertEqual(result, test[1], "Program "+str(test[0])+" returns an incorrect result "+str(result)+", the correct result would be "+str(test[1]))

    if __name__ == '__main__':
    unittest.main()

    Continue reading...

Compartilhe esta Página