I have seen a much easier version of this and understand it. For some reason when solving the problem/doing the project, I broke each part into a section and then just worked at it.
My question is: how can I get better at writing efficient code rather than complex code.
Here is my code for reference
#I made this wy harder than it should have been
def arithmetic_arranger(*problems):
import re
from textwrap import dedent
#determines if True is present or not and assigns it
if len(problems) >= 2: tfresult = problems[1]
else: tfresult = False
flist = list()
slist = list()
tlist = list()
folist = list()
rf=list()
rs=list()
rt=list()
rfo=list()
#Rules (checks len of problems)
if len(problems[0])>5: return("Error: Too many problems.")
#makes sure no division or multiplication
count=0
for tasks in problems[0]:
#print(tasks)
if "*" in tasks or "/" in tasks: return("Error: Operator must be '+' or '-'.")
#makes sure no letters
if len(re.findall("[a-zA-Z]",tasks))>0: return("Error: Numbers must only contain digits.")
# splits task into each expression and checks length
#variable assignment for for loop
repeat = len(problems[0])
for expression in tasks.split():
if len(expression) > 4: return("Error: Numbers cannot be more than four digits.")
#END OF RULES
#actual arranger
if count<3: flist.append(expression)
elif count<6: slist.append(expression)
elif count<9: tlist.append(expression)
elif count<12: folist.append(expression)
count+=1
alllists = [flist,slist,tlist,folist]
iterationloop = 0
for indlist in alllists:
if iterationloop == repeat: break
iterationloop+=1
#find how far +/- has to be indented
print(indlist)
print(len(indlist))
f = int(indlist[0])
s = int(indlist[2])
op = str(indlist[1])
if len(indlist[0])>=len(indlist[2]):
opspace = len(indlist[0])
elif len(indlist[0])<len(indlist[2]):
opspace = len(indlist[2])
# formats 1st and 2nd line accordingly (and adds plus to 2nd line)
indlist[2] = indlist[2].rjust(opspace+1)
indlist[1] = indlist[1] + indlist[2]
indlist[0] = indlist[0].rjust(len(indlist[1]))
indlist[2] = '-'*len(indlist[1])
if op == "+":
indlist.append(str(f+s))
elif op == '-':
indlist.append(str(f-s))
indlist[3] = indlist[3].rjust(len(indlist[1]))
rf.append(indlist[0])
rf.append(" ")
rs.append(indlist[1])
rs.append(" ")
rt.append(indlist[2])
rt.append(" ")
if tfresult == True:
rfo.append(indlist[3])
rfo.append(" ")
else: rfo=""
#I feel there is a better quicker way to print the sums, this seems inefficient
strf=''
strs=''
strt=''
strfo=''
strf = ''.join(rf)
strs = ''.join(rs)
strt = ''.join(rt)
strfo = ''.join(rfo)
return dedent(f'''
{strf}
{strs}
{strt}
{strfo} ''')
Here is the simpler code
def arithmetic_arranger(problems, show = False):
firstAnswer = ""
operators = ""
dashlines = ""
secondAnswer = ""
sumup = ""
answer = ""
sum = ""
if(len(problems) > 5):
return "Error: Too many problems."
for problem in problems:
#spliting the strings into a list
firstNumber = problem.split(" ")[0]
operators = problem.split(" ")[1]
secondNumber = problem.split(" ")[2]
# check the length of the number, max 4 digits
if (len(firstNumber) > 4 or len(secondNumber) > 4):
return "Error: Numbers cannot be more than four digits."
# check the input as valid digits
if not firstNumber.isnumeric() or not secondNumber.isnumeric():
return "Error: Numbers must only contain digits."
# check for the correct form of operators
if (operators == '+' or operators == '-'):
if operators == "+":
sum = str(int(firstNumber) + int(secondNumber))
if operators == "-":
sum = str(int(firstNumber) - int(secondNumber))
else:
return "Error: Operator must be '+' or '-'."
length = max(len(firstNumber) , len(secondNumber)) + 2
fisrtline = str(firstNumber).rjust(length)
secondline = operators + str(secondNumber.rjust(length - 1))
sltn = str(sum.rjust(length))
dashline = ""
for dash in range(length):
dashline += "-"
if problem != problems[-1]:
firstAnswer += fisrtline + ' '
secondAnswer += secondline + ' '
dashlines += dashline + ' '
sumup += sltn + ' '
else:
firstAnswer += fisrtline
secondAnswer += secondline
dashlines += dashline
sumup += sltn
if show:
answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines + "\n" + sumup
else:
answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines
return answer