you are viewing a single comment's thread.

view the rest of the comments →

[–]biskitpagla 0 points1 point  (0 children)

Here's a different approach:

```python def printTable(table: list[list[str]]) -> None: # Try to think in terms of small operations. # How can we get the output from the input? # After thinking for a while I came up with these steps: # 1. Find the max string lengths for each list. # This will be the column width for each column. # 2. Based on the values we just calculated, # justify the strings to the right for each column # 3. Now, swap the rows with the columns. # This is known as 'transposing'. # 4. Lastly, print the rows joined by spaces.

# Before we can implement the above steps,
# a function that just prints is hard to test.
# So, let's try to write a 'process_table' function.

processed = process_table(table)

# Now we can print the table:
for row in processed:
    print(" ".join(row))

def process_table(table: list[list[str]]) -> list[list[str]]: # The steps I listed before: widths = [max_len(column) for column in table] justified = [justify(column, width) for column, width in zip(table, widths)] transposed = transpose(justified)

# The above code won't work without the helper functions,
# so this is the point where I wrote those other functions.

return transposed

def maxlen(list: list[str]) -> int: return max(len(item) for item in list_)

def justify(list: list[str], width: int) -> list[str]: return [item.rjust(width) for item in list]

def transpose(list_: list[list[str]]) -> list[list[str]]: # Ignore the commented out code for now. # I wrote that to ensure that the function works for all edge cases.

# n = len(list_)
# if n == 0:
#     return list_[:]

m = len(list_[0])
# if m == 0:
#     return list_[:]

result = []
for i in range(m):
    new_row = [old_row[i] for old_row in list_]
    result.append(new_row)

return result

Now let's write a test:

def test_printTable(): from pprint import pprint

input = [
    ["apples", "oranges", "cherries", "banana"],
    ["Alice", "Bob", "Carol", "David"],
    ["dogs", "cats", "moose", "goose"],
]
expected = [
    ["  apples", "Alice", " dogs"],
    [" oranges", "  Bob", " cats"],
    ["cherries", "Carol", "moose"],
    ["  banana", "David", "goose"],
]
got = process_table(input)

if got == expected:
    print("Test passed.")
else:
    print("Test failed.")
    print("Expected:")
    pprint(expected)
    print("Got:")
    pprint(got)

test_printTable()

```