Finished
11class StackUnderflowError(Exception):
2 pass
2 """Exception raised when Stack is not full.
3 message: explanation of the error.
4 """
5 def __init__(self, message):
6 self.message = message
37
48
59def evaluate(input_data: list[str]) -> list[int]:
6 pass
10 stack = []
11 definitions = {}
12
13 for line in input_data:
14 tokens = line.split()
15 i = 0
16 while i < len(tokens):
17 token = tokens[i].lower()
18
19 # Handle number
20 if token.lstrip('-').isdigit():
21 stack.append(int(token))
22 i += 1
23 continue
24
25 # Handle defined words
26 if token in definitions:
27 # Replace the token with its definition and reprocess
28 expanded = definitions[token]
29 tokens = tokens[:i] + expanded + tokens[i+1:]
30 continue
31
32 # Handle built-in operations
33 if token == '+':
34 # Edge Case: Insufficient stack items for addition
35 if len(stack) < 2:
36 raise StackUnderflowError("Insufficient number of items in stack")
37 b = stack.pop()
38 a = stack.pop()
39 stack.append(a + b)
40
41 elif token == '-':
42 # Edge Case: Insufficient stack items for subtraction
43 if len(stack) < 2:
44 raise StackUnderflowError("Insufficient number of items in stack")
45 b = stack.pop()
46 a = stack.pop()
47 stack.append(a - b)
48
49 elif token == '*':
50 # Edge Case: Insufficient stack items for multiplication
51 if len(stack) < 2:
52 raise StackUnderflowError("Insufficient number of items in stack")
53 b = stack.pop()
54 a = stack.pop()
55 stack.append(a * b)
56
57 elif token == '/':
58 # Edge Case: Insufficient stack items for division
59 if len(stack) < 2:
60 raise StackUnderflowError("Insufficient number of items in stack")
61 b = stack.pop()
62 a = stack.pop()
63 # Edge Case: Division by zero
64 if b == 0:
65 raise ZeroDivisionError("divide by zero")
66 stack.append(a // b)
67
68 elif token == 'dup':
69 # Edge Case: Insufficient stack items for dup
70 if len(stack) < 1:
71 raise StackUnderflowError("Insufficient number of items in stack")
72 stack.append(stack[-1])
73
74 elif token == 'drop':
75 # Edge Case: Insufficient stack items for drop
76 if len(stack) < 1:
77 raise StackUnderflowError("Insufficient number of items in stack")
78 stack.pop()
79
80 elif token == 'swap':
81 # Edge Case: Insufficient stack items for swap
82 if len(stack) < 2:
83 raise StackUnderflowError("Insufficient number of items in stack")
84 b = stack.pop()
85 a = stack.pop()
86 stack.append(b)
87 stack.append(a)
88
89 elif token == 'over':
90 # Edge Case: Insufficient stack items for over
91 if len(stack) < 2:
92 raise StackUnderflowError("Insufficient number of items in stack")
93 stack.append(stack[-2])
94
95 elif token == ':':
96 # Handle word definition
97 # Edge Case: Invalid word definition - missing name
98 if i + 1 >= len(tokens):
99 raise ValueError("undefined operation")
100
101 word_name = tokens[i+1].lower()
102 # Edge Case: Invalid word definition - redefining a number
103 if word_name.lstrip('-').isdigit():
104 raise ValueError("undefined operation")
105
106 # Find the ending ';'
107 j = i + 2
108 definition = []
109 found_end = False
110 while j < len(tokens):
111 if tokens[j] == ';':
112 found_end = True
113 break
114 definition.append(tokens[j])
115 j += 1
116
117 # Edge Case: Invalid word definition - missing semicolon
118 if not found_end:
119 raise ValueError("undefined operation")
120
121 # Expand any existing definitions in the new definition
122 expanded_def = []
123 for def_token in definition:
124 def_token_lower = def_token.lower()
125 if def_token_lower in definitions:
126 expanded_def.extend(definitions[def_token_lower])
127 else:
128 expanded_def.append(def_token)
129
130 definitions[word_name] = expanded_def
131 i = j + 1 # Skip to after the semicolon
132 continue
133
134 else:
135 # Edge Case: Undefined operation
136 raise ValueError("undefined operation")
137
138 i += 1
139
140 # Handled Edge Cases: Insufficient stack items for operations, division by zero, invalid word definitions, undefined operations, redefining numbers
141
142 return stack
Test NameStatus
test_user_defined_words_cannot_redefine_negative_numbers
Fail
test_user_defined_words_cannot_redefine_non_negative_numbers
Fail
test_addition_can_add_two_numbers
Pass
test_addition_errors_if_there_is_nothing_on_the_stack
Pass
test_addition_errors_if_there_is_only_one_value_on_the_stack
Pass
test_addition_more_than_two_values_on_the_stack
Pass
test_case_insensitivity_definitions_are_case_insensitive
Pass
test_case_insensitivity_drop_is_case_insensitive
Pass
test_case_insensitivity_dup_is_case_insensitive
Pass
test_case_insensitivity_over_is_case_insensitive
Pass
test_case_insensitivity_swap_is_case_insensitive
Pass
test_case_insensitivity_user_defined_words_are_case_insensitive
Pass
test_combined_arithmetic_addition_and_multiplication
Pass
test_combined_arithmetic_addition_and_subtraction
Pass
test_combined_arithmetic_multiplication_and_addition
Pass
test_combined_arithmetic_multiplication_and_division
Pass
test_division_can_divide_two_numbers
Pass
test_division_errors_if_dividing_by_zero
Pass
test_division_errors_if_there_is_nothing_on_the_stack
Pass
test_division_errors_if_there_is_only_one_value_on_the_stack
Pass
test_division_more_than_two_values_on_the_stack
Pass
test_division_performs_integer_division
Pass
test_drop_errors_if_there_is_nothing_on_the_stack
Pass
test_drop_removes_the_top_value_on_the_stack_if_it_is_not_the_only_one
Pass
test_drop_removes_the_top_value_on_the_stack_if_it_is_the_only_one
Pass
test_dup_copies_a_value_on_the_stack
Pass
test_dup_copies_the_top_value_on_the_stack
Pass
test_dup_errors_if_there_is_nothing_on_the_stack
Pass
test_multiplication_can_multiply_two_numbers
Pass
test_multiplication_errors_if_there_is_nothing_on_the_stack
Pass
test_multiplication_errors_if_there_is_only_one_value_on_the_stack
Pass
test_multiplication_more_than_two_values_on_the_stack
Pass
test_over_copies_the_second_element_if_there_are_more_than_two
Pass
test_over_copies_the_second_element_if_there_are_only_two
Pass
test_over_errors_if_there_is_nothing_on_the_stack
Pass
test_over_errors_if_there_is_only_one_value_on_the_stack
Pass
test_parsing_and_numbers_numbers_just_get_pushed_onto_the_stack
Pass
test_parsing_and_numbers_pushes_negative_numbers_onto_the_stack
Pass
test_subtraction_can_subtract_two_numbers
Pass
test_subtraction_errors_if_there_is_nothing_on_the_stack
Pass
test_subtraction_errors_if_there_is_only_one_value_on_the_stack
Pass
test_subtraction_more_than_two_values_on_the_stack
Pass
test_swap_errors_if_there_is_nothing_on_the_stack
Pass
test_swap_errors_if_there_is_only_one_value_on_the_stack
Pass
test_swap_swaps_the_top_two_values_on_the_stack_if_they_are_not_the_only_ones
Pass
test_swap_swaps_the_top_two_values_on_the_stack_if_they_are_the_only_ones
Pass
test_user_defined_words_can_consist_of_built_in_words
Pass
test_user_defined_words_can_define_word_that_uses_word_with_the_same_name
Pass
test_user_defined_words_can_override_built_in_operators
Pass
test_user_defined_words_can_override_built_in_words
Pass
test_user_defined_words_can_override_other_user_defined_words
Pass
test_user_defined_words_can_use_different_words_with_the_same_name
Pass
test_user_defined_words_errors_if_executing_a_non_existent_word
Pass
test_user_defined_words_execute_in_the_right_order
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.