Display Table of Food Orders in a Restaurant LeetCode solution in Python

1
class Solution:
2
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
3
column = ["Table"]
4
dish = []
5
table_dict = {}
6
for order_row in orders:
7
if order_row[-1] not in dish:
8
dish.append(order_row[-1])
9

10
for order_row in orders:
11
if order_row[1] not in table_dict.keys():
12
table_dict[order_row[1]] = {}
13
for food in dish:
14
table_dict[order_row[1]][food] = 0
15
table_dict[order_row[1]][order_row[-1]] += 1
16
else:
17
table_dict[order_row[1]][order_row[-1]] += 1
18

19
dish.sort()
20
column = column + dish
21
ans = [column]
22
table = []
23
childDict = {}
24
for key in sorted(table_dict.keys()):
25
table.append(int(key))
26
childDict[key] = []
27
for value in column:
28
if value != "Table":
29
childDict[key].append(str(table_dict[key][value]))
30
table.sort()
31
output = [ans[0]]
32
for table_num in table:
33
childList = [str(table_num)]
34
output.append(childList + childDict[str(table_num)])
35
return output

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0