回答編集履歴

2

edit

2018/04/04 14:02

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -180,9 +180,9 @@
180
180
 
181
181
  res3 = sorted(res3)
182
182
 
183
- assert(all([x==y for x,y in zip(res2, res2)]))
183
+ assert(all([x==y for x,y in zip(res1, res2)]))
184
-
184
+
185
- assert(all([x==y for x,y in zip(res3, res3)]))
185
+ assert(all([x==y for x,y in zip(res1, res3)]))
186
186
 
187
187
  '''
188
188
 

1

edit

2018/04/04 14:02

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -36,4 +36,180 @@
36
36
 
37
37
  c = next(iter_string)
38
38
 
39
+ if b != '':
40
+
41
+ result = [s+b for s in result]
42
+
39
43
  ```
44
+
45
+
46
+
47
+ ---
48
+
49
+
50
+
51
+ 測定。
52
+
53
+ strのjoinが遅い…ナゼ??
54
+
55
+
56
+
57
+ ```python
58
+
59
+ from contextlib import contextmanager
60
+
61
+
62
+
63
+ import time
64
+
65
+ import itertools
66
+
67
+
68
+
69
+ string = "tera-tail"
70
+
71
+ string_org = string
72
+
73
+ rule = "te"
74
+
75
+
76
+
77
+ def f_can110(string, rule):
78
+
79
+ pat = [(c,c.upper()) if c in rule else tuple(c) for c in string]
80
+
81
+ l = [''.join(s) for s in itertools.product(*pat)]
82
+
83
+ return l
84
+
85
+
86
+
87
+ def f_hayataka2049(string, lst):
88
+
89
+ result = [""]
90
+
91
+ for c in string:
92
+
93
+ if c in lst:
94
+
95
+ result = [s+c for s in result] + [s+(c.upper())for s in result]
96
+
97
+ else:
98
+
99
+ result = [s+c for s in result]
100
+
101
+ return result
102
+
103
+
104
+
105
+ def f_rev_hayataka2049(string, lst):
106
+
107
+ iter_string = iter(list(string) + [None])
108
+
109
+ c = next(iter_string)
110
+
111
+ b = ''
112
+
113
+ result = ['']
114
+
115
+ while c:
116
+
117
+ if c not in lst:
118
+
119
+ b += c
120
+
121
+ c = next(iter_string)
122
+
123
+ else:
124
+
125
+ result = [s+b+c for s in result] + [s+b+(c.upper())for s in result]
126
+
127
+ b = ''
128
+
129
+ c = next(iter_string)
130
+
131
+ if b != '':
132
+
133
+ result = [s+b for s in result]
134
+
135
+ return result
136
+
137
+
138
+
139
+ @contextmanager
140
+
141
+ def timer(name):
142
+
143
+ t0 = time.time()
144
+
145
+ yield
146
+
147
+ print(f'[{name:20}] done in {time.time() - t0:.3f} s')
148
+
149
+
150
+
151
+ string = string_org*5
152
+
153
+ for i in range(5, 8):
154
+
155
+ string = string+string_org
156
+
157
+ print('-'*30)
158
+
159
+ with timer(f'rev_hayataka2049_{i}'):
160
+
161
+ res1 = f_rev_hayataka2049(string, rule)
162
+
163
+
164
+
165
+ with timer(f'can110_{i}'):
166
+
167
+ res2 = f_can110(string, rule)
168
+
169
+
170
+
171
+ with timer(f'hayataka2049_{i}'):
172
+
173
+ res3 = f_hayataka2049(string, rule)
174
+
175
+
176
+
177
+ res1 = sorted(res1)
178
+
179
+ res2 = sorted(res2)
180
+
181
+ res3 = sorted(res3)
182
+
183
+ assert(all([x==y for x,y in zip(res2, res2)]))
184
+
185
+ assert(all([x==y for x,y in zip(res3, res3)]))
186
+
187
+ '''
188
+
189
+ ------------------------------
190
+
191
+ [rev_hayataka2049_5 ] done in 0.184 s
192
+
193
+ [can110_5 ] done in 0.225 s
194
+
195
+ [hayataka2049_5 ] done in 0.338 s
196
+
197
+ ------------------------------
198
+
199
+ [rev_hayataka2049_6 ] done in 1.713 s
200
+
201
+ [can110_6 ] done in 2.289 s
202
+
203
+ [hayataka2049_6 ] done in 3.259 s
204
+
205
+ ------------------------------
206
+
207
+ [rev_hayataka2049_7 ] done in 15.343 s
208
+
209
+ [can110_7 ] done in 20.240 s
210
+
211
+ [hayataka2049_7 ] done in 31.384 s
212
+
213
+ '''
214
+
215
+ ```