# Scientific Computing with Python Projects - Arithmetic Formatter

Hello, I’m looking for feedback and advice.
https://replit.com/@BiggPaully/boilerplate-arithmetic-formatter-13?v=1

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 ‘-’.”“”

``````def arithmetic_arranger(problems):

#  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()[0]
operator = parts.split()[1]
operand2 = parts.split()[2]

# 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."

#     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

return arranged_problems
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.50`

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

The project doesn’t care which way you do things.
Splitting the strings has to be done anyway so you might as well check the operators when you do that.

if someone was to do it before splitting problems, can you plz advise on best method way to do it? for example: I cant find correct way to code this into python

if + or - not in (problems) :
return “ERROR must b + or -”

I’m trying to check if theres any + or - in the list problems without having to split or do anything else to problems.
thank you

Your logic is a bit backwards. You need to check if every single problem has either a + or a -

well wouldnt the pythonic way be not using resources for splitting up a list when you might not need to

You can use a regex expression if you wish.

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.
for ex:
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?

basically,
I’m looking for way to

1. check if + or - is anywhere in problems before splitting problems.
2. 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

thanks

Find function on the string can be used also. But, why overcomplicate?

People get obsessed with ‘pythonic’. First get something that works. It isn’t ‘pythonic’ if it is broken.

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

Don’t worry about chasing documentation first. First come up with a plan on how to break down the problem. Syntax comes second

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).

1 Like

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)
return problems.sum()

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

Stop thinking about specific syntax for a moment. You are getting wrapped up in the weeds here.

I hand you a list of problems.

There can be more more than 5 problems.

Each problem can only be addition or subtraction.

The numbers in the problems can only have digits, not letters or other characters

The numbers in the problems can only be 4 digits long.

How do you check this by hand?

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)

then finally from there
return operands(sum)

Keep it simple. Split the problem and check if the operator is valid.

You don’t need to pop or anything crazy. Keep it super simple. Simple code is good code.

What if the person types this:

“3 - 4 - 45”

If you try to get the operand without splitting you will not know which one you are getting…

(Remember : testcases here can be tricky too) You shouldn’t assume every test will give you what you expect.

But if you split it then you can immediately see it is not valid.

if you do not know the solution to my question, it is OK to just admit it.
thank you.

again, my initial question was answered regarding the instructions for the challenge & where to do the + - step.

thank you

I know the solution. The solution is to not make it as complicated as you are trying to make it. Complicated is not better.

Break apart the problem. Check if it’s valid. If it is, use the problem. If it isn’t, error.

You’re talking about going back and forth through the input problems array multiple times. That’s not effecient. Only go through once. Keep. It. Simple.

Complicated logic isn’t elegant or ‘pythonic’. Complicated logic usually performs less well. Complicated logic is less maintainable.

Keep. It. Simple.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.