Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
 
Example 1:
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]] 
Explanation:
The displaying table looks like:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0
For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
For the table 5: Carla orders "Water" and "Ceviche".
For the table 10: Corina orders "Beef Burrito". 

Example 2:
Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]] 
Explanation: 
For the table 1: Adam and Brianna order "Canadian Waffles".
For the table 12: James, Ratesh and Amadeus order "Fried Chicken".

Example 3:
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]

 
Constraints:

1 <= orders.length <= 5 * 10^4
orders[i].length == 3
1 <= customerNamei.length, foodItemi.length <= 20
customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
tableNumberi is a valid integer between 1 and 500.

Difficulty: MEDIUM
Tags: String algorithms

Sorting

Data structures
Skill types: Sorting

Data structures

Starter code:
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        

Solutions:
from collections import defaultdict

class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		record = defaultdict(lambda : defaultdict(int))
		for (_, table, item) in orders:
			record[table][item] += 1
		res = []
		items = sorted(set((i for v in list(record.values()) for i in list(v.keys()))))
		for (table, v) in list(record.items()):
			res.append([table] + ['0' if i not in v else str(v[i]) for i in items])
		res.sort(key=lambda x: [int(i) for i in x])
		res = [['Table'] + items] + res
		return res


class Solution:

	def displayTable(self, orders):
		(food, table) = ([0] * len(orders), [''] * len(orders))
		for (i, item) in enumerate(orders):
			(table[i], food[i]) = (int(item[1]), item[2])
		food = sorted(set(food))
		table = sorted(set(table))
		dic = {str(key): [0] * len(food) for key in table}
		for item in orders:
			dic[item[1]][food.index(item[2])] += 1
		res = [['Table'] + food]
		for (key, val) in dic.items():
			res.append([key] + list(map(str, val)))
		return res


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		desk = collections.defaultdict(collections.Counter)
		meal = set()
		for (_, table, food) in orders:
			meal.add(food)
			desk[table][food] += 1
		foods = sorted(meal)
		result = [['Table'] + [food for food in foods]]
		for table in sorted(desk, key=int):
			result.append([table] + [str(desk[table][food]) for food in foods])
		return result


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		table = collections.defaultdict(dict)
		food = set()
		for (_, i, j) in orders:
			table[i][j] = table[i].get(j, 0) + 1
			food.add(j)
		res = [['Table'] + sorted(food)]
		for i in sorted(table, key=lambda x: int(x)):
			tmp = [str(i)]
			for j in range(1, len(res[0])):
				tmp.append(str(table[str(i)].get(res[0][j], 0)))
			res.append(tmp)
		return res


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		table_numbers = sorted(set([int(i[1]) for i in orders]))
		items = sorted(set([i[2] for i in orders]))
		temp_dict = {str(key): [0] * len(items) for key in table_numbers}
		for item in orders:
			temp_dict[item[1]][items.index(item[2])] = temp_dict[item[1]][items.index(item[2])] + 1
		result = [['Table'] + items]
		for (key, val) in temp_dict.items():
			result.append([key] + list(map(str, val)))
		return result


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		tables = {}
		dishes = {}
		for (name, table, dish) in orders:
			if table not in tables:
				tables[table] = 0
			if dish not in dishes:
				dishes[dish] = 0
		tables = sorted(tables.keys(), key=lambda x: int(x))
		dishes = sorted(dishes.keys())
		tables.insert(0, 'Table')
		dishes.insert(0, 'Table')
		nrows = len(tables)
		ncolumns = len(dishes)
		results = [[0 for j in range(ncolumns)] for i in range(nrows)]
		results[0] = dishes
		tables = {table: index for (index, table) in enumerate(tables)}
		dishes = {dish: index for (index, dish) in enumerate(dishes)}
		for (name, table, dish) in orders:
			i = tables[table]
			j = dishes[dish]
			results[i][0] = table
			results[i][j] = results[i][j] + 1
		for i in range(nrows):
			for j in range(ncolumns):
				results[i][j] = str(results[i][j])
		return results


from collections import Counter, defaultdict

class Solution:

	def displayTable(self, orders):
		foods = set()
		table_to_food = defaultdict(Counter)
		for (customer, table, food) in orders:
			table_to_food[table][food] += 1
			foods.add(food)
		foods = sorted(foods)
		m = len(table_to_food)
		n = len(foods)
		res = [[None for j in range(n + 1)] for i in range(m + 1)]
		res[0] = ['Table'] + foods
		for (i, table) in enumerate(sorted(table_to_food.keys(), key=int)):
			res[i + 1][0] = str(table)
			for j in range(0, n):
				food = foods[j]
				res[i + 1][j + 1] = str(table_to_food[table][food])
		return res


from collections import defaultdict

class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		result = []
		table_dct = defaultdict(list)
		items = set()
		tables = set()
		for order in orders:
			table_dct[order[1]].append(order[2])
			items.add(order[2])
			tables.add(int(order[1]))
		items = sorted(list(items))
		tables = sorted(list(tables))
		result.append(['Table'] + items)
		for table in tables:
			table_result = [str(table)]
			for item in items:
				table_result.append(str(table_dct[str(table)].count(item)))
			result.append(table_result)
		return result


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		tables = {}
		menu = set()
		for o in orders:
			(num, food) = (int(o[1]), o[2])
			if num not in tables:
				tables[num] = {}
			if food not in tables[num]:
				tables[num][food] = 0
			tables[num][food] += 1
			if food not in menu:
				menu.add(food)
		menu = sorted(menu)
		res = [['Table'] + menu]
		for table_num in sorted(tables, key=lambda n: n):
			row = [str(table_num)]
			table_order = tables[table_num]
			for food in menu:
				food_count = table_order.get(food, 0)
				row.append(str(food_count))
			res.append(row)
		return res


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		table = []
		food = []
		for item in orders:
			table.append(int(item[1]))
			food.append(item[2])
		food = sorted(list(set(food)))
		table = sorted(list(set(table)))
		my_dict = {str(key): [0] * len(food) for key in table}
		for item in orders:
			my_dict[item[1]][food.index(item[2])] = my_dict[item[1]][food.index(item[2])] + 1
		header = ['Table'] + food
		footer = []
		for (key, val) in list(my_dict.items()):
			footer.append([key] + list(map(str, val)))
		return [header] + footer


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		menu = sorted(set([i[2] for i in orders]))
		wat = sorted(orders, key=lambda order: int(order[1]))
		wat2 = itertools.groupby(wat, key=lambda order: order[1])
		alpha_cust = [[c, [i[2] for i in cgen]] for (c, cgen) in wat2]
		ans = [['Table'] + menu]
		for table in alpha_cust:
			ans.append([table[0]] + [str(table[1].count(i)) for i in menu])
		return ans


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		d = {}
		foods = set()
		for x in orders:
			table = x[1]
			food = x[2]
			foods.add(food)
			if table not in d:
				d[table] = {}
			if food in d[table]:
				d[table][food] += 1
			else:
				d[table][food] = 1
		print(d)
		foodList = list(foods)
		foodList.sort()
		foodList.insert(0, 'Table')
		hold = foodList
		f = [hold.copy()]
		dTemp = sorted(list(d.items()), key=lambda x: int(x[0]))
		for (tableNo, tableFood) in dTemp:
			a = []
			for colName in hold:
				if colName == 'Table':
					a.append(str(tableNo))
				elif colName in tableFood:
					a.append(str(tableFood[colName]))
				else:
					a.append('0')
			f.append(a)
		return f


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		orders.sort(key=lambda x: int(x[1]))
		record = defaultdict(lambda : defaultdict(int))
		foods = []
		for (name, table, food) in orders:
			if food not in foods:
				foods.append(food)
			record[table][food] += 1
		foods = sorted(foods)
		out = [['Table'] + foods]
		for (k, v) in record.items():
			tmp = [k]
			for f in foods:
				tmp += [str(v.get(f)) if v.get(f) else '0']
			out.append(tmp)
		return out


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		ht1 = {}
		col = []
		display = []
		for (i, item) in enumerate(orders):
			table = int(item[1])
			dish = item[2]
			if table in ht1:
				ht1[table].append(i)
			else:
				ht1[table] = [i]
			if dish not in col:
				col.append(dish)
		row = list(ht1.keys())
		row.sort()
		col.sort()
		display = [[]] * (len(row) + 1)
		display[0] = ['Table']
		for item in col:
			display[0].append(item)
		for (i, item) in enumerate(row):
			display[i + 1] = [str(item)]
			for j in range(len(col)):
				display[i + 1].append(0)
			for o in ht1[item]:
				dish = orders[o][2]
				c = col.index(dish)
				display[i + 1][c + 1] += 1
			for j in range(len(col)):
				display[i + 1][j + 1] = str(display[i + 1][j + 1])
		return display


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		food = []
		d = OrderedDict()
		for (_, _, k) in orders:
			if k not in food:
				food.append(k)
		food.sort()
		n = len(food)
		new = [(i, f) for (i, f) in enumerate(food)]
		for (_, j, k) in orders:
			if j not in d:
				d[j] = [0 for _ in range(n)]
				for (i, f) in new:
					if k == f:
						d[j][i] = 1
						break
			else:
				for (i, f) in new:
					if k == f:
						d[j][i] += 1
						break
		d = OrderedDict(sorted(d.items(), key=lambda x: int(x[0])))
		title = ['Table'] + food
		details = [[k] + [str(i) for i in v] for (k, v) in d.items()]
		details.insert(0, title)
		return details


from collections import defaultdict

class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		d = defaultdict(lambda : defaultdict(int))
		foods = set()
		for (_, table, order) in orders:
			d[int(table)][order] += 1
			foods.add(order)
		foods = sorted(foods)
		ret = [['Table'] + foods]
		for table in sorted(d.keys()):
			ret.append([str(table)] + [str(d[table][f]) for f in foods])
		return ret


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		item_list = []
		order_dict = {}
		for order in orders:
			table = order[1]
			item = order[2]
			if item not in item_list:
				item_list.append(item)
			if int(table) not in order_dict:
				order_dict[int(table)] = []
			order_dict[int(table)].append(item)
		item_list.sort()
		item_list.insert(0, 'Table')
		order_dict = dict(sorted(order_dict.items()))
		sol = []
		sol.append(item_list)
		for (table_name, table_orders) in list(order_dict.items()):
			table_sol = ['0' for i in range(len(item_list))]
			table_sol[0] = table_name
			for order in table_orders:
				ind = item_list.index(order)
				table_sol[ind] = str(int(table_sol[ind]) + 1)
			sol.append(table_sol)
		for i in range(1, len(sol)):
			sol[i][0] = str(sol[i][0])
		return sol


from collections import defaultdict

class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		table_to_dishes = defaultdict(lambda : defaultdict(int))
		dishes = set()
		for order in orders:
			table = int(order[1])
			dish = order[2]
			table_to_dishes[table][dish] += 1
			dishes.add(dish)
		headers = ['Table']
		headers += [dish for dish in sorted(dishes)]
		result = [headers]
		for table in sorted(table_to_dishes.keys()):
			table_order = [str(table)]
			for ind in range(1, len(headers)):
				dish = headers[ind]
				table_order.append(str(table_to_dishes[table][dish]))
			result.append(table_order)
		return result


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		hm = {}
		food = sorted(set([x[2] for x in orders]))
		tables = sorted(set([int(x[1]) for x in orders]))
		res = [['Table'] + list(food)] + [[str(x)] for x in tables]
		for item in orders:
			cur = hm.get(item[1], {})
			num = cur.get(item[2], 0)
			cur[item[2]] = num + 1
			hm[item[1]] = cur
		for i in range(1, len(tables) + 1):
			cur = hm[str(tables[i - 1])]
			for f in food:
				res[i].append(str(cur.get(f, 0)))
		return res


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		unique_tables = []
		unique_dishes = []
		ans = [['Table']]
		for i in orders:
			if int(i[1]) not in unique_tables:
				unique_tables.append(int(i[1]))
			if i[2] not in unique_dishes:
				unique_dishes.append(i[2])
		unique_tables.sort()
		unique_dishes.sort()
		ans[0].extend(unique_dishes)
		for i in range(len(unique_tables)):
			listt = [unique_tables[i]]
			listt.extend([0] * len(unique_dishes))
			ans.append(listt)
		for i in orders:
			ans[unique_tables.index(int(i[1])) + 1][unique_dishes.index(i[2]) + 1] += 1
		for i in range(len(ans)):
			for j in range(len(ans[0])):
				ans[i][j] = str(ans[i][j])
		return ans


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		ords = defaultdict(collections.Counter)
		meals = set()
		for (_, T, M) in orders:
			meals.add(M)
			ords[T][M] += 1
		foods = sorted(meals)
		result = [['Table'] + [food for food in foods]]
		for T in sorted(ords, key=int):
			result.append([T] + [str(ords[T][M]) for M in foods])
		return result


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		all_foods = set()
		all_tables = set()
		mapping = defaultdict()
		for order in orders:
			(_, table_id, food_name) = (order[0], int(order[1]), order[2])
			all_foods.add(food_name)
			all_tables.add(table_id)
			if table_id not in mapping:
				mapping[table_id] = defaultdict(int)
			mapping[table_id][food_name] += 1
		all_foods = sorted(list(all_foods))
		all_tables = sorted(list(all_tables))
		labels = ['Table'] + all_foods
		data_table = [labels]
		for table in all_tables:
			temp = [str(table)]
			for food in all_foods:
				temp.append(str(mapping[table][food]))
			data_table.append(temp.copy())
		return data_table


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		tableFood = {}
		Fooditems = set()
		for i in orders:
			if i[1] in list(tableFood.keys()):
				tableFood[i[1]].append(i[2])
			else:
				tableFood[i[1]] = [i[2]]
			Fooditems.add(i[2])
		Fooditems = list(Fooditems)
		Fooditems.sort()
		outTable = []
		for (k, v) in list(tableFood.items()):
			tmp = [k]
			for i in Fooditems:
				tmp.append(str(v.count(i)))
			outTable.append(tmp)
		outTable.sort(key=lambda outTable: int(outTable[0]))
		return [['Table'] + Fooditems] + outTable


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		d = {}
		dn = []
		for o in orders:
			if o[2] not in dn:
				dn.append(o[2])
			if o[1] in d:
				if o[2] in d[o[1]]:
					d[o[1]][o[2]] += 1
				else:
					d[o[1]][o[2]] = 1
			else:
				d[o[1]] = {}
				d[o[1]][o[2]] = 1
		dn.sort()
		fr = ['Table']
		for n in dn:
			fr.append(n)
		ans = [fr]
		st = list(d.keys())
		st.sort(key=lambda x: int(x))
		for t in st:
			r = [t]
			for n in dn:
				if n in d[t]:
					r.append(str(d[t][n]))
				else:
					r.append('0')
			ans.append(r)
		return ans


class Solution:

	def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
		d = collections.defaultdict(lambda : collections.defaultdict(int))
		foods = set()
		for (_, table, food) in orders:
			foods.add(food)
			d[int(table)][food] += 1
		res = [[0] * (len(foods) + 1) for _ in range(len(list(d.keys())) + 1)]
		columns = {food: i + 1 for (i, food) in enumerate(sorted(foods))}
		res[0] = ['Table'] + sorted(foods)
		for (i, table) in enumerate(sorted(d.keys())):
			res[i + 1][0] = table
			for food in d[table]:
				res[i + 1][columns[food]] += d[table][food]
		return [list(map(str, x)) for x in res]