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

回答編集履歴

3

修正

2018/06/28 08:23

投稿

hayataka2049
hayataka2049

スコア30939

answer CHANGED
@@ -18,8 +18,8 @@
18
18
 
19
19
  ```python
20
20
  s = "!?+!!??+!!??"
21
- convert_dict = {"!":1, "?":10}
21
+ conv_dict = {"!":1, "?":10}
22
- taisyou = [convert_dict.get(i, i)
22
+ taisyou = [conv_dict.get(i, i) # get(i, i)でまずconv_dict[i]を試み、存在しなければiを返す
23
23
  for i in s
24
24
  if i != "+"]
25
25
  print(taisyou)
@@ -32,7 +32,7 @@
32
32
  s = "!?+!-!??-+!-!??"
33
33
  conv_dict = {"!":1, "?":10}
34
34
  exc_set = {"+", "-"}
35
- taisyou = [convert_dict.get(i, i)
35
+ taisyou = [conv_dict.get(i, i)
36
36
  for i in s
37
37
  if i not in exc_set]
38
38
  print(taisyou)

2

ちょっといじる

2018/06/28 08:23

投稿

hayataka2049
hayataka2049

スコア30939

answer CHANGED
@@ -30,10 +30,11 @@
30
30
 
31
31
  ```python
32
32
  s = "!?+!-!??-+!-!??"
33
- convert_dict = {"!":1, "?":10}
33
+ conv_dict = {"!":1, "?":10}
34
+ exc_set = {"+", "-"}
34
35
  taisyou = [convert_dict.get(i, i)
35
36
  for i in s
36
- if i not in {"+", "-"}]
37
+ if i not in exc_set]
37
38
  print(taisyou)
38
39
  print(sum(taisyou))
39
40
  ```

1

小修正

2018/06/28 08:21

投稿

hayataka2049
hayataka2049

スコア30939

answer CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
  ということで、こんな感じでどうですか。
5
+ その他ならiにするという規定がなければ、条件演算子をもう1つ減らせます。
6
+
5
7
  ```python
6
8
  s = "!?+!!??+!!??"
7
9
  taisyou = [1 if i =="!" else 10 if i == "?" else i
@@ -11,7 +13,6 @@
11
13
  print(sum(taisyou))
12
14
  ```
13
15
 
14
- その他ならiにするという規定がなければ、条件演算子をもう1つ減らせます。
15
16
 
16
17
  でも、そもそも条件演算子で書くのは悪夢なので、
17
18