forked from Shwetha-Kalavara/Python_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted_assessment.py
More file actions
119 lines (54 loc) · 1.69 KB
/
Copy pathsorted_assessment.py
File metadata and controls
119 lines (54 loc) · 1.69 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# anagrams
def anagram(string1, string2):
return sorted(string1) == sorted(string2)
# print(anagram("tea", "teaa"))
# list of dictionaries
data = [
{"name": "John", "age": 30},
{"name": "Aman", "age": 19},
{"name": "Gita", "age": 15}
]
def function(item):
if item["age"] > 18:
return item["age"]
res = list(filter(function, data))
print(res)
print(sorted(res, key=lambda item: item["age"]))
# s = ["flipkart", "apple", "google"]
# sorted(s, key=lambda item: item[-1])
# data[0]["name"]
# print(sorted(data, key=lambda dict_: dict_["age"]))
shares = {"ACME": 45.23, "AAPL": 612.78, "IBM": 205.55, "HPQ": 37.20}
def func(item):
if item[1] > 40:
return item
res = list(filter(func, shares.items()))
# print(res)
# print(sorted(res, key=lambda item: item[-1]))
# grouping anagrams
l = ["tea", "eat", "silent", "hello", "listen", "ate"]
d = {}
for item in l: # tea
key = "".join(sorted(item)) # ["a", "e", "t"] --> "aet"
if key not in d:
d[key] = [item]
else:
d[key] += [item]
# print(d)
#######################################################################################################
l1 = [1, 2]
l2 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
def zip_(a, b):
res = []
if len(a) == len(b):
return list(zip(a, b))
else:
if len(a) > len(b):
max, min = a, b
else:
max, min = b, a
while max:
res += list(zip(min, max))
del max[:len(min)]
return res
print(zip_(l1, l2))