-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytemp_tozawa.ini
425 lines (322 loc) · 8.27 KB
/
pytemp_tozawa.ini
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import numpy as np
import bisect
再起上限の引き上げ
import sys
sys.setrecursionlimit(1 << 20)
入力 x
x = int(input())
入力 x y
n,x = map(int, input().split(' '))
入力 n / a1 ... an
n = int(input())
a = list(map(int, input().split(' ')))
入力 n / s1 / s2 / ... / sn
n = int(input())
s=[]
for i in range(n):
s.append(int(input()))
入力 n / x1 y1 / ... / xn yn
n=int(input())
pair = [tuple(map(int, input().split(' '))) for i in range(n)]
入力 H W/ a11 a12 ... a1W / ... / aH1 aH2 ... aHW
H,W=map(int,input().split())
A = [input().split() for _ in range(H)]
入力を桁ごとに配列に格納
s=input()
S=[int(x) for x in list(str(s))]
入力を桁ごとに配列に格納(改良)
A=[0]
a=list(map(int,input().split()))
A.extend(a)
入力 N M/ A1 B1 / ... / AM BM
import numpy as np
N,M=map(int,input().split())
A,B=np.zeros(M+1,dtype=int),np.zeros(M+1,dtype=int)
for i in range(1,N+1):
A[i],B[i]=map(int,input().split())
入力 to 隣接リスト
import numpy as np
N, M = map(int, input().split())
graph = [[] for _ in range(N+1)]
A,B=np.zeros(M+1),np.zeros(M+1)
for i in range(1,M+1):
A[i], B[i] = map(int, input().split())
a,b=int(A[i]),int(B[i])
graph[a].append(b)
graph[b].append(a) # 有向グラフなら消す
入力 to 隣接リスト(重み付き)
import numpy as np
N, M = map(int, input().split())
graph = [[] for _ in range(N+1)]
A,B,C =np.zeros(M+1),np.zeros(M+1),np.zeros(M+1)
for i in range(1,M+1):
A[i], B[i] ,C[i]= map(int, input().split())
a,b,c=int(A[i]),int(B[i]),int(C[i])
graph[a].append([b,c])
graph[b].append([a,c]) # 有向グラフなら消す
優先度付きキュー
import heapq # heapqライブラリのimport
a = [1, 6, 8, 0, -1]
heapq.heapify(a) # リストを優先度付きキューへ
print(a)
# 出力: [-1, 0, 8, 1, 6] (優先度付きキューとなった a)
入力1から入れたいとき
A=[0]
a=list(map(int,input().split()))
for i in range(N):
A.append(a[i])
辞書にリストをいれ込む
friend = {}
friend.setdefault(A[i],[]).append(B[i])
多次元配列のソート
A,B,C=np.zeros(M+1,dtype=int),np.zeros(M+1,dtype=int),np.zeros(M+1,dtype=int)
all_abc=np.zeros((M+1,3),dtype=int)
for i in range(1,M+1):
A[i],B[i],C[i]=map(int,input().split())
all_abc[i]=np.array([A[i],B[i],C[i]])
all_abc=all_abc[np.argsort(all_abc[:,2])]
配列
arr = []
print(arr)
# []
arr = [0] * 10
print(arr)
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
二次元配列
arr = [[]]
print(arr)
# [[]]
arr = [[0] * 4 for i in range(3)]
print(arr)
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
配列の展開
arr = [1, 2, 3]
print(*arr)
# 1 2 3
*をつけると、配列でもタプルでもdictでも展開される。
キュー
from collections import deque
追加
d = deque(['m', 'n'])
d.append('o')
print(d)
# deque(['m', 'n', 'o'])
d.appendleft('l')
print(d)
# deque(['l', 'm', 'n', 'o'])
削除
d = deque(['a', 'b', 'c', 'b', 'd'])
print(d.pop())
# d
print(d)
# deque(['a', 'b', 'c', 'b'])
print(d.popleft())
# a
print(d)
# deque(['b', 'c', 'b'])
d.remove('b')
print(d)
# deque(['c', 'b'])
リスト内にあるかどうか確認
import itertools
lst = [0,1,2]
print(1 in lst)
# True
print(3 in lst)
# False
lst = set([0,1,2])
print(1 in lst)
# True
lst = {0,1,2}
print(1 in lst)
# True
mod = 1000000007
累積和(数値)
import itertools
arr = [0,1,3,5,7,9]
print(arr)
# [0, 1, 3, 5, 7, 9]
cumsum = list(itertools.accumulate(arr))
print(cumsum)
# [0, 1, 4, 9, 16, 25]
累積和(文字)
import itertools
s = ['ab', 'bc', 'cd']
print(s)
# ['ab', 'bc', 'cd']
scum = list(itertools.accumulate(s))
print(scum)
# ['ab', 'abbc', 'abbccd']
順列(数値)
import itertools
lst = [1, 2, 3]
print(lst)
# [1, 2, 3]
permu = list(itertools.permutations(lst))
print(permu)
# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
順列(文字列)
import itertools
s = 'abc'
permus = list(itertools.permutations(s))
print(permus)
# [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
組み合わせ(数値)
import itertools
lst = [1, 2, 3, 4]
print(lst)
# [1, 2, 3, 4]
comb = list(itertools.combinations(lst, 2))
print(comb)
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
comb = list(itertools.combinations(lst, 3))
print(comb)
# [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
重複を許す組み合わせ(数値)
import itertools
lst = [1, 2, 3, 4]
print(lst)
# [1, 2, 3, 4]
combr = list(itertools.combinations_with_replacement(lst, 2))
print(combr)
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
直積
import itertools
prd = list(itertools.product([0,1], repeat=3))
print(prd)
# [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
入力データを用いて、直積で全パターンを得る。
import itertools
lst = [(0,1), (2,3), (2,4)]
prd = list(itertools.product( *lst ))
print(prd)
# [(0, 2, 2), (0, 2, 4), (0, 3, 2), (0, 3, 4), (1, 2, 2), (1, 2, 4), (1, 3, 2), (1, 3, 4)]
for p in prd:
p = set(p)
print(p)
# {0, 2}
# {0, 2, 4}
# {0, 2, 3}
# {0, 3, 4}
# {1, 2}
# {1, 2, 4}
# {1, 2, 3}
# {1, 3, 4}
ビットが立っているものだけ残す
import itertools
lst = [1, 2, 3, 4]
bit = [1, 0, 0, 1]
cps = list(itertools.compress(lst, bit))
print(cps)
# [1, 4]
各値が何個連続しているか
import itertools
bi = [0,0,0,1,1,0,0,0,1,1,0,1]
print(bi)
# [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1]
gr = itertools.groupby(bi)
for key, group in gr:
print(f'{key}: {list(group)}')
# 0: [0, 0, 0]
# 1: [1, 1]
# 0: [0, 0, 0]
# 1: [1, 1]
# 0: [0]
# 1: [1]
gr = itertools.groupby(bi)
for key, group in gr:
print(f'{key}: {len(list(group))}')
# 0: 3
# 1: 2
# 0: 3
# 1: 2
# 0: 1
# 1: 1
参考:
https://docs.python.org/ja/3/library/itertools.html
https://qiita.com/SaitoTsutomu/items/ddb5076ef62745f03b56
nCr:組み合わせの数(math)
import math
n = 7
r = 5
cb = math.comb(n, r)
print(cb) # -> 21
nCr:組み合わせの数(scipy) ※速い
※言語をPythonにする
from scipy.special import comb
n = 7
r = 5
cb = comb(n, r, exact=True)
print(cb) # 21
exact=Trueを指定することで戻り値が"inf"になるのを回避できる 正確な値を求めたい場合にもこの引数が必要。(その分若干遅くなる。)
上記を気にせず使いたい場合は引数を省略。
a = comb(n, r)
素因数分解
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
factorization(24)
## [[2, 3], [3, 1]]
## 24 = 2^3 * 3^1
タイル問題出力
for l in tile:
print("".join(l))
##UnionFind
from collections import defaultdict
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
group_members = defaultdict(list)
for member in range(self.n):
group_members[self.find(member)].append(member)
return group_members
def __str__(self):
return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
##
https://note.nkmk.me/python-union-find/
##平衡2分探索木
https://github.com/tatyam-prime/SortedSet/blob/main/SortedMultiset.py
##配列の全てに対してif文を通す
a = [1, 2, 3, 4, 5]
print(np.all(a < 5))
# False