Home > database >  Arrange output vertically and side-by-side in python
Arrange output vertically and side-by-side in python

Time:01-30

So I want to arrange arithmetic problems vertically in a nice column.

Code: `def arithmetic_arranger(problems, calculate = False):

for problem in problems:
    problem = problem.split()
    first_number = problem[0]
    second_number = problem[2]
    sign = problem[1]
    
    try:
        first_number = int(first_number) 
        second_number = int(second_number)   
    except:
        print('Error: Numbers must only contain digits.')
        break

    if calculate is True:
        if sign == ' ':
            result = first_number   second_number
        else:
            result = first_number - second_number

    print(f'{first_number}\n{sign} {second_number}\n-----')
    
    if calculate is True:
        print(result)

arithmetic_arranger(["32 8", "1 - 3801", "9999 9999", "523 - 49"], True)`

This is the current output:


  8
-----
40
1
- 3801
-----
-3800
9999
  9999
-----
19998
523
- 49
-----
474

And I want to turn that into:

  32         1      9999      523
   8    - 3801      9999    -  49
----    ------    ------    -----
  40     -3800     19998      474

I have tried adding first numbers to list and then displaying them in the row but that didn't really work.

CodePudding user response:

Here is a proposition (based on your code) with a little help from .

#pip install tabulate
from tabulate import tabulate
​
def arithmetic_arranger(problems, calculate=False):
    problem_lists = [problem.split() for problem in problems]
    
    if calculate:
        results = [eval(" ".join(problem)) for problem in problem_lists]
        problem_lists = [problem_lists[i]   [result]
                         for i, result in enumerate(results)]
        
    problem_lists = list(map(list, zip(*problem_lists)))
    problem_lists[1:3] = [[" ".join(x)
                           for x in zip(problem_lists[1], problem_lists[2])]]
​
    print(tabulate(problem_lists, tablefmt="simple", showindex=False, 
                   numalign="right", stralign="right"))

Output : ​

arithmetic_arranger(["32   8", "1 - 3801", "9999   9999", "523 - 49"], True)
​
---  ------  ------  ----
 32       1    9999   523
  8  - 3801    9999  - 49
 40   -3800   19998   474
---  ------  ------  ----

CodePudding user response:

I'd approach this by creating a function that takes the two numbers to add/subtract as numbers, not strings. Then using len(str(x)) to infer the printed length of the number as a string, we can print the appropraite number of leading spaces and dashes.

def long_op(x, y, op=" "):
    if op == " ":
        result = x   y
    elif op == "-":
        result = x - y
    else:
        raise ValueError("op must be   or -")
    width = max([len(str(i)) for i in [x, y]])
    first = " " * (2   width - len(str(x)))   str(x)
    second = op   " " * (1   width - len(str(y)))   str(y)
    bar = "-" * (2   width)
    last = " " * (2   width - len(str(result)))   str(result)
    return "\n".join([first, second, bar, last])

def multi_ops(*args):
    long_ops = [long_op(*arg).split("\n") for arg in args]
    transpose = list(map(list, zip(*long_ops)))
    return "\n".join(["   ".join(l) for l in transpose])

print(long_op(32,8))
print()
print(multi_ops([32,8], [1,3801,"-"], [9999,9999], [523,49,"-"]))

Output:

  32
   8
----
  40

  32        1     9999     523
   8   - 3801     9999   -  49
----   ------   ------   -----
  40    -3800    19998     474

Demo at SageCell.SageMath.org.

  • Related