Skip to content

Commit bfc816f

Browse files
committed
Added solutions for Table Printer
1 parent 1a91cac commit bfc816f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Table_Printer/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Table Printer
2+
3+
def printTable(arr):
4+
colWidths = [0] * len(arr)
5+
for i in range(len(arr)):
6+
for j in range(len(arr[i])):
7+
if len(arr[i][j]) > colWidths[i]:
8+
colWidths[i] = len(arr[i][j])
9+
10+
for i in range(len(arr[0])):
11+
for j in range(len(arr)):
12+
print(arr[j][i].rjust(colWidths[j]), end=" ")
13+
print()
14+
15+
tableData = [['apples', 'oranges', 'cherries', 'banana'],
16+
['Alice', 'Bob', 'Carol', 'David'],
17+
['dogs', 'cats', 'moose', 'goose']]
18+
19+
printTable(tableData)

0 commit comments

Comments
 (0)