回答編集履歴

1

コード分離

2017/07/23 04:35

投稿

can110
can110

スコア38266

test CHANGED
@@ -3,6 +3,10 @@
3
3
  空白 or 文字が出現したタイミングで状態を切り替えて処理しています。
4
4
 
5
5
  ```Python
6
+
7
+ # 空白はそのまま、文字は{}で囲んで出力
8
+
9
+
6
10
 
7
11
  # 'word' -> '{word}'
8
12
 
@@ -16,17 +20,9 @@
16
20
 
17
21
 
18
22
 
19
- lines = ['','a\t','\t','\ta','\ ','\ta ','\t a','\ta b','\t aaa bbb cccc dd eeeeee']
23
+ # 'a b' -> '{a} {b}'
20
24
 
21
- for line in lines:
25
+ def quoteWords(line):
22
-
23
- if not line.startswith('\t'):
24
-
25
- continue
26
-
27
- line = line[1:] # 先頭\t除去
28
-
29
-
30
26
 
31
27
  isSpace = True
32
28
 
@@ -36,13 +32,11 @@
36
32
 
37
33
  if s == ' ':
38
34
 
39
- if not isSpace: # 文字→空白への切り替わり
35
+ if not isSpace:
40
36
 
41
37
  line_new += quote(word)
42
38
 
43
39
  word = ''
44
-
45
-
46
40
 
47
41
  line_new += s
48
42
 
@@ -54,11 +48,23 @@
54
48
 
55
49
  isSpace = False
56
50
 
51
+ line_new += quote(word) # 空白以外で終わった場合(積み残し)
52
+
53
+ return line_new
57
54
 
58
55
 
59
- # 空白以外で終わった場合(積み残し)
60
56
 
57
+ lines = ['','a\t','\t','\ta','\ ','\ta ','\t a','\ta b','\t aaa bbb cccc dd eeeeee']
58
+
59
+ for line in lines:
60
+
61
+ if not line.startswith('\t'):
62
+
63
+ continue
64
+
65
+ line = line[1:] # 先頭\t除去
66
+
61
- line_new += quote(word)
67
+ line_new = quoteWords(line)
62
68
 
63
69
 
64
70