# Scientific Computing with Python Projects - Arithmetic Formatter

Please could I have some feedback on my code:

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0`

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Nice. Don’t think I’ve seen someone make a ‘spaces’ string and take slices from that to space things… especially after you use `.rjust()` elsewhere which basically solves that problem. Python has soo many little ways to make things much more efficient. I kicked myself when I found out you can just multiply characters in python… so much time spent doing things a harder way.

``````x = "*" * 6
print(x)   #  prints "******"
``````

Anyway, good creative job. The neat thing about these projects is the number of different ways certain problems can be solved. Contradictively the other neat thing about these projects is how two completely independent people can write almost identical code. Don’t know how many times I complete a project thinking I came up with a one of a kind, super creative, no way anyone else thought of that off the wall solution, only to see a ton of other solutions doing the exact same thing.

Thank you for your reply. I found out about `.rjust()` after I’d done the spaces thing. But your code of `"*" * 6` is even better - how did you find that out?

One of my favorite things after completing these and other challenges is to search for other peoples solutions, and see how else it could have been done. A good additional way to learn I always felt. Kinda wish the classes here had the option to see other peoples answers after you’ve successfully submitted your own, but understand the need to keep answers private for certification reasons.

I didn’t know about the multiplication trick for this project either. I did my spacing with formatted strings ( `"{:>5}".format(prob)`), splicing in maxLength in place of 5, but saw the multiplication trick in someone elses answer after the fact and thought, neat.

I also found that the majority of solutions to this one do like you did, maintaining 4 separate lines, then joining them together. My answer was a little messier, keeping an array of each problem as a sub array with first num, sign, second num, answer, and then at the end, loop through the array several times, appending the first value from each, then the sign and second from each, then the answer. Definitely less efficient, but still kinda neat.

I’ve tweaked my code so arithmetic_arranger now looks like this:

``````def arithmetic_arranger(*problems):
if len(problems) > 2:
return 'Error: Too many arguments.'

if isinstance(problems[0], list) == False:
return 'Error: Argument is not an array.'

if len(problems[0]) > 5:
return 'Error: Too many problems.'

oline1 = ''
oline2 = ''
oline3 = ''
oline4 = ''
spaces = '      '
dashes = '------'
for problem in problems[0]:
splitproblem = problem.split()
if len(splitproblem) != 3:
return problem + '\nError: Should have 3 parts.'

num1 = splitproblem[0]
op   = splitproblem[1]
num2 = splitproblem[2]
if op != '+' and op != '-':
return "Error: Operator must be '+' or '-'."

if not num1.isdigit() or not num2.isdigit():
return 'Error: Numbers must only contain digits.'

len1 = len(num1)
len2 = len(num2)
if len1 > 4 or len2 > 4:
return 'Error: Numbers cannot be more than four digits.'

maxlen = max(len1, len2) + 2 #to allow for op and space
if op == '+':
result = int(num1) + int(num2)
else:
result = int(num1) - int(num2)
oline1 += num1.rjust(maxlen)          + '    '
oline2 += op + num2.rjust(maxlen - 1) + '    '
oline3 += '-' * maxlen                + '    '
oline4 += str(result).rjust(maxlen)   + '    '

oline = '\n'.join((oline1.rstrip(), oline2.rstrip(), oline3.rstrip()))
arranged_problems = oline
if len(problems) == 2 and problems[1] == True:
arranged_problems += '\n' + oline4.rstrip()

return arranged_problems
``````

Easier to read, and maybe more efficient(?)

Definitely sleeker. If I remember right on first look I was definitely thinking shouldn’t run .split() multiple times… see you fixed that.

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