-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07. Grocery.py
61 lines (49 loc) · 1.35 KB
/
07. Grocery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def grocery_store(**kwargs):
products = sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))
return '\n'.join([f"{item[0]}: {item[1]}" for item in products])
""" ON ONE LINE!!! """
# def grocery_store(**kwargs): return '\n'.join([f"{item[0]}: {item[1]}" for item in sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))])
''' TESTS '''
# print(grocery_store(
# bread=5,
# pasta=12,
# eggs=12,
# ))
# print(grocery_store(
# bread=2,
# pasta=2,
# eggs=20,
# carrot=1,
# ))
# def grocery_store(**kwargs):
# '''
#
# The groceries should be sorted by their quantity in descending order.
# If there are two or more products with the same quantity,
# the groceries should be sorted by their name's length in descending order.
# If there are two or more products with the same name's length,
# the groceries should be sorted by their name in ascending order (alphabetically).
#
# '''
#
# string = ''
#
# for item, quantity in sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0])):
# string += f'{item}: {quantity}\n'
#
# return string
#
#
# ''' TESTS '''
# # print(grocery_store(
# # bread=5,
# # pasta=12,
# # eggs=12,
# # ))
#
# # print(grocery_store(
# # bread=2,
# # pasta=2,
# # eggs=20,
# # carrot=1,
# # ))