-
Notifications
You must be signed in to change notification settings - Fork 12
/
Python07_Collections
295 lines (250 loc) · 7.29 KB
/
Python07_Collections
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
## Python
## Collections
## collections.Counter()
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option with Counter
# from collections import Counter
# X = int(input())
# input_list = list(map(int, input().split()))
# sizes_counter = Counter(input_list)
# N = int(input())
# sales = 0
# for _ in range(N):
# size, price = map(int, input().split())
# if sizes_counter[size] > 0:
# sales += price
# sizes_counter[size] -= 1
# print(sales)
# Option without Counter
X = int(input())
sizes_list = list(map(int, input().split()))
N = int(input())
sales = 0
for _ in range(N):
size, price = map(int, input().split())
if size in sizes_list:
sales += price
sizes_list.remove(size)
print(sales)
## DefaultDict Tutorial
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import defaultdict
# n, m = map(int, input().split())
# A = defaultdict(list)
# for i in range(n):
# A[input()].append(i+1)
# B = [input() for _ in range(m)]
# [print(*A.get(letter, [-1])) for letter in B]
# # Option 2
# from collections import defaultdict
# A = defaultdict(list)
# B = []
# n, m = map(int, input().split())
# for i in range(n):
# A[input()].append(i+1)
# for _ in range(m):
# B.append(input()) # or B += [input()]
# for word in B:
# if word in A:
# print(' '.join( map(str, A[word]) ))
# else:
# print(-1)
# Option 3
from collections import defaultdict
A = defaultdict(list)
n, m = map(int, input().split())
for i in range(n):
A[input()].append(i+1)
for _ in range(m):
word = input()
if word in A:
print(' '.join( map(str, A[word]) ))
else:
print(-1)
# # Option 4
# from collections import defaultdict
# n, m = map(int, input().split())
# A = defaultdict(lambda: -1)
# for i in range(1, n+1):
# word = input()
# A[word] = A[word] + ' ' + str(i) if word in A else str(i)
# for _ in range(m):
# print(A[input()])
## Collections.namedtuple()
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import namedtuple
# n = int(input())
# Report = namedtuple('Report', input())
# reports = [Report(*input().split()) for _ in range(n)]
# print(sum([int(e.MARKS) for e in reports])/n)
# # Option 2
# from collections import namedtuple
# n = int(input())
# categories = input().split()
# Report = namedtuple('Report', categories)
# marks = [int(Report._make(input().split()).MARKS) for x in range(n)]
# print('{: 0.2F}'.format( sum(marks)/len(marks) ))
# Option 3
from collections import namedtuple
n = int(input())
Report = namedtuple('Report', input().split())
total = 0
for _ in range(n):
total += int(Report(*input().split()).MARKS)
print(total/n)
# # Option 4 (1 line)
# import collections, statistics
# print('%.2f' % statistics.mean(next((int(student(*row).MARKS) for row in (input().split() for i in range(size))) for size, student in [[int(input()), collections.namedtuple('Report', input())]])))
## Collections.OrderedDict()
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import OrderedDict
# D = OrderedDict()
# for _ in range(int(input())):
# item, space, price = input().rpartition(' ')
# D[item] = D.get(item, 0) + int(price)
# for item, price in D.items():
# print(item, price)
# # Option 2
# from collections import OrderedDict
# D = OrderedDict()
# for _ in range(int(input())):
# item, space, price = input().rpartition(' ')
# D[item] = D.get(item, 0) + int(price)
# print(*[' '.join([item, str(price)]) for item, price in D.items()], sep='\n')
# Option 3 (latest python dictionary is default ordered)
store_item = dict()
for _ in range(int(input())):
key, _, value = input().rpartition(" ")
store_item[key] = store_item.get(key, 0) + int(value)
for k, v in store_item.items():
print(k, v)
## Word Order
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import defaultdict
# n = int(input())
# words_dict = defaultdict(int)
# for _ in range(n):
# key = input()
# words_dict[key] += 1
# print(len(words_dict.keys()))
# print(*words_dict.values())
# # Option 2
# from collections import OrderedDict
# words_dict = OrderedDict()
# for i in range(int(input())):
# key = input()
# if not key in words_dict.keys():
# words_dict.update({key: 1})
# else:
# words_dict[key] += 1
# print(len(words_dict.keys()))
# print(*words_dict.values())
# # Option 3
# from collections import Counter
# input_list = []
# for _ in range(int(input())):
# input_list.extend((input().split('\n')))
# freq = Counter(input_list)
# print(len(freq))
# for key in freq:
# print(freq.get(key), end=' ')
# # Option 4
# from collections import Counter
# input_list = []
# for _ in range(int(input())):
# input_list.append(input())
# print(len(set(input_list)))
# # print(*list(Counter(input_list).values()), sep=' ') #same
# print(' '.join([str(v) for k, v in Counter(input_list).items()]))
# Option 5 (without library)
words = dict()
for _ in range(int(input())):
word = input()
words[word] = words.get(word, 0) + 1
print(len(words))
print(*(word for word in words.values()))
## Collections.deque()
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import deque
# d = deque()
# for _ in range(int(input())):
# inp = input().split()
# getattr(d, inp[0])(*[inp[1]] if len(inp) > 1 else '')
# print(*[item for item in d])
# # Option 2
# from collections import deque
# d = deque()
# for _ in range(int(input())):
# com = input().split() + ['']
# eval( f'd.{com[0]} ({com[1]})' )
# print(*d)
# Option 3
from collections import deque
d = deque()
for _ in range(int(input())):
oper, val, *args = input().split() + ['']
eval( f'd.{oper} ({val})' )
print(*d)
## Company Logo
import math
import os
import random
import re
import sys
if __name__ == '__main__':
s = input()
# # Option 1
# from collections import Counter
# [print(*c) for c in Counter(sorted(s)).most_common(3)]
# # Option 2
# from collections import Counter
# for c in Counter(sorted(s)).most_common(3):
# print(*c)
# Option 3
from collections import Counter
s = sorted(s)
freq = Counter(list(s))
for k,v in freq.most_common(3):
print(k,v)
## Piling Up!
# Enter your code here. Read input from STDIN. Print output to STDOUT
# # Option 1
# from collections import deque
# for _ in range(int(input())):
# result = 'Yes'
# D = deque()
# N = int(input())
# l = list(map(int, input().split()))
# D.extend(l)
# l.sort(reverse=True)
# for i in range(N):
# right = D.pop()
# if i<N-1:
# left = D.popleft()
# if l[i] == left:
# D.append(right)
# continue
# elif l[i] == right:
# D.appendleft(left)
# continue
# else:
# result = 'No'
# break
# print(result)
# Option 2
for t in range(int(input())):
input()
lst = list(map(int, input().split()))
l = len(lst)
i = 0
while i < l - 1 and lst[i] >= lst[i+1]:
i += 1
while i < l - 1 and lst[i] <= lst[i+1]:
i += 1
print('Yes' if i == l - 1 else 'No')
## end ##