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

回答編集履歴

4

テキスト修正

2020/10/01 17:22

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -22,4 +22,25 @@
22
22
 
23
23
  - **動作確認用Repl.it:** [repl.it/@jun68ykt/Q295152](https://repl.it/@jun68ykt/Q295152)
24
24
 
25
- 参考になれば幸いです。
25
+ 参考になれば幸いです。
26
+
27
+ ## 追記
28
+
29
+ 正規表現を使わず、forループで目的の文字列を得るコードを追記します。
30
+
31
+ ```python
32
+ def f(text):
33
+ output = ''
34
+ buff = []
35
+
36
+ for i in range(len(text)+1):
37
+ ch = text[i] if i < len(text) else None
38
+ if len(buff) > 0 and buff[0] != ch:
39
+ output += f'{buff[0]}{len(buff)}'
40
+ buff.clear()
41
+ buff.append(ch)
42
+
43
+ return output
44
+
45
+ ```
46
+ - **動作確認用Repl.it:** [repl.it/@jun68ykt/Q295152-2](https://repl.it/@jun68ykt/Q295152-2)

3

テキスト修正

2020/10/01 17:22

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  # 以下、実行例
14
14
  print(f('jjffir')) # => 'j2f2i1r1'
15
- print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2y1c5b2X2p9z2'
15
+ print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2Y1c5b2X2p9Z2'
16
16
  print(f('u')) # => 'u1'
17
17
  print(f('xxxxxxxxxxxxxxx')) # => 'x15'
18
18
  print(f('abcd')) # => 'a1b1c1d1'

2

テキスト修正

2020/10/01 16:59

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -12,10 +12,12 @@
12
12
 
13
13
  # 以下、実行例
14
14
  print(f('jjffir')) # => 'j2f2i1r1'
15
- print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2Y1c5b2X2p9Z2'
15
+ print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2y1c5b2X2p9z2'
16
16
  print(f('u')) # => 'u1'
17
17
  print(f('xxxxxxxxxxxxxxx')) # => 'x15'
18
+ print(f('abcd')) # => 'a1b1c1d1'
18
19
  print(f('')) # => ''
20
+
19
21
  ```
20
22
 
21
23
  - **動作確認用Repl.it:** [repl.it/@jun68ykt/Q295152](https://repl.it/@jun68ykt/Q295152)

1

テキスト修正

2020/09/30 17:35

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  # 以下、実行例
14
14
  print(f('jjffir')) # => 'j2f2i1r1'
15
- print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2y1c5b2X2p9z2'
15
+ print(f('kkkkrrYcccccbbXXpppppppppZZ')) # => 'k4r2Y1c5b2X2p9Z2'
16
16
  print(f('u')) # => 'u1'
17
17
  print(f('xxxxxxxxxxxxxxx')) # => 'x15'
18
18
  print(f('')) # => ''