| 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 math question |
| 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 | # Remove "What is" and "?" from the question |
| 24 | expression = question[8:-1].strip() |
| 25 | |
| 26 | # Edge Case: Empty expression after removing "What is" and "?" |
| 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 | # Parse the first number |
| 38 | try: |
| 39 | result = int(tokens[0]) |
| 40 | tokens = tokens[1:] |
| 41 | except ValueError: |
| 42 | # Edge Case: First token is not a number |
| 43 | raise ValueError("syntax error") |
| 44 | |
| 45 | # Process operations |
| 46 | while tokens: |
| 47 | # Get operation |
| 48 | operation = tokens[0] |
| 49 | |
| 50 | # Handle different operation types |
| 51 | if operation == "plus": |
| 52 | # Edge Case: Not enough tokens for operand |
| 53 | if len(tokens) < 2: |
| 54 | raise ValueError("syntax error") |
| 55 | try: |
| 56 | next_number = int(tokens[1]) |
| 57 | except ValueError: |
| 58 | # If we can't parse the next token as a number, it might be an unknown operation |
| 59 | # But we should check if it's a valid operation first |
| 60 | if tokens[1] in ["plus", "minus", "multiplied", "divided"]: |
| 61 | raise ValueError("syntax error") |
| 62 | else: |
| 63 | raise ValueError("unknown operation") |
| 64 | result += next_number |
| 65 | tokens = tokens[2:] |
| 66 | elif operation == "minus": |
| 67 | # Edge Case: Not enough tokens for operand |
| 68 | if len(tokens) < 2: |
| 69 | raise ValueError("syntax error") |
| 70 | try: |
| 71 | next_number = int(tokens[1]) |
| 72 | except ValueError: |
| 73 | # If we can't parse the next token as a number, it might be an unknown operation |
| 74 | # But we should check if it's a valid operation first |
| 75 | if tokens[1] in ["plus", "minus", "multiplied", "divided"]: |
| 76 | raise ValueError("syntax error") |
| 77 | else: |
| 78 | raise ValueError("unknown operation") |
| 79 | result -= next_number |
| 80 | tokens = tokens[2:] |
| 81 | elif operation == "multiplied": |
| 82 | # Edge Case: Must have "by" after "multiplied" |
| 83 | if len(tokens) < 3 or tokens[1] != "by": |
| 84 | raise ValueError("syntax error") |
| 85 | try: |
| 86 | next_number = int(tokens[2]) |
| 87 | except ValueError: |
| 88 | # If we can't parse the next token as a number, it might be an unknown operation |
| 89 | # But we should check if it's a valid operation first |
| 90 | if len(tokens) > 2 and tokens[2] in ["plus", "minus", "multiplied", "divided"]: |
| 91 | raise ValueError("syntax error") |
| 92 | else: |
| 93 | raise ValueError("unknown operation") |
| 94 | result *= next_number |
| 95 | tokens = tokens[3:] |
| 96 | elif operation == "divided": |
| 97 | # Edge Case: Must have "by" after "divided" |
| 98 | if len(tokens) < 3 or tokens[1] != "by": |
| 99 | raise ValueError("syntax error") |
| 100 | try: |
| 101 | next_number = int(tokens[2]) |
| 102 | except ValueError: |
| 103 | # If we can't parse the next token as a number, it might be an unknown operation |
| 104 | # But we should check if it's a valid operation first |
| 105 | if len(tokens) > 2 and tokens[2] in ["plus", "minus", "multiplied", "divided"]: |
| 106 | raise ValueError("syntax error") |
| 107 | else: |
| 108 | raise ValueError("unknown operation") |
| 109 | # Edge Case: Division by zero |
| 110 | if next_number == 0: |
| 111 | raise ValueError("syntax error") |
| 112 | result //= next_number |
| 113 | tokens = tokens[3:] |
| 114 | else: |
| 115 | # Check if this is a number (syntax error) or unknown operation |
| 116 | if operation.lstrip('-').isdigit(): |
| 117 | # It's a number, so it's a syntax error (number with no operation) |
| 118 | raise ValueError("syntax error") |
| 119 | else: |
| 120 | # It's not a number, so it's an unknown operation |
| 121 | raise ValueError("unknown operation") |
| 122 | |
| 123 | # Edge Case: Check if there are any leftover tokens (syntax error) |
| 124 | if tokens: |
| 125 | # Any leftover tokens constitute a syntax error |
| 126 | raise ValueError("syntax error") |
| 127 | |
| 128 | return result |