回答編集履歴
4
リファクタリング
test
CHANGED
@@ -8,13 +8,11 @@
|
|
8
8
|
|
9
9
|
def has_serial_duplicated_chars(src_str):
|
10
10
|
|
11
|
-
|
11
|
+
return any(
|
12
12
|
|
13
|
-
|
13
|
+
ch1 == ch2 for ch1, ch2 in zip(src_str, src_str[1:])
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
)
|
18
16
|
|
19
17
|
|
20
18
|
|
@@ -54,7 +52,7 @@
|
|
54
52
|
|
55
53
|
|
56
54
|
|
57
|
-
**実行結果** [Wandbox](https://wandbox.org/permlink/
|
55
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/AKBkeathKA1JpZ8Q)
|
58
56
|
|
59
57
|
```
|
60
58
|
|
3
修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
[itertools.groupby](https://docs.python.jp/3/library/itertools.html#itertools.groupby)を使うのはいかがでしょうか。
|
2
2
|
|
3
3
|
```Python
|
4
4
|
|
@@ -6,17 +6,35 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
def has_serial_duplicated_chars(src_str):
|
10
|
+
|
11
|
+
for ch1, ch2 in zip(src_str, src_str[1:]):
|
12
|
+
|
13
|
+
if ch1 == ch2: return True
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
return False
|
18
|
+
|
19
|
+
|
20
|
+
|
9
21
|
def remove_serial_duplicated_chars(src_str):
|
10
22
|
|
11
|
-
ret
|
23
|
+
while has_serial_duplicated_chars(src_str):
|
12
24
|
|
13
|
-
|
25
|
+
src_str = ''.join(
|
14
26
|
|
15
|
-
|
27
|
+
k for k, gen in groupby(src_str)
|
16
28
|
|
17
|
-
)
|
29
|
+
if sum(1 for _ in gen) % 2 == 1
|
18
30
|
|
19
|
-
|
31
|
+
)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
return src_str
|
36
|
+
|
37
|
+
|
20
38
|
|
21
39
|
|
22
40
|
|
@@ -36,11 +54,11 @@
|
|
36
54
|
|
37
55
|
|
38
56
|
|
39
|
-
**実行結果** [Wandbox](https://wandbox.org/permlink/
|
57
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/7fBFYEBOwgRvvQFt)
|
40
58
|
|
41
59
|
```
|
42
60
|
|
43
|
-
|
61
|
+
c
|
44
62
|
|
45
63
|
abac
|
46
64
|
|
@@ -48,46 +66,10 @@
|
|
48
66
|
|
49
67
|
|
50
68
|
|
51
|
-
|
69
|
+
過去の修正について
|
52
70
|
|
53
71
|
---
|
54
72
|
|
55
|
-
|
73
|
+
仕様の誤認が度重なり、二度にわたり大々的に回答を変更しています。
|
56
74
|
|
57
|
-
```Python
|
58
|
-
|
59
|
-
from itertools import groupby
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
75
|
+
本来なら過去の回答に追記する形が良いのでしょうが、可読性を考慮し上書き修正いたしました。
|
64
|
-
|
65
|
-
return ''.join(k for k, _ in groupby(src_str))
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
print(
|
70
|
-
|
71
|
-
remove_serial_duplicated_chars('abbac')
|
72
|
-
|
73
|
-
)
|
74
|
-
|
75
|
-
print(
|
76
|
-
|
77
|
-
remove_serial_duplicated_chars('abbbac')
|
78
|
-
|
79
|
-
)
|
80
|
-
|
81
|
-
```
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
**実行結果** [Wandbox](https://wandbox.org/permlink/VevhtrNLDXMe04y4)
|
86
|
-
|
87
|
-
```
|
88
|
-
|
89
|
-
abac
|
90
|
-
|
91
|
-
abac
|
92
|
-
|
93
|
-
```
|
2
修正
test
CHANGED
@@ -1,8 +1,54 @@
|
|
1
|
-
|
1
|
+
仕様を勘違いして書き始めたもので、かなり強引な解法です。参考までに。
|
2
2
|
|
3
|
-
|
3
|
+
```Python
|
4
|
+
|
5
|
+
from itertools import groupby
|
4
6
|
|
5
7
|
|
8
|
+
|
9
|
+
def remove_serial_duplicated_chars(src_str):
|
10
|
+
|
11
|
+
return ''.join(
|
12
|
+
|
13
|
+
k for k, gen in groupby(src_str)
|
14
|
+
|
15
|
+
if sum(1 for _ in gen) % 2 == 1
|
16
|
+
|
17
|
+
)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
print(
|
24
|
+
|
25
|
+
remove_serial_duplicated_chars('abbac')
|
26
|
+
|
27
|
+
)
|
28
|
+
|
29
|
+
print(
|
30
|
+
|
31
|
+
remove_serial_duplicated_chars('abbbac')
|
32
|
+
|
33
|
+
)
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/e1amUzbJeH95b5gR)
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
aac
|
44
|
+
|
45
|
+
abac
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
以下、当初の回答
|
6
52
|
|
7
53
|
---
|
8
54
|
|
1
修正
test
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
投稿した直後に気付きましたが、私が仕様を勘違いしていたようです。
|
2
|
+
|
3
|
+
失礼しました。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
---
|
8
|
+
|
1
9
|
[itertools.groupby](https://docs.python.jp/3/library/itertools.html#itertools.groupby)を使うのはいかがでしょうか。
|
2
10
|
|
3
11
|
```Python
|