| 1 | 1 | def answer(question: str) -> int: |
| 2 | | pass |
| 2 | """ |
| 3 | Parse and evaluate a simple math word problem. |
| 4 | |
| 5 | Args: |
| 6 | question: A string representing a math word problem |
| 7 | |
| 8 | Returns: |
| 9 | int: The result of evaluating the math expression |
| 10 | |
| 11 | Raises: |
| 12 | ValueError: If the question is malformed, contains unknown operations, or is not a math question |
| 13 | """ |
| 14 | # Edge Case: Empty or None input |
| 15 | if not question: |
| 16 | raise ValueError("syntax error") |
| 17 | |
| 18 | # Edge Case: Question doesn't start with "What is" |
| 19 | if not question.startswith("What is"): |
| 20 | raise ValueError("syntax error") |
| 21 | |
| 22 | # Remove "What is" and "?" from the question |
| 23 | if not question.endswith('?'): |
| 24 | raise ValueError("syntax error") |
| 25 | expression = question[8:-1].strip() |
| 26 | |
| 27 | # Edge Case: Empty expression after removing "What is" and "?" |
| 28 | if not expression: |
| 29 | raise ValueError("syntax error") |
| 30 | |
| 31 | # Split the expression into tokens |
| 32 | tokens = expression.split() |
| 33 | |
| 34 | # Edge Case: No tokens |
| 35 | if not tokens: |
| 36 | raise ValueError("syntax error") |
| 37 | |
| 38 | # Define operation mappings |
| 39 | operations = { |
| 40 | "plus": "+", |
| 41 | "minus": "-", |
| 42 | "multiplied": "*", |
| 43 | "divided": "/" |
| 44 | } |
| 45 | |
| 46 | # Parse tokens into a list of numbers and operations |
| 47 | parsed_tokens = [] |
| 48 | i = 0 |
| 49 | |
| 50 | while i < len(tokens): |
| 51 | token = tokens[i] |
| 52 | |
| 53 | # Try to parse as number |
| 54 | try: |
| 55 | # Handle negative numbers |
| 56 | if token == "-" and i + 1 < len(tokens): |
| 57 | # Check if next token is a number |
| 58 | next_token = tokens[i + 1] |
| 59 | try: |
| 60 | num = int(next_token) |
| 61 | parsed_tokens.append(-num) |
| 62 | i += 2 |
| 63 | continue |
| 64 | except ValueError: |
| 65 | pass |
| 66 | |
| 67 | num = int(token) |
| 68 | parsed_tokens.append(num) |
| 69 | i += 1 |
| 70 | continue |
| 71 | except ValueError: |
| 72 | pass |
| 73 | |
| 74 | # Check for operations |
| 75 | if token in operations: |
| 76 | parsed_tokens.append(operations[token]) |
| 77 | i += 1 |
| 78 | # Special handling for "multiplied by" and "divided by" |
| 79 | if token == "multiplied" or token == "divided": |
| 80 | # Edge Case: Missing "by" after "multiplied" or "divided" |
| 81 | if i >= len(tokens) or tokens[i] != "by": |
| 82 | raise ValueError("syntax error") |
| 83 | i += 1 |
| 84 | continue |
| 85 | |
| 86 | # Edge Case: Unknown operation |
| 87 | raise ValueError("unknown operation") |
| 88 | |
| 89 | # Edge Case: Expression with just a number |
| 90 | if len(parsed_tokens) == 1: |
| 91 | if isinstance(parsed_tokens[0], int): |
| 92 | return parsed_tokens[0] |
| 93 | else: |
| 94 | # This shouldn't happen with our parsing, but just in case |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Even number of tokens (should be odd: number, operation, number, ...) |
| 98 | if len(parsed_tokens) % 2 == 0: |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | # Evaluate from left to right |
| 102 | result = parsed_tokens[0] |
| 103 | |
| 104 | # Edge Case: First token is not a number |
| 105 | if not isinstance(result, int): |
| 106 | raise ValueError("syntax error") |
| 107 | |
| 108 | i = 1 |
| 109 | while i < len(parsed_tokens): |
| 110 | # Edge Case: Missing operation or number |
| 111 | if i + 1 >= len(parsed_tokens): |
| 112 | raise ValueError("syntax error") |
| 113 | |
| 114 | operation = parsed_tokens[i] |
| 115 | next_number = parsed_tokens[i + 1] |
| 116 | |
| 117 | # Edge Case: Invalid operation type |
| 118 | if not isinstance(operation, str): |
| 119 | raise ValueError("syntax error") |
| 120 | |
| 121 | # Edge Case: Next token is not a number |
| 122 | if not isinstance(next_number, int): |
| 123 | raise ValueError("syntax error") |
| 124 | |
| 125 | if operation == "+": |
| 126 | result += next_number |
| 127 | elif operation == "-": |
| 128 | result -= next_number |
| 129 | elif operation == "*": |
| 130 | result *= next_number |
| 131 | elif operation == "/": |
| 132 | # Edge Case: Division by zero |
| 133 | if next_number == 0: |
| 134 | raise ValueError("syntax error") # or we could let Python raise ZeroDivisionError |
| 135 | result //= next_number # Integer division as per examples |
| 136 | else: |
| 137 | # Edge Case: Unknown operation in evaluation |
| 138 | raise ValueError("unknown operation") |
| 139 | |
| 140 | i += 2 |
| 141 | |
| 142 | return result |
| 143 | # Handled Edge Cases: Empty input, None input, questions not starting with "What is", |
| 144 | # empty expression, no tokens, unknown operations, missing "by" after |
| 145 | # "multiplied" or "divided", expression with just a number, even number |
| 146 | # of tokens, first token not a number, missing operation or number, |
| 147 | # invalid operation type, next token not a number, division by zero, |
| 148 | # unknown operation in evaluation |