| 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, |
| 13 | or is not a valid math word problem |
| 14 | """ |
| 15 | # Edge Case: Empty or None input |
| 16 | if not question: |
| 17 | raise ValueError("syntax error") |
| 18 | |
| 19 | # Edge Case: Question doesn't start with "What is" |
| 20 | if not question.startswith("What is"): |
| 21 | raise ValueError("syntax error") |
| 22 | |
| 23 | # Check for unknown operations that should take precedence over syntax errors |
| 24 | # This handles overlapping exception cases |
| 25 | if "cubed" in question: |
| 26 | raise ValueError("unknown operation") |
| 27 | |
| 28 | # Remove "What is" and "?" from the question |
| 29 | expression = question[8:-1].strip() |
| 30 | |
| 31 | # Edge Case: Empty expression after removing "What is" and "?" |
| 32 | if not expression: |
| 33 | raise ValueError("syntax error") |
| 34 | |
| 35 | # Split the expression into tokens |
| 36 | tokens = expression.split() |
| 37 | |
| 38 | # Edge Case: No tokens in expression |
| 39 | if not tokens: |
| 40 | raise ValueError("syntax error") |
| 41 | |
| 42 | # Parse the first number |
| 43 | try: |
| 44 | result = int(tokens[0]) |
| 45 | tokens = tokens[1:] |
| 46 | except ValueError: |
| 47 | # Edge Case: First token is not a number |
| 48 | raise ValueError("syntax error") |
| 49 | |
| 50 | # Process operations |
| 51 | while tokens: |
| 52 | # Get operation |
| 53 | operation = tokens[0] |
| 54 | |
| 55 | # Handle different operations |
| 56 | if operation == "plus": |
| 57 | # Need at least one more token for the operand |
| 58 | if len(tokens) < 2: |
| 59 | raise ValueError("syntax error") |
| 60 | |
| 61 | try: |
| 62 | operand = int(tokens[1]) |
| 63 | except ValueError: |
| 64 | raise ValueError("syntax error") |
| 65 | |
| 66 | result += operand |
| 67 | tokens = tokens[2:] |
| 68 | |
| 69 | elif operation == "minus": |
| 70 | # Need at least one more token for the operand |
| 71 | if len(tokens) < 2: |
| 72 | raise ValueError("syntax error") |
| 73 | |
| 74 | try: |
| 75 | operand = int(tokens[1]) |
| 76 | except ValueError: |
| 77 | raise ValueError("syntax error") |
| 78 | |
| 79 | result -= operand |
| 80 | tokens = tokens[2:] |
| 81 | |
| 82 | elif operation == "multiplied": |
| 83 | # Need at least two more tokens: "by" and operand |
| 84 | if len(tokens) < 3 or tokens[1] != "by": |
| 85 | raise ValueError("syntax error") |
| 86 | |
| 87 | try: |
| 88 | operand = int(tokens[2]) |
| 89 | except ValueError: |
| 90 | raise ValueError("syntax error") |
| 91 | |
| 92 | result *= operand |
| 93 | tokens = tokens[3:] |
| 94 | |
| 95 | elif operation == "divided": |
| 96 | # Need at least two more tokens: "by" and operand |
| 97 | if len(tokens) < 3 or tokens[1] != "by": |
| 98 | raise ValueError("syntax error") |
| 99 | |
| 100 | try: |
| 101 | operand = int(tokens[2]) |
| 102 | except ValueError: |
| 103 | raise ValueError("syntax error") |
| 104 | |
| 105 | if operand == 0: |
| 106 | raise ValueError("syntax error") |
| 107 | |
| 108 | result //= operand |
| 109 | tokens = tokens[3:] |
| 110 | |
| 111 | else: |
| 112 | # Check if this looks like it should be an operation but isn't recognized |
| 113 | # If it's a number where an operation should be, that's a syntax error |
| 114 | # If it's a word that could be an operation, that's an unknown operation |
| 115 | if operation.lstrip('-').isdigit(): |
| 116 | # It's a number where an operation should be - syntax error |
| 117 | raise ValueError("syntax error") |
| 118 | else: |
| 119 | # It's not a recognized operation - unknown operation |
| 120 | raise ValueError("unknown operation") |
| 121 | |
| 122 | return result |
| 123 | # Handled Edge Cases: Empty input, None input, question not starting with "What is", |
| 124 | # empty expression, no tokens, first token not a number, odd number of remaining tokens, |
| 125 | # not enough tokens for operation, operand not a number, division by zero, |
| 126 | # incomplete "multiplied" operation, incomplete "divided" operation, unknown operation |