| 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 | An integer result of the calculation. |
| 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"): |
| 17 | raise ValueError("unknown operation") |
| 18 | |
| 19 | # Remove the "What is" prefix and the trailing question mark |
| 20 | expression = question[8:-1].strip() |
| 21 | |
| 22 | # Edge Case: Empty expression after "What is" |
| 23 | if not expression: |
| 24 | raise ValueError("syntax error") |
| 25 | |
| 26 | # Split the expression into tokens |
| 27 | tokens = expression.split() |
| 28 | |
| 29 | # Edge Case: Invalid syntax with operation at the end |
| 30 | if tokens[-1] in ["plus", "minus", "multiplied", "divided"]: |
| 31 | raise ValueError("syntax error") |
| 32 | |
| 33 | # Convert tokens to a list of numbers and operations |
| 34 | parsed_tokens = [] |
| 35 | i = 0 |
| 36 | while i < len(tokens): |
| 37 | token = tokens[i] |
| 38 | |
| 39 | # Try to parse as number |
| 40 | try: |
| 41 | # Handle negative numbers |
| 42 | if token == "-": |
| 43 | # Edge Case: Negative number handling |
| 44 | if i + 1 < len(tokens): |
| 45 | next_token = tokens[i+1] |
| 46 | try: |
| 47 | num = int(next_token) |
| 48 | parsed_tokens.append(-num) |
| 49 | i += 2 |
| 50 | continue |
| 51 | except ValueError: |
| 52 | pass |
| 53 | raise ValueError("syntax error") |
| 54 | else: |
| 55 | num = int(token) |
| 56 | parsed_tokens.append(num) |
| 57 | i += 1 |
| 58 | continue |
| 59 | except ValueError: |
| 60 | # Not a number, check for operations |
| 61 | if token == "plus": |
| 62 | parsed_tokens.append("+") |
| 63 | elif token == "minus": |
| 64 | parsed_tokens.append("-") |
| 65 | elif token == "multiplied" and i + 1 < len(tokens) and tokens[i+1] == "by": |
| 66 | parsed_tokens.append("*") |
| 67 | i += 1 # Skip "by" |
| 68 | elif token == "divided" and i + 1 < len(tokens) and tokens[i+1] == "by": |
| 69 | parsed_tokens.append("/") |
| 70 | i += 1 # Skip "by" |
| 71 | else: |
| 72 | # Edge Case: Unsupported operations |
| 73 | raise ValueError("unknown operation") |
| 74 | i += 1 |
| 75 | |
| 76 | # Edge Case: Expression starting with an operation |
| 77 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[0], str): |
| 78 | raise ValueError("syntax error") |
| 79 | |
| 80 | # Edge Case: Expression ending with an operation |
| 81 | if len(parsed_tokens) > 0 and isinstance(parsed_tokens[-1], str): |
| 82 | raise ValueError("syntax error") |
| 83 | |
| 84 | # Edge Case: Invalid syntax with consecutive operations or numbers |
| 85 | for i in range(len(parsed_tokens) - 1): |
| 86 | current = parsed_tokens[i] |
| 87 | next_item = parsed_tokens[i+1] |
| 88 | |
| 89 | # Check for consecutive operations |
| 90 | if isinstance(current, str) and isinstance(next_item, str): |
| 91 | raise ValueError("syntax error") |
| 92 | |
| 93 | # Check for consecutive numbers |
| 94 | if isinstance(current, int) and isinstance(next_item, int): |
| 95 | raise ValueError("syntax error") |
| 96 | |
| 97 | # Edge Case: Single number |
| 98 | if len(parsed_tokens) == 1 and isinstance(parsed_tokens[0], int): |
| 99 | return parsed_tokens[0] |
| 100 | |
| 101 | # Edge Case: Invalid number of tokens for operations |
| 102 | if len(parsed_tokens) < 3 or len(parsed_tokens) % 2 == 0: |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | # Evaluate from left to right |
| 106 | result = parsed_tokens[0] |
| 107 | i = 1 |
| 108 | while i < len(parsed_tokens): |
| 109 | operation = parsed_tokens[i] |
| 110 | operand = parsed_tokens[i+1] |
| 111 | |
| 112 | if operation == "+": |
| 113 | result += operand |
| 114 | elif operation == "-": |
| 115 | result -= operand |
| 116 | elif operation == "*": |
| 117 | result *= operand |
| 118 | elif operation == "/": |
| 119 | # Edge Case: Division by zero |
| 120 | if operand == 0: |
| 121 | raise ValueError("syntax error") # Or we could raise ZeroDivisionError |
| 122 | result //= operand # Integer division as per examples |
| 123 | else: |
| 124 | # This shouldn't happen with our parsing, but just in case |
| 125 | raise ValueError("unknown operation") |
| 126 | |
| 127 | i += 2 |
| 128 | |
| 129 | return result |
| 130 | # Handled Edge Cases: Non-math questions, empty expression, negative number handling, |
| 131 | # unsupported operations, expression starting/ending with operation, |
| 132 | # consecutive operations/numbers, single number, invalid token count, |
| 133 | # division by zero |