-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPython06_Itertools
More file actions
145 lines (116 loc) · 2.98 KB
/
Python06_Itertools
File metadata and controls
145 lines (116 loc) · 2.98 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
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
## Python
## Itertools
## itertools.product()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import product
A = list(map(int, input().split())) # 1 2
B = list(map(int, input().split())) # 3 4
print(*product(A, B))
# (1, 3) (1, 4) (2, 3) (2, 4)
## itertools.permutations()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import permutations
s, k = input().split() # default input type str, eg HACK 2 becomes 'HACK' '2'
s = sorted(s)
k = int(k)
for p in list(permutations(s, k)):
print(*p, sep='')
# AC
# AH
# AK
# CA
# CH
# CK
# HA
# HC
# HK
# KA
# KC
# KH
## itertools.combinations()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations
s, n = input().split() # HACK 2
# # Option A
# for i in range(1, int(n)+1):
# for j in combinations(sorted(s), i):
# print(''.join(j))
# Option B
for k in range(1, int(n)+1):
print(*[''.join(item) for item in combinations(sorted(s), k)], sep='\n')
# A
# C
# H
# K
# AC
# AH
# AK
# CH
# CK
# HK
## itertools.combinations_with_replacement()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations_with_replacement
s, k = map(str, input().split()) # HACK 2
s = sorted(s)
k = int(k)
for e in list(combinations_with_replacement(s, k)):
print(*e, sep='')
# AA
# AC
# AH
# AK
# CC
# CH
# CK
# HH
# HK
# KK
## Compress the String!
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import groupby
# # Option A
# print(*[(len(list(c)), int(k)) for k, c in groupby(input())])
# Option B
for k,v in groupby(input()):
print((len(list(v)),int(k)),end=' ')
# input 1222311
# output (1, 1) (3, 2) (1, 3) (2, 1)
## Iterables and Iterators
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations
N = int(input())
letters = input().split()
K = int(input())
all_combi = list(combinations(letters, K))
# # Option A
# count_a = [i for i in all_combi if 'a' in i]
# print('{0:.3}'.format( len(count_a)/len(all_combi) ))
# Option B
count_a = 0
for combi in all_combi:
if 'a' in combi:
count_a += 1
print('{0:.3}'.format( count_a/len(all_combi) ))
## Maximize It!
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import product
K, M = map(int, input().split())
# # Option A
# Numbers = (list(map(int, input().split()))[1:] for _ in range(K))
# result = map(lambda x: sum(i**2 for i in x)%M, product(*Numbers))
# print(max(result))
# # Option B
# Numbers = []
# for _ in range(K):
# Numbers.append(list(map(int, input().split()))[1:])
# result = list(map(lambda x: sum(pow(i,2) for i in x)%M, product(*Numbers)))
# print(max(result))
# Option C
Numbers = []
for _ in range(K):
Numbers.append(list(map(int, input().split()))[1:])
list_com = list(product(*Numbers))
result = [sum(map(lambda x: x**2,x))%M for x in list_com]
print(max(result))
## end ##