0
0

# 異様な書き方でプログラミング

Posted at

• `式1 if 条件 else 式2` 構文を使う。
• `式1`,`式2`は複数の式から構成されてもよい。ただし，
• 全体を `( )` で囲む
• それぞれの式は `,` で区切る
• 関数の引用も式である
• 代入文の場合はいわゆるセイウチ演算子 `:=` を使う
• 実行される複数の式の結果をタプルとしてセイウチ演算子で捕捉しておくこともできる
``````n = 976
oddcount, evencount = 0, 0
while n != 1:
(junk1 := (print('n =', n, 'is even. =>', n//2), n := n//2, evencount := evencount + 1)) if n % 2 == 0 else (junk2 := (print('n =', n, 'is odd. =>', n*3 + 1), n := n*3 + 1, oddcount := oddcount + 1))
print('oddcount =', oddcount, 'evencount =', evencount)
print(junk1, junk2)
``````
``````n = 976 is even. => 488
n = 488 is even. => 244
n = 244 is even. => 122
n = 122 is even. => 61
n = 61 is odd. => 184
n = 184 is even. => 92
n = 92 is even. => 46
n = 46 is even. => 23
n = 23 is odd. => 70
n = 70 is even. => 35
n = 35 is odd. => 106
n = 106 is even. => 53
n = 53 is odd. => 160
n = 160 is even. => 80
n = 80 is even. => 40
n = 40 is even. => 20
n = 20 is even. => 10
n = 10 is even. => 5
n = 5 is odd. => 16
n = 16 is even. => 8
n = 8 is even. => 4
n = 4 is even. => 2
n = 2 is even. => 1
oddcount = 5 evencount = 18
(None, 1, 18) (None, 16, 5)
``````

きったね～プログラムになった

0
0
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
0
0