I am a beginner programmer working through one of the final projects on freecodecamp.
I am using Mac OS python3.10
The problem requests that we create a funciton that takes a list of horizontally arranged arithmetic problems as an argument and rearranges them vertically.
This is the function call:
arithmetic_arranger(["32 698", "3801 - 2", "45 43", "123 49"])
And this is the desired outcome:
32 3801 45 123
698 - 2 43 49
----- ------ ---- -----
I was able to reformat the equations vertically but I got stuck trying to figure out how to print them side by side on the same line. Here is the code I wrote.
def aa(problem) :
for i in problem[:] :
problem.remove(i)
p = i.split()
# print(p)
ve = '\t{:>5}\n\t{:<1}{:>4}\n\t-----'.format(p[0],p[1],p[2])
print(ve)
return
aa(["32 698", "3801 - 2", "45 43", "123 49"])
And here is the outcome of that code.
32
698
-----
3801
- 2
-----
45
43
-----
123
49
-----
I have already tried using print(*variable) and ' '.join. When i try those solutions I get this.
3 2
6 9 8
- - - - -
3 8 0 1
- 2
- - - - -
4 5
4 3
- - - - -
1 2 3
4 9
- - - - -
I appreciate you taking the time to read my problem, and thanks for the help.
CodePudding user response:
when you print a character to the terminal the cursor position moves forward. When you print a newline the cursor goes one position down. You'd have to manually bring it up again to print in the above line. You could use ANSI Escape Codes to control the position of the cursor. It's a lot more hard and complex.
You achieve the desired output by changing how to represent the equation. Store each of the equations as [operand1, sign, operand2]. Now, simply print all operand1 in a single line. print sign and operand 2 next. Then print -----.
def fmt(lst):
op1, sign, op2 = zip(*map(str.split, lst))
line1 = "\t".join([f"{op:>5}" for op in op1])
line2 = "\t".join([f"{s:<1}{op:>4}" for s, op in zip(sign, op2)])
line3 = "-----\t"*len(lst)
print("\n".join([line1, line2, line3])
fmt(["32 698", "3801 - 2", "45 43", "123 49"])
Output:
32 3801 45 123
698 - 2 43 49
----- ----- ----- -----
CodePudding user response:
Thank you to all who have taken the time to help. I came up with a solution using the hint from @MarkTolonen who suggested I put a list within a list. Below is my solution. Let me know if there is a way to do this that is more elegant.
Function:
def aa(problem) :
new_list = list()
for i in problem[:] :
problem.remove(i)
p = i.split()
new_list.append(p)
print(new_list)
for l in new_list :
l1 = '{:>5}'.format(l[:][0])
print(l1,end=' ')
print('')
for l in new_list:
l2 = '{:<1}{:>4}'.format(l[:][1],l[:][2])
print(l2,end=' ')
print('')
for l in new_list:
print('-----',end=' ')
return
Function call:
aa(["32 698", "3801 - 2", "45 43", "123 49"])
Outcome:
32 3801 45 123
698 - 2 43 49
----- ----- ----- ----- %
Also. what does the '%' mean at the end of the printed dashes?
CodePudding user response:
Here's a solution that exactly matches the desired outcome. It determines the widths of each equation:
def arithmetic_arranger(equations):
# Parse to list of lists, e.g.:
# [['32', ' ', '698'], ['3801', '-', '2'], ['45', ' ', '43'], ['123', ' ', '49']]
parsed = [[item for item in equation.split()] for equation in equations]
# For each sublist, determine the widest string and build a list of those widths, e.g.:
# [3, 4, 2, 3]
widths = [len(max(items,key=len)) for items in parsed]
# zip() matches each width with each parsed sublist.
# Print the first operands right-justified with appropriate widths.
print(' '.join([f' {a:>{w}}' for w,(a,op,b) in zip(widths,parsed)]))
# Print the operator and the second operand.
print(' '.join([f'{op} {b:>{w}}' for w,(a,op,b) in zip(widths,parsed)]))
# Print the dashed lines under each equation.
print(' '.join(['-'*(w 2) for w in widths]))
arithmetic_arranger(["32 698", "3801 - 2", "45 43", "123 49"])
Output:
32 3801 45 123
698 - 2 43 49
----- ------ ---- -----
