| 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 has invalid syntax |
| 14 | """ |
| 15 | # Edge Case: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Check if question starts with "What is" and ends with "?" |
| 20 | if not question.startswith("What is") or not question.endswith("?"): |
| 21 | raise ValueError("unknown operation") |
| 22 | |
| 23 | # Extract the expression part |
| 24 | expression = question[8:-1].strip() |
| 25 | |
| 26 | # Edge Case: Empty expression after "What is" and before "?" |
| 27 | if not expression: |
| 28 | raise ValueError("syntax error") |
| 29 | |
| 30 | # Split the expression into tokens |
| 31 | tokens = expression.split() |
| 32 | |
| 33 | # Edge Case: No tokens |
| 34 | if not tokens: |
| 35 | raise ValueError("syntax error") |
| 36 | |
| 37 | # Define operation mappings |
| 38 | operations = { |
| 39 | "plus": "+", |
| 40 | "minus": "-", |
| 41 | "multiplied": "*", |
| 42 | "divided": "/" |
| 43 | } |
| 44 | |
| 45 | # Parse tokens |
| 46 | parsed_tokens = [] |
| 47 | i = 0 |
| 48 | |
| 49 | while i < len(tokens): |
| 50 | token = tokens[i] |
| 51 | |
| 52 | # Try to parse as number |
| 53 | try: |
| 54 | num = int(token) |
| 55 | parsed_tokens.append(num) |
| 56 | i += 1 |
| 57 | continue |
| 58 | except ValueError: |
| 59 | pass |
| 60 | |
| 61 | # Handle operations |
| 62 | if token in operations: |
| 63 | # Edge Case: "multiplied" and "divided" need "by" after them |
| 64 | if token in ["multiplied", "divided"]: |
| 65 | if i + 1 >= len(tokens) or tokens[i + 1] != "by": |
| 66 | raise ValueError("syntax error") |
| 67 | parsed_tokens.append(operations[token]) |
| 68 | i += 2 # Skip the operation and "by" |
| 69 | else: |
| 70 | parsed_tokens.append(operations[token]) |
| 71 | i += 1 |
| 72 | # Handle "by" when not preceded by "multiplied" or "divided" |
| 73 | elif token == "by": |
| 74 | # Edge Case: "by" without preceding "multiplied" or "divided" |
| 75 | raise ValueError("syntax error") |
| 76 | else: |
| 77 | # Edge Case: Unknown operation |
| 78 | raise ValueError("unknown operation") |
| 79 | |
| 80 | # Edge Case: Expression with no numbers |
| 81 | if not parsed_tokens: |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Edge Case: Expression starting with an operation |
| 85 | if isinstance(parsed_tokens[0], str): |
| 86 | raise ValueError("syntax error") |
| 87 | |
| 88 | # Edge Case: Expression ending with an operation |
| 89 | if isinstance(parsed_tokens[-1], str): |
| 90 | raise ValueError("syntax error") |
| 91 | |
| 92 | # Validate alternating pattern (number, operation, number, operation, ...) |
| 93 | for j in range(len(parsed_tokens)): |
| 94 | if j % 2 == 0: # Even indices should be numbers |
| 95 | if not isinstance(parsed_tokens[j], int): |
| 96 | raise ValueError("syntax error") |
| 97 | else: # Odd indices should be operations |
| 98 | if not isinstance(parsed_tokens[j], str): |
| 99 | raise ValueError("syntax error") |
| 100 | |
| 101 | # Evaluate from left to right |
| 102 | result = parsed_tokens[0] |
| 103 | |
| 104 | # Edge Case: Single number |
| 105 | if len(parsed_tokens) == 1: |
| 106 | return result |
| 107 | |
| 108 | # Process operations |
| 109 | for i in range(1, len(parsed_tokens), 2): |
| 110 | operation = parsed_tokens[i] |
| 111 | next_number = parsed_tokens[i + 1] |
| 112 | |
| 113 | if operation == "+": |
| 114 | result += next_number |
| 115 | elif operation == "-": |
| 116 | result -= next_number |
| 117 | elif operation == "*": |
| 118 | result *= next_number |
| 119 | elif operation == "/": |
| 120 | # Edge Case: Division by zero |
| 121 | if next_number == 0: |
| 122 | raise ValueError("syntax error") |
| 123 | result //= next_number # Integer division as per examples |
| 124 | |
| 125 | return result |
| 126 | # Handled Edge Cases: Empty input, None input, missing "What is" prefix, missing "?" suffix, |
| 127 | # empty expression, no tokens, unknown operations, "by" without "multiplied/divided", |
| 128 | # expression starting with operation, expression ending with operation, |
| 129 | # invalid alternating pattern, single number, division by zero |