質問するログイン新規登録

回答編集履歴

2

edit

2018/04/04 14:02

投稿

mkgrei
mkgrei

スコア8562

answer CHANGED
@@ -89,8 +89,8 @@
89
89
  res1 = sorted(res1)
90
90
  res2 = sorted(res2)
91
91
  res3 = sorted(res3)
92
- assert(all([x==y for x,y in zip(res2, res2)]))
92
+ assert(all([x==y for x,y in zip(res1, res2)]))
93
- assert(all([x==y for x,y in zip(res3, res3)]))
93
+ assert(all([x==y for x,y in zip(res1, res3)]))
94
94
  '''
95
95
  ------------------------------
96
96
  [rev_hayataka2049_5 ] done in 0.184 s

1

edit

2018/04/04 14:02

投稿

mkgrei
mkgrei

スコア8562

answer CHANGED
@@ -17,4 +17,92 @@
17
17
  result = [s+b+c for s in result] + [s+b+(c.upper())for s in result]
18
18
  b = ''
19
19
  c = next(iter_string)
20
+ if b != '':
21
+ result = [s+b for s in result]
22
+ ```
23
+
24
+ ---
25
+
26
+ 測定。
27
+ strのjoinが遅い…ナゼ??
28
+
29
+ ```python
30
+ from contextlib import contextmanager
31
+
32
+ import time
33
+ import itertools
34
+
35
+ string = "tera-tail"
36
+ string_org = string
37
+ rule = "te"
38
+
39
+ def f_can110(string, rule):
40
+ pat = [(c,c.upper()) if c in rule else tuple(c) for c in string]
41
+ l = [''.join(s) for s in itertools.product(*pat)]
42
+ return l
43
+
44
+ def f_hayataka2049(string, lst):
45
+ result = [""]
46
+ for c in string:
47
+ if c in lst:
48
+ result = [s+c for s in result] + [s+(c.upper())for s in result]
49
+ else:
50
+ result = [s+c for s in result]
51
+ return result
52
+
53
+ def f_rev_hayataka2049(string, lst):
54
+ iter_string = iter(list(string) + [None])
55
+ c = next(iter_string)
56
+ b = ''
57
+ result = ['']
58
+ while c:
59
+ if c not in lst:
60
+ b += c
61
+ c = next(iter_string)
62
+ else:
63
+ result = [s+b+c for s in result] + [s+b+(c.upper())for s in result]
64
+ b = ''
65
+ c = next(iter_string)
66
+ if b != '':
67
+ result = [s+b for s in result]
68
+ return result
69
+
70
+ @contextmanager
71
+ def timer(name):
72
+ t0 = time.time()
73
+ yield
74
+ print(f'[{name:20}] done in {time.time() - t0:.3f} s')
75
+
76
+ string = string_org*5
77
+ for i in range(5, 8):
78
+ string = string+string_org
79
+ print('-'*30)
80
+ with timer(f'rev_hayataka2049_{i}'):
81
+ res1 = f_rev_hayataka2049(string, rule)
82
+
83
+ with timer(f'can110_{i}'):
84
+ res2 = f_can110(string, rule)
85
+
86
+ with timer(f'hayataka2049_{i}'):
87
+ res3 = f_hayataka2049(string, rule)
88
+
89
+ res1 = sorted(res1)
90
+ res2 = sorted(res2)
91
+ res3 = sorted(res3)
92
+ assert(all([x==y for x,y in zip(res2, res2)]))
93
+ assert(all([x==y for x,y in zip(res3, res3)]))
94
+ '''
95
+ ------------------------------
96
+ [rev_hayataka2049_5 ] done in 0.184 s
97
+ [can110_5 ] done in 0.225 s
98
+ [hayataka2049_5 ] done in 0.338 s
99
+ ------------------------------
100
+ [rev_hayataka2049_6 ] done in 1.713 s
101
+ [can110_6 ] done in 2.289 s
102
+ [hayataka2049_6 ] done in 3.259 s
103
+ ------------------------------
104
+ [rev_hayataka2049_7 ] done in 15.343 s
105
+ [can110_7 ] done in 20.240 s
106
+ [hayataka2049_7 ] done in 31.384 s
107
+ '''
20
108
  ```