| 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 unsupported operations, |
| 13 | or is not a math question |
| 14 | """ |
| 15 | # Edge Case: Non-math questions |
| 16 | if not question.startswith("What is ") or not question.endswith("?"): |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Extract the expression part |
| 20 | expression = question[8:-1].strip() |
| 21 | |
| 22 | # Edge Case: Empty expression |
| 23 | if not expression: |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Tokenize the expression |
| 27 | tokens = expression.split() |
| 28 | |
| 29 | # Edge Case: Just a number |
| 30 | if len(tokens) == 1: |
| 31 | try: |
| 32 | return int(tokens[0]) |
| 33 | except ValueError: |
| 34 | # Edge Case: Single token that's not a number |
| 35 | raise ValueError("syntax error") |
| 36 | |
| 37 | # Parse tokens into numbers and operations |
| 38 | parsed_tokens = [] |
| 39 | i = 0 |
| 40 | while i < len(tokens): |
| 41 | token = tokens[i] |
| 42 | |
| 43 | # Try to parse as number |
| 44 | try: |
| 45 | num = int(token) |
| 46 | parsed_tokens.append(num) |
| 47 | i += 1 |
| 48 | continue |
| 49 | except ValueError: |
| 50 | pass |
| 51 | |
| 52 | # Handle operations |
| 53 | if token == "plus": |
| 54 | parsed_tokens.append("+") |
| 55 | elif token == "minus": |
| 56 | parsed_tokens.append("-") |
| 57 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 58 | parsed_tokens.append("*") |
| 59 | i += 1 # Skip "by" |
| 60 | elif token == "divided" and i + 1 < len(tokens) and tokens[i + 1] == "by": |
| 61 | parsed_tokens.append("/") |
| 62 | i += 1 # Skip "by" |
| 63 | else: |
| 64 | # Edge Case: Unknown operations |
| 65 | raise ValueError("unknown operation") |
| 66 | |
| 67 | i += 1 |
| 68 | |
| 69 | # Edge Case: Invalid syntax - empty tokens after parsing |
| 70 | if not parsed_tokens: |
| 71 | raise ValueError("syntax error") |
| 72 | |
| 73 | # Edge Case: Invalid syntax - expression must start with number |
| 74 | if not isinstance(parsed_tokens[0], int): |
| 75 | raise ValueError("syntax error") |
| 76 | |
| 77 | # Evaluate left to right |
| 78 | result = parsed_tokens[0] |
| 79 | i = 1 |
| 80 | |
| 81 | # Edge Case: Invalid syntax - must have operation followed by number |
| 82 | while i < len(parsed_tokens): |
| 83 | # Expect an operation |
| 84 | if i % 2 == 1: # Odd indices should be operations |
| 85 | if not isinstance(parsed_tokens[i], str): |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | operation = parsed_tokens[i] |
| 89 | |
| 90 | # Edge Case: Not enough tokens |
| 91 | if i + 1 >= len(parsed_tokens): |
| 92 | raise ValueError("syntax error") |
| 93 | |
| 94 | # Expect a number |
| 95 | if not isinstance(parsed_tokens[i + 1], int): |
| 96 | raise ValueError("syntax error") |
| 97 | |
| 98 | operand = parsed_tokens[i + 1] |
| 99 | |
| 100 | if operation == "+": |
| 101 | result += operand |
| 102 | elif operation == "-": |
| 103 | result -= operand |
| 104 | elif operation == "*": |
| 105 | result *= operand |
| 106 | elif operation == "/": |
| 107 | result //= operand # Integer division as per examples |
| 108 | else: |
| 109 | # Edge Case: Unknown operation (should not happen but for safety) |
| 110 | raise ValueError("unknown operation") |
| 111 | |
| 112 | i += 2 |
| 113 | |
| 114 | # Edge Case: Invalid syntax - wrong number of tokens |
| 115 | if (len(parsed_tokens) - 1) % 2 != 0: |
| 116 | raise ValueError("syntax error") |
| 117 | |
| 118 | return result |
| 119 | # Handled Edge Cases: Non-math questions, empty expression, single non-number token, |
| 120 | # unknown operations, invalid syntax (expression not starting with number), |
| 121 | # not enough tokens, wrong token types, wrong number of tokens |