Uh oh!
There was an error while loading. Please reload this page.
[Wordy] Clean up Approaches - #4198
Conversation
also some formatting fixes
This comment was marked as resolved.
This comment was marked as resolved.
Briefly browsing through the rest of the approaches, it seems like my solution uses yet another approach. Maybe I'll add an approach doc for it after I finish auditing all of the others. |
Uh oh!
There was an error while loading. Please reload this page.
BethanyG
commented
May 21, 2026
Thought I'd start with a read-through. Looking good so far. PSA: Don't forget to add yourself as a contributor for any approach that you fix/edit. 😄 I'll come back and do another once you're ready. |
That will probably be sometime tomorrow, considering that it's getting pretty late in my timezone. |
BethanyG
commented
May 21, 2026
awesome sauce! I love it. The more approaches, the better. I find that a wide variety teaches a TON of things. It can become an obsession. 😉 |
Yrahcaz7
commented
May 21, 2026
Audit part 2
|
also more formatting fixes
Yrahcaz7
commented
May 21, 2026
Audit part 3
|
Yrahcaz7
commented
May 21, 2026
Audit part 4
|
Yrahcaz7
commented
May 21, 2026
Audit part 5
|
BethanyG
commented
May 21, 2026
This one? When I put it into the online editor, it was complaining about an indentation error. I went back over it, and the version below now passes all the tests. importrefromoperatorimportadd, mul, subfromoperatorimportfloordivasdivDIGITS=re.compile(r"-?\d+")
OPERATORS= (
(mul, re.compile(r"(?P<x>.*) multiplied by (?P<y>.*)")),
(div, re.compile(r"(?P<x>.*) divided by (?P<y>.*)")),
(add, re.compile(r"(?P<x>.*) plus (?P<y>.*)")),
(sub, re.compile(r"(?P<x>.*) minus (?P<y>.*)")),
)
defanswer(question):
ifnotquestion.startswith( "What is") or"cubed"inquestion:
raiseValueError("unknown operation")
question=question.removeprefix( "What is").removesuffix("?").strip()
ifnotquestion:
raiseValueError("syntax error")
returncalculate(question)
defcalculate(question):
ifDIGITS.fullmatch(question):
returnint(question)
foroperation, patterninOPERATORS:
ifmatch:=pattern.match(question): #<-- The walrus operator assigns the new re match to `match` each loop.returnoperation(calculate(match['x']), calculate(match['y']))#<-- the loop is paused here to make the two recursive calls.raiseValueError("syntax error")Careful of that Also note that this uses a walrus operator, which means the value of If it is calculating in the wrong order, at least a few of the tests should have failed. But this is also a very complex solution, since it has two recursive branches in a loop. IIRC, when I was going over this, I had to put it into python tutor and walk it to make sure I really understood the recursive calls in the loop. This actually came from Isaac. You can see his solution variations here. The recursion in a loop variants are the first three, I think. |
BethanyG
commented
May 21, 2026
Yup. Definitely cleaner that way (I should listen to PyLint more often). I need to update my published solution! 😆 fromoperatorimportadd, mul, subfromoperatorimportfloordivasdiv# Define a lookup table for mathematical operationsOPERATIONS= {"plus": add, "minus": sub, "multiplied": mul, "divided": div}
defanswer(question):
# Call clean() and feed it to calculate()returncalculate(clean(question))
defclean(question):
# It's not a question unless it starts with 'What is'.ifnotquestion.startswith("What is") or"cubed"inquestion:
raiseValueError("unknown operation")
# Remove the unnecessary parts of the question and# parse the cleaned question into a list of items to process.# The wrapping () invoke implicit concatenation for the chained functionsreturn (question.removeprefix("What is")
.removesuffix("?")
.replace("by", "")
.strip()).split() # <-- this split() turns the string into a list.# Recursively calculate the first piece of the equation, calling # calculate() on the product + the remainder.# Return the solution when len(equation) is one.defcalculate(equation):
iflen(equation) ==1:
returnint(equation[0])
try:
# Unpack the equation into first int, operator, and second int.# Stuff the remainder into *restx_value, operation, y_value, *rest=equation# Redefine the equation list as the product of the first three# variables concatenated with the unpacked remainder.equation= [OPERATIONS[operation](int(x_value),
int(y_value)), *rest]
except:
raiseValueError("syntax error")
# Call calculate() with the redefined/partially reduced equation.returncalculate(equation) |
I see. I didn't know about soft keywords.
Turns out there actually isn't a test for the case that I'm thinking of! There are no questions in the test suite with at least three operations/four numbers in them. This pythontutor link should show the case I'm talking about. You can see that since it parses the expression in the wrong order, it returns |
BethanyG
commented
May 21, 2026
This is an excellent catch!!. Methinks we should add some additional track-specific test cases for this exercise. NICE WORK!!! Here are my current thoughts:
Make sense? 😄 |
Yrahcaz7
commented
May 21, 2026
I think so! Number 5 would be in a separate PR, right? |
Yuppers. A tad more involved than it looks, since we need to also regenerate the test file, may or may not need to adjust the test template (likely not), and do some re-tests on the example. And 6 would be an entirely different repo, Problem Specs with likely a long discussion among maintainers on the forum first. For that, you'll want some illustrative examples from both Python and other languages. |
BethanyG
commented
May 21, 2026
Let's get the major changes here merged, and then I can walk you through the test generation/template/additional tests thing. If that's OK? |
Yrahcaz7
commented
May 21, 2026
Sounds good! |
The last two approaches basically just had the same kind of errors that were present in the previous ones. Though the very last one was also too compact, so I expanded it so it would be readable. |
@BethanyG, that's numbers 1 and 2 done; now it should be time for 3 and 4. I'm not quite sure how to go about doing these, however. Especially number 3... That variant is super complicated. |
BethanyG
commented
May 21, 2026
Oh! I am so sorry! Somehow, past a certain point, comments don't seem to be triggering new notifications for me. I was head's down on fixing classes (spoiler: it might go the way of that other one), and didn't get pinged. Give me a few, and let me see if I can take a crack. 😄 |
BethanyG
commented
May 21, 2026
As for 4, are you testing locally with PyTest, or using the website? If you are testing locally, we can do a special hand-edit to the test file and you can then use that for the other approaches. If not...well, time to learn how to set up a local testing env with PyTest. 😉 Ping me on the forum or Discord, and I can walk you through. I don't trust GH at the moment. 😡 |
BethanyG
commented
May 21, 2026
Today, children, we are going to learn about positive lookaheads in regex 🤦🏽♀️ . I mean, why not? I don't really need those neurons, do I? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
BethanyG
commented
May 22, 2026
Big, green button or a cooling-off period? |
As discussed in #4055 and #4197.