Skip to content

Commit

Permalink
what I hope is the final pass of the wordy typo monsters. (#3803)
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyG authored Oct 31, 2024
1 parent 7993df7 commit 694a421
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions exercises/practice/wordy/.approaches/functools-reduce/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ def answer(question):
raise ValueError("unknown operation")

# Using the built-in filter() to clean & split the question..
list(filter(lambda x:
x not in ("What", "is", "by"),
question.strip("?").split()))
question = list(filter(lambda x:
x not in ("What", "is", "by"),
question.strip("?").split()))

# Separate candidate operators and numbers into two lists.
operations = question[1::2]

# Convert candidate elements to int(), checking for "-".
# All other values are replaced with None.
digits = [int(element) if (element.isdigit() or element[1:].isdigit())
digits = [int(element) if
(element.isdigit() or element[1:].isdigit())
else None for element in question[::2]]

# If there is a mis-match between operators and numbers, toss error.
Expand Down Expand Up @@ -106,7 +107,8 @@ def answer(question):

# Convert candidate elements to int(), checking for "-".
# All other values are replaced with None.
digits = [int(element) if (element.isdigit() or element[1:].isdigit())
digits = [int(element) if
(element.isdigit() or element[1:].isdigit())
else None for element in question[::2]]

# If there is a mis-match between operators and numbers, toss error.
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/wordy/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ However, solutions all follow the same 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._
_Converting the question string into a `list` of words is hugely helpful here._
3. **_Starting from the left_**, take the first three elements and convert number strings to `int` and operations words to the mathematical operations +, -, *, and /.
4. Apply the operation to the numbers, which should result in a single number.
-- _Employing a `try-except` block can trap any errors thrown and make the code both "safer" and less complex._
_Employing a `try-except` block 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._
_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")`.
~~~~

Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/wordy/.approaches/recursion/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ For example:

1. "1 plus -10 multiplied by 13 divided by 2" would match on "1 plus -10" (_group x_) **multiplied by** "13 divided by 2" (_group y_).
2. This is re-arranged to `mul(calculate("1 plus -10"), calculate("13 divided by 2"))`
3. At this point the loop would pause as the two recursive calls to `calculate()` spawn
4. The loops then run again — and so would the calls to `calculate()`, until there wasn't any match that caused a split of the question or an error.
5. One at a time, the numbers would then be returned, until the main `mul(calculate("1 plus -10"), calculate("13 divided by 2"))` could be solved, at which point the answer is returned.
3. At this point, the loop pauses as the two recursive calls to `calculate()` spawn
4. The loop runs again — and so do the calls to `calculate()`until there isn't any match that splits the question or any errors.
5. One at a time, the numbers are returned from the `calculate()` calls on the stack, until the main `mul(calculate("1 plus -10"), calculate("13 divided by 2"))` is solved, at which point the answer is returned.

For a more visual picture, you can step through the code on [pythontutor.com][recursion-in-loop-pythontutor].

Expand Down

0 comments on commit 694a421

Please sign in to comment.