From 63cc10b06f425edb3a85c404d15d360c333bde0b Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 14 Oct 2024 16:29:08 -0700 Subject: [PATCH] Further refinements to approaches. Added hits.md file. (#3788) --- .../.approaches/functools-reduce/content.md | 2 +- .../wordy/.approaches/introduction.md | 89 +++++---- .../string-list-and-dict-methods/content.md | 183 ++++++++++++------ .../wordy/.docs/instructions.append.md | 16 +- 4 files changed, 191 insertions(+), 99 deletions(-) diff --git a/exercises/practice/wordy/.approaches/functools-reduce/content.md b/exercises/practice/wordy/.approaches/functools-reduce/content.md index 0a2532d442..602a678ffd 100644 --- a/exercises/practice/wordy/.approaches/functools-reduce/content.md +++ b/exercises/practice/wordy/.approaches/functools-reduce/content.md @@ -33,7 +33,7 @@ def answer(question): raise ValueError("syntax error") # Evaluate the expression from left to right using functools.reduce(). - # Look up each operation in the operation dictionary. + # Look up each operation in the OPERATORS dictionary. return reduce(lambda x, y: OPERATORS[operations.pop(0)](x, y), digits) ``` diff --git a/exercises/practice/wordy/.approaches/introduction.md b/exercises/practice/wordy/.approaches/introduction.md index 0ea7592704..6f23f040ba 100644 --- a/exercises/practice/wordy/.approaches/introduction.md +++ b/exercises/practice/wordy/.approaches/introduction.md @@ -8,17 +8,39 @@ This means that for some of the test cases, the solution will not be the same as ## General Guidance The key to a Wordy solution is to remove the "question" portion of the sentence (_"What is", "?"_) and process the remaining words between numbers as [operators][mathematical operators]. -If a single number remains after removing the "question", it should be converted to an [`int`][int] and returned as the answer. + + +If a single number remains after removing the "question" pieces, it should be converted to an [`int`][int] and returned as the answer. + + Any words or word-number combinations that do not fall into the simple mathematical evaluation pattern (_number-operator-number_) should [`raise`][raise-statement] a [`ValueError`][value-error] with a message. This includes any "extra" spaces between numbers. + One way to reduce the number of `raise` statements/ `ValueError`s needed in the code is to determine if a problem is a "valid" question _before_ proceeding to parsing and calculation. As shown in various approaches, there are multiple strategies for validating questions, with no one "canonical" solution. -One very effective approach is to check if a question starts with "What is", ends with "?", and includes only valid operations. -That could lead to future maintenance issues if the definition of a question ever changes or operations are added, but for the purposes of passing the current Wordy tests, it works well. -There are various Pythonic ways to go about the cleaning, parsing, and calculation steps of Wordy. -For cleaning the "question" portion of the problem, [`str.removeprefix`][removeprefix] and + +One very effective validation approach is to check if a question starts with "What is", ends with "?", and does not include the word "cubed". +Any other question formulation becomes a `ValueError("unknown operation")`. +This very restrictive approach could lead to future maintenance issues if the definition of a question ever changes or operations are added, but for the purposes of passing the current Wordy tests, it works well. + + +Proceeding from validation, there are many Pythonic ways to go about the cleaning, parsing, and calculation steps of Wordy. +However, they all follow these general steps: + +1. Remove the parts of the question string that do not apply to calculating the answer. +2. Iterate over the question, determining which words are numbers, and which are meant to be mathematical operations. + - _Converting the question string into a `list` of words is hugely helpful here, but not absolutely necessary._ +3. **_Starting from the left_**, take the first three elements and convert number strings to `int` and operations words to +, -, *, /. +4. Apply the operation to the numbers, which should result in a single number. + - _Employing a `try-except` block around the conversion and operator application steps can trap any errors thrown and make the code both "safer" and less complex._ +5. Use the calculated number from step 4 as the start for the next "trio" (_number, operation, number_) in the question. The calculated number + the remainder of the question becomes the question being worked on in the next iteration. + - _Using a `while-loop` with a test on the length of the question to do calculation is a very common strategy._ +6. Once the question is calculated down to a single number, that is the answer. Anything else that happens in the loop/iteration or within the accumulated result is a `ValueError("syntax error")`. + + +For cleaning the question, [`str.removeprefix`][removeprefix] and [`str.removesuffix`][removesuffix] introduced in `Python 3.9` can be very useful: @@ -53,73 +75,70 @@ You can also use [`str.startswith`][startswith] and [`str.endswith`][endswith] i ``` -Different combinations of [`str.find`][find], [`str.rfind`][rfind], or [`str.index`][index] with string slicing could be used to clean up the initial word problem. -A [regex][regex] could also be used to process the question, but might be considered overkill given the fixed nature of the prefix/suffix and operations. +Different combinations of [`str.find`][find], [`str.rfind`][rfind], or [`str.index`][index] with string slicing could also be used to clean up the initial question. +A [regex][regex] could be used to process the question as well, but might be considered overkill given the fixed nature of the prefix/suffix and operations. Finally, [`str.strip`][strip] and its variants are very useful for cleaning up any leftover leading or trailing whitespace. -Many solutions then use [`str.split`][split] to process the remaining "cleaned" question into a `list` for convenient iteration, although other strategies are also used. +Many solutions then use [`str.split`][split] to process the remaining "cleaned" question into a `list` for convenient looping/iteration, although other strategies can also be used. + For math operations, many solutions involve importing and using methods from the [operator][operator] module in combination with different looping, parsing, and substitution strategies. -Some solutions use either [lambda][lambdas] expressions or [dunder/"special" methods][dunder-methods] to replace words with arithmetic operations. -However, the exercise can be solved without using `operator`, `lambdas`, or `dunder-methods`. +Some solutions use either [lambda][lambdas] expressions, [dunder/"special" methods][dunder-methods], or even `eval()` to replace words with arithmetic operations. +However, the exercise can be solved **without** using `operator`, `lambdas`, `dunder-methods` or `eval`. + It is recommended that you first start by solving it _without_ "advanced" strategies, and then refine your solution into something more compact or complex as you learn and practice. + +~~~~exercism/caution Using [`eval`][eval] for the operations might seem convenient, but it is a [dangerous][eval-danger] and possibly [destructive][eval-destructive] approach. It is also entirely unnecessary, as the other methods described here are safer and equally performant. +~~~~ ## Approach: String, List, and Dictionary Methods ```python -OPERATIONS = {"plus": '+', "minus": '-', "multiplied": '*', "divided": '/'} - - def answer(question): if not question.startswith("What is") or "cubed" in question: raise ValueError("unknown operation") - question = question.removeprefix("What is").removesuffix("?").strip() + question = question.removeprefix("What is") + question = question.removesuffix("?") + question = question.replace("by", "") + question = question.strip() if not question: raise ValueError("syntax error") - - if question.isdigit(): - return int(question) - - formula = [] - for operation in question.split(): - if operation == 'by': - continue - else: - formula.append(OPERATIONS.get(operation, operation)) + formula = question.split() while len(formula) > 1: try: x_value = int(formula[0]) - symbol = formula[1] y_value = int(formula[2]) + symbol = formula[1] remainder = formula[3:] - if symbol == "+": + if symbol == "plus": formula = [x_value + y_value] + remainder - elif symbol == "-": + elif symbol == "minus": formula = [x_value - y_value] + remainder - elif symbol == "*": + elif symbol == "multiplied": formula = [x_value * y_value] + remainder - elif symbol == "/": + elif symbol == "divided": formula = [x_value / y_value] + remainder else: raise ValueError("syntax error") except: raise ValueError("syntax error") - return formula[0] + return int(formula[0]) ``` -This approach uses only data structures and methods (_[dict][dict], [dict.get()][dict-get] and [list()][list]_) from core Python, and does not import any extra modules. +This approach uses only data structures and methods (_[str methods][str-methods], [list()][list], loops, etc._) from core Python, and does not import any extra modules. It may have more lines of code than average, but it is clear to follow and fairly straightforward to reason about. It does use a [try-except][handling-exceptions] block for handling unknown operators. -As an alternative to the `formula` loop-append, a [list-comprehension][list-comprehension] can be used to create the initial parsed formula. + +Alternatives could use a [dictionary][dict] to store word --> operator mappings that could be looked up in the `while-loop` using [`.get()`][dict-get], among other strategies. For more details and variations, read the [String, List and Dictionary Methods][approach-string-list-and-dict-methods] approach. @@ -350,7 +369,7 @@ def answer(question): ``` -This approach replaces the `while-loop` used in many solutions (_or the `recursion` strategy outlined in the approach above_) with a call to [`functools.reduce`][functools-reduce]. +This approach replaces the `while-loop` used in many solutions (_or the `recursion` strategy outlined in the approach above_) with a call to [`functools.reduce`][functools-reduce]. It also employs a lookup dictionary for methods imported from the `operator` module, as well as a `list-comprehension`, the built-in [`filter`][filter] function, and multiple string [slices][sequence-operations]. If desired, the `operator` imports can be replaced with a dictionary of `lambda` expressions or `dunder-methods`. @@ -418,9 +437,6 @@ For more detail on this solution, take a look at the [dunder method with `__geta [dict]: https://docs.python.org/3/library/stdtypes.html#dict [dunder-methods]: https://www.pythonmorsels.com/what-are-dunder-methods/?watch [endswith]: https://docs.python.org/3.9/library/stdtypes.html#str.endswith -[eval-danger]: https://softwareengineering.stackexchange.com/questions/311507/why-are-eval-like-features-considered-evil-in-contrast-to-other-possibly-harmfu -[eval-destructive]: https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html -[eval]: https://docs.python.org/3/library/functions.html?#eval [filter]: https://docs.python.org/3/library/functions.html#filter [find]: https://docs.python.org/3.9/library/stdtypes.html#str.find [functools-reduce]: https://docs.python.org/3/library/functools.html#functools.reduce @@ -444,4 +460,5 @@ For more detail on this solution, take a look at the [dunder method with `__geta [split]: https://docs.python.org/3.9/library/stdtypes.html#str.split [startswith]: https://docs.python.org/3.9/library/stdtypes.html#str.startswith [strip]: https://docs.python.org/3.9/library/stdtypes.html#str.strip +[str-methods]: https://docs.python.org/3/library/stdtypes.html#string-methods [value-error]: https://docs.python.org/3.11/library/exceptions.html#ValueError diff --git a/exercises/practice/wordy/.approaches/string-list-and-dict-methods/content.md b/exercises/practice/wordy/.approaches/string-list-and-dict-methods/content.md index 3c3dcfe8ee..cce88a4bb0 100644 --- a/exercises/practice/wordy/.approaches/string-list-and-dict-methods/content.md +++ b/exercises/practice/wordy/.approaches/string-list-and-dict-methods/content.md @@ -1,6 +1,73 @@ # String, List, and Dictionary Methods +```python +def answer(question): + if not question.startswith("What is") or "cubed" in question: + raise ValueError("unknown operation") + + question = question.removeprefix("What is") + question = question.removesuffix("?") + question = question.replace("by", "") + question = question.strip() + + if not question: + raise ValueError("syntax error") + + formula = question.split() + while len(formula) > 1: + try: + x_value = int(formula[0]) + y_value = int(formula[2]) + symbol = formula[1] + remainder = formula[3:] + + if symbol == "plus": + formula = [x_value + y_value] + remainder + elif symbol == "minus": + formula = [x_value - y_value] + remainder + elif symbol == "multiplied": + formula = [x_value * y_value] + remainder + elif symbol == "divided": + formula = [x_value / y_value] + remainder + else: + raise ValueError("syntax error") + except: + raise ValueError("syntax error") + + return int(formula[0]) +``` + +Within the `answer()` function, the question is first checked for "unknown operations" by validating that it starts with "What is" ([`str.startswith`][startswith], [`str.endswith`][endswith]) and does not include the word "cubed" (_which is an invalid operation_). +This eliminates all the [current cases][unknown-operation-tests] where a [`ValueError("unknown operation")`][value-error] needs to be [raised][raise-statement]. +Should the definition of a question expand or change, this strategy would need to be revised. + + +The question is then "cleaned" by removing the prefix "What is" and the suffix "?" ([`str.removeprefix`][removeprefix], [`str.removesuffix`][removesuffix]), replacing "by" with "" ([`str.replace`][str-replace]), and [stripping][strip] any leading or trailing whitespaces. + + +If the question is now an empty string, a `ValueError("syntax error")` is raised. + + +The remaining question string is then converted into a `list` of elements via [`str.split`][split], and that `list` is iterated over using a `while-loop` with a `len()` > 1 condition. + +Within a [`try-except`][handling-exceptions] block to trap/handle any errors (_which will all map to `ValueError("syntax error")`_), the question `list` is divided up among 4 variables using [bracket notation][bracket-notation]: + +1. The first element, `x_value`. This is assumed to be a number, so it is converted to an `int()` +2. The third element, `y_value`. This is also assumed to be a number and converted to an `int()`. +3. The second element, `symbol`. This is assumed to be an operator, and is left as-is. +4. The `remainder` of the question, if there is any. This is a [slice][list-slice] starting at index 3, and going to the end. + + +`symbol` is then tested for "plus, minus, multiplied, or divided", and the `formula` list is modified by applying the given operation, and creating a new `formula` `list` by concatenating a `list` of the first product with the `remainder` list. +If `symbol` doesn't match any known operators, a `ValueError("syntax error")` is raised. + +Once `len(formula) == 1`, the first element (`formula[0]`) is converted to an `int()` and returned as the answer. + + +## Variation 1: Use a Dictionary for Lookup/Replace + + ```python OPERATIONS = {"plus": '+', "minus": '-', "multiplied": '*', "divided": '/'} @@ -9,21 +76,19 @@ def answer(question): if not question.startswith("What is") or "cubed" in question: raise ValueError("unknown operation") - question = question.removeprefix("What is").removesuffix("?").strip() + question = question.removeprefix("What is").removesuffix("?").replace("by", "").strip() if not question: raise ValueError("syntax error") formula = [] for operation in question.split(): - if operation == 'by': - continue - else: - formula.append(OPERATIONS.get(operation, operation)) + formula.append(OPERATIONS.get(operation, operation)) while len(formula) > 1: try: - x_value, y_value = int(formula[0]), int(formula[2]) + x_value = int(formula[0]) + y_value = int(formula[2]) symbol = formula[1] remainder = formula[3:] @@ -43,31 +108,31 @@ def answer(question): return int(formula[0]) ``` -Within the `answer()` function, the question is first checked for "unknown operations" by validating that it starts with "What is" ([`str.startswith`][startswith], [`str.endswith`][endswith]) and does not include the word "cubed" (_which is an invalid operation_). -This eliminates all the [current cases][unknown-operation-tests] where a [`ValueError("unknown operation")`][value-error] needs to be [raised][raise-statement]. -Should the definition of a question expand or change, this strategy would need to be revised. - -The question is then "cleaned" by removing the prefix "What is" and the suffix "?" ([`str.removeprefix`][removeprefix], [`str.removesuffix`][removesuffix]) and [stripping][strip] any leading or trailing whitespaces. +````exercism/note +[chaining][method-chaining] is used in the clean step for this variation, and is the equivalent of assigning and re-assigning `question` as is done in the initial approach. + This is because `str.startswith`, `str.endswith`, and `str.replace` all return strings, so the output of one can be used as the input to the next. + + [method-chaining]: https://www.tutorialspoint.com/Explain-Python-class-method-chaining +```` -If the question is now an empty string, a `ValueError("syntax error")` is raised. +This variation creates a dictionary to map operation words to symbols. +It pre-processes the question string into a `formula` list by looking up the operation words and replacing them with the symbols via the [`.get`][dict-get] method, which takes a [default argument][default-argument] for when a [`KeyError`][keyerror] is thrown. +Here the default for `dict.get()` is set to the element being iterated over, which is effectively _"if not found, skip it"_. +This means the number strings will be passed through, even though they would otherwise toss an error. + The results of iterating through the question are appended to `formula` via [`list.append`][list-append]. -Next, the question is [split][split] into a `list` and iterated over, with each element looked up and replaced from the OPERATIONS dictionary. -The [`dict.get`][dict-get] method is used for this, as it takes a default argument for when a [`KeyError`][keyerror] is thrown. -Here the default for `dict.get` is set to the element being iterated over, which is effectively _"if not found, skip it"_. - This avoids error handling, extra logic, or interruption when an element is not found. - One exception here is the word "by", which is explicitly skipped within the `for-loop`, so that it doesn't appear in the formula to be processed. - This filtering out could also be accomplished by using [`str.replace`][str-replace] in the cleaning step or during the `split` step. - The results of iterating through the question are then appended to a new formula `list`. +This dictionary is not necessary, but does potentially make adding/tracking future operations easier, although the `if-elif-else` block in the `while-loop` is equally awkward for maintenance (_see the [import callables from operator][approach-import-callables-from-operator] for a way to replace the block_). +The `while-loop`, `if-elif-else` block, and the `try-except` block are then the same as in the initial approach. ````exercism/note -There are a couple of common alternatives to the `loop-append`: +There are a couple of common alternatives to the `loop-append` used here: -1. [`list-comprehensions`][list-comprehension] duplicate the same process in a more succinct and declarative fashion: +1. [`list-comprehensions`][list-comprehension] duplicate the same process in a more succinct and declarative fashion. This one also includes filtering out "by": ```python formula = [OPERATIONS.get(operation, operation) for @@ -88,67 +153,68 @@ There are a couple of common alternatives to the `loop-append`: [map]: https://docs.python.org/3/library/functions.html#map ```` + Rather than indexing and slicing, [concept: unpacking and multiple assignment](/tracks/python/concepts/unpacking-and-multiple-assignment) can be used to assign the variables. + However, this does require a modification to the returned formula `list`: -After the formula `list` is composed, it is processed in a `while-loop`. - -The processing within the `loop` is wrapped in a [try-except][handling-exceptions] block to trap any errors and raise them as `ValueError("syntax error")`. -While each type of error could be checked for individually, it is not necessary since only `ValueError("syntax error")` is required here. - -1. `x_value` and `y_value` are assigned to the first element and third element of the list using [bracket notation][bracket-notation], and converted to integers. - - Rather than indexing and slicing, [concept: unpacking and multiple assignment](/tracks/python/concepts/unpacking-and-multiple-assignment) can be used to assign the variables. - This does require a modification to the returned formula `list`: - ```python - x_value, operation, y_value, *remainder = formula # <-- Unpacking won't allow conversion to int() here. - - ... - if symbol == "+": - formula = [int(x_value) + int(y_value)] + remainder # <-- Instead, conversion to int() must happen here. - ... - - return int(formula[0]) - ``` -2. `symbol` is assigned to the second element of the list. -3. `remainder` is assigned to a [slice][list-slice] of everything else in the `list`. + ```python + x_value, operation, y_value, *remainder = formula # <-- Unpacking won't allow conversion to int() here. + + ... + if symbol == "+": + formula = [int(x_value) + int(y_value)] + remainder # <-- Instead, conversion to int() must happen here. + ... + + return int(formula[0]) + ``` -The `symbol` is then tested in the `if-elif-else` block and the formula `list` is modified by calculating the operation on `x_value` and `y_value` and then appending whatever part of the question remains. -Once the formula `list` is calculated down to a number, that number is converted to an `int` and returned as the answer. +## Variation 2: Structural Pattern Matching to Replace `if-elif-else` -````exercism/note -Introduced in Python 3.10, [structural pattern matching][structural-pattern-matching] can be used to replace the `if-elif-else` chain in the `while-loop`. +Introduced in Python 3.10, [structural pattern matching][structural-pattern-matching] can be used to replace the `if-elif-else` chain in the `while-loop` used in the two approaches above. In some circumstances, this could be easier to read and/or reason about: ```python +def answer(question): + if not question.startswith("What is") or "cubed" in question: + raise ValueError("unknown operation") + + question = question.removeprefix("What is").removesuffix("?").replace("by", "").strip() + + if not question: + raise ValueError("syntax error") + + formula = question.split() while len(formula) > 1: try: - x_value, symbol, y_value, *remainder = formula + x_value, symbol, y_value, *remainder = formula #<-- unpacking and multiple assignment. - match symbol: - case "+": + match symbol: + case "plus": formula = [int(x_value) + int(y_value)] + remainder - case "-": + case "minus": formula = [int(x_value) - int(y_value)] + remainder - case "*": + case "multiplied": formula = [int(x_value) * int(y_value)] + remainder - case "/": + case "divided": formula = [int(x_value) / int(y_value)] + remainder - case _: - raise ValueError("syntax error") - except: - raise ValueError("syntax error") + case _: + raise ValueError("syntax error") #<-- "fall through case for no match." + except: raise ValueError("syntax error") # <-- error handling for anything else that goes wrong. + + return int(formula[0]) ``` -[structural-pattern-matching]: https://peps.python.org/pep-0636/ -```` - +[approach-import-callables-from-operator]: https://exercism.org/tracks/python/exercises/wordy/approaches/import-callables-from-operator [bracket-notation]: https://docs.python.org/3/library/stdtypes.html#common-sequence-operations +[default-argument]: https://docs.python.org/3/tutorial/controlflow.html#default-argument-values [dict-get]: https://docs.python.org/3/library/stdtypes.html#dict.get [endswith]: https://docs.python.org/3.9/library/stdtypes.html#str.endswith [handling-exceptions]: https://docs.python.org/3.11/tutorial/errors.html#handling-exceptions [keyerror]: https://docs.python.org/3/library/exceptions.html#KeyError +[list-append]: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists [list-slice]: https://www.pythonmorsels.com/slicing/ [raise-statement]: https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement [removeprefix]: https://docs.python.org/3.9/library/stdtypes.html#str.removeprefix @@ -157,5 +223,6 @@ In some circumstances, this could be easier to read and/or reason about: [startswith]: https://docs.python.org/3.9/library/stdtypes.html#str.startswith [str-replace]: https://docs.python.org/3/library/stdtypes.html#str.replace [strip]: https://docs.python.org/3.9/library/stdtypes.html#str.strip +[structural-pattern-matching]: https://peps.python.org/pep-0636/ [unknown-operation-tests]: https://github.com/exercism/python/blob/main/exercises/practice/wordy/wordy_test.py#L58-L68 [value-error]: https://docs.python.org/3.11/library/exceptions.html#ValueError diff --git a/exercises/practice/wordy/.docs/instructions.append.md b/exercises/practice/wordy/.docs/instructions.append.md index ca646b7bd9..d26afab5ff 100644 --- a/exercises/practice/wordy/.docs/instructions.append.md +++ b/exercises/practice/wordy/.docs/instructions.append.md @@ -5,6 +5,8 @@ Sometimes it is necessary to [raise an exception][raise-an-exception]. When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types][built-in-errors], but should still include a meaningful message. This particular exercise requires that you use the [raise statement][raise-statement] to "throw" a `ValueError` if the question passed to `answer()` is malformed/invalid, or contains an unknown operation. The tests will only pass if you both `raise` the `exception` and include a message with it. +**Please note**: The message needed is different for each scenario, even though the same _error type_ is being raised. +Check the tests carefully. To raise a [`ValueError`][value-error] with a message, write the message as an argument to the `exception` type: @@ -17,16 +19,22 @@ raise ValueError("unknown operation") raise ValueError("syntax error") ``` -To _handle_ a raised error within a particular code block, one can use a [try-except][handling-exceptions] : +To _handle_ a raised error within a particular code block, one can use a [try-except][handling-exceptions]. + `try-except` blocks "wrap" the code that could potentially cause an error, mapping all the exceptions to one error, multiple errors, or other pieces of code to deal with the problem: + ```python while len(equation) > 1: - try: + try: + # The questionable/error-prone code goes here,in an indented block + # It can contain statements, loops, if-else blocks, or other executable code. x_value, operation, y_value, *rest = equation - ... - + ... + except: + # Code for what to do when an error gets thrown in the code above. + # This could be one error, or more complicated logging, error checking and messaging. raise ValueError("syntax error") ```