specifically - Should I do this step before or after splitting problems-
“”“The appropriate operators the function will accept are addition and subtraction. Multiplication and division will return an error. Other operators not mentioned in this bullet point will not need to be tested. The error returned will be: Error: Operator must be ‘+’ or ‘-’.”“”
# defining User Data Format Requirements
# for problems that arithmetic_arranger() will accept
# max 5 problems can be passed to arithmetic_operator()
if len(problems) > 5:
return "Error: Too many problems."
# if not
# only + or - is accepted by arithmetic_operator()
# slicing list to access parts of problem
# to verify if operands contain only digits
# and no more than 4 digits per operand
# and return formatted results to user
for parts in problems:
operand1 = parts.split()
operator = parts.split()
operand2 = parts.split()
# operands must be only digits
if (operand1.isdigit() or operand2.isdigit()) == False:
return "Error: Numbers must only contain digits."
# max 4 digits per operator
if (len(operand1) or len(operand2)) > 4:
return "Error: Numbers cannot be more than four digits."
# formatting results to return to user
# If the user supplied the correct format of problems, the conversion you return will follow these rules:
# single space between the operator and the longest of the two operands,
# the operator will be on the same line as the second operand,
# both operands will be in the same order as provided (the first will be the top one and the second will be the bottom).
# Numbers should be right-aligned.
# There should be four spaces between each problem.
# There should be dashes at the bottom of each problem. That run along the entire length of each problem individually. (The example above shows what this should look like.)
# 32 1 9999 523
# + 8 - 3801 + 9999 - 49
# ---- ------ ------ -----
# 40 -3800 19998 474
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/184.108.40.206 Safari/537.36 Edg/105.0.1343.50
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
ok thanks, I see youre a mod thanks for the advice.
I didnt want to use that for the challenge, but if thatd be good way for one to do something like that , then thanks for the feedback.
I was also curious about possible ways to also complete this challenge using sum() or similar method
I was thinking could check for addition BEFORE splitting, then some how add all the integers in problems & return the sum,
, but seems a little challenging.
If you know what I;m getting at, any advice/ thoughts be appreciated.
so you have a problem = “32 + 698”
to use sum, do you have to get it to problem = (32, 698) ??
so then you can use problem.sum() ??
would that work? would there be a good/easy way?
I’m looking for way to
check if + or - is anywhere in problems before splitting problems.
if + is found, then being able perform sum() ,or best way to add the integers in problems, & return the sum of the integers…all before the splitting step
( I believe I need to eventually split to find lengths of operands etc, but 1st if I could quickly check problems for an + & if found the just return sum of integers in problems thatd be SICK)
****bonus- also way to do it with subtraction?
PS I’m not planning on doing that for this challenge, just interested if something like thatd be possible & best/good options one could do that - including regenex
thats also what I was looking at. using find() to find + or -, then extract the + or -, then sum() or what to just add the remining integers…
eventually I’ll find proper way to code it so it works,
but any knowledge you can explain would be appreciated bc of the time it saves me tying to find all these pieces in the documentations
the BIGGEST hurdle for me in this challenge is have a list thats an equation. if Ive worked with that before then its been a little while, & I havent been able to find anything in the coursework etc that talks about it, or other places I’ve been trying to find more info…
so the list = “operand2 operator operand2” & its not concatenations & ijust havent been able to find much on how to deal w/ a list like that.
if anyone has any documention or link to info on that please let me know
My being a mod has no impact on the quality of advice you get from me. I started learning python only in July of this year. (But I did pass this challenge) Being a Mod only means I get to moderate the forum for bad behaviour/spam etc nothing special.
Honestly you are over complicating this. Let me ask you, are you trying to just practice your python programming? If so it might be good to solve this one the regular way first (split the strings, and examine them them as you lay them out).
If you have solved it, then you can certainly go in again and work on other things like using a regex with a map function for eg to see if the strings are what is expected. Or you can use a reduce function that takes a lambda that performs additions and subtractions depending on a param you pass in. I am sure there are other ways. Try any way you prefer and that you can articulate in a simple step by step list.
(Or you can drive yourself nuts with indecision).
yes, like I stated I’m not going to do it this way for the challenge.
I only posted my initial question bc I know the wording direction of instructions is critical, & where they put this bullet point about needs to contain + or -,
I wasn’t sure if they were looking for a solution doing this before or after splitting the problems list, obviously easiest would be after splitting when checking for length requirements.
however, I then became interested in ways this could be done before splitting the problems list to check for the length requirements etcs.
to be clear - I am specifically trying to figure out a way to do this without regenex.
so far, my best guess is something like
if ‘+’ in problems:
problems = problems.pop(1)
but every way I think to get the operator out , doesnt leave me with something i think sum() can work on.
now I’m looking at instead extracting the integers to put in new list or string to then perform sum(). instead of ttry g to remove the operator.
i think thatll work I just thought of that but havent looked it up
thanks but I basically already completed this challenge except for arranging the arithmetic & returning it to the user.
Im just trying to learn/practice on any things that can be done to lists & the items.
if you have any feed back on best way someone could take this
problem = (“999 + 101”)
what to use to see if a + or - is anywhere in problem,
and if + is somewhere in problem,
then how to create a new variable that only has the integers, for ex:
go from problem = (“999 + 101”)
to operands = (999, 101)