Last active
July 8, 2017 09:05
-
-
Save Sunao-Yoshii/0aa5ad5fc855f2dcba6b85ff0a978a1d to your computer and use it in GitHub Desktop.
面倒なことはPython にやらせよう chapter 演習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# コラッツ予想 | |
# コンソールアプリ | |
def collatz(v): | |
if v % 2 == 0: | |
return int(v / 2) | |
else: | |
return v * 3 + 1 | |
while True: | |
try: | |
print("Input start value (end with empty):") | |
value = input() | |
if len(value) == 0: | |
break | |
print("Start collatz") | |
r = int(value) | |
while r != 1: | |
r = collatz(r) | |
print(r) | |
except Exception as e: | |
print("Except error: " + str(e)) | |
print("Was input bad value?") | |
print("Bye!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 任意長の文字のリストを渡して、基本 「,」 でJOINして、最後だけ and 結合する関数 | |
def join_as_and(messages): | |
counts = len(messages) | |
if counts == 0: | |
return "" | |
elif counts == 1: | |
return messages[0] | |
else: | |
last = messages[-1] | |
return ", ".join(messages[:-1]) + " and " + last | |
print(join_as_and(["A", "B", "C"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 次のリストを 90 度反転して表示 | |
love = [[' ',' ',' ',' ',' ',' '], | |
[' ','0','0',' ',' ',' '], | |
['0','0','0','0',' ',' '], | |
['0','0','0','0','0',' '], | |
[' ','0','0','0','0','0'], | |
['0','0','0','0','0',' '], | |
['0','0','0','0',' ',' '], | |
[' ','0','0',' ',' ',' '], | |
[' ',' ',' ',' ',' ',' ']] | |
for x in range(6): | |
for y in range(9): | |
print(love[y][x], end='') | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
items = { | |
'ロープ': 1, | |
'たいまつ': 6, | |
'金貨': 42, | |
'手裏剣': 1, | |
'矢': 12 | |
} | |
def show_items(data): | |
for key in data.keys(): | |
print("%-6s : %d" % (key, data[key])) | |
show_items(items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inventory = { | |
'ロープ': 1, | |
'たいまつ': 6, | |
'金貨': 42, | |
'手裏剣': 1, | |
'矢': 12 | |
} | |
def show_items(data): | |
for key in data.keys(): | |
print("%-6s : %d" % (key, data[key])) | |
def add_to_inventory(inventory, add_items): | |
for itm in add_items: | |
if itm in inventory.keys(): | |
inventory[itm] += 1 | |
else: | |
inventory[itm] = 1 | |
show_items(inventory) | |
add_to_inventory(inventory, ['金貨', '矢']) | |
print('add finished'.ljust(20, '-')) | |
show_items(inventory) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 以下のリストを 4 行 3 列全て右揃えで列挙してください | |
table_data = [ | |
['apple', 'orange', 'cherries', 'banana'], | |
['Alice', 'Bob', 'Carol', 'David'], | |
['dogs', 'cats', 'mouse', 'goose'] | |
] | |
col_length = {} | |
for r in range(3): | |
if r not in col_length.keys(): | |
col_length[r] = 0 | |
for c in range(4): | |
if col_length[r] < len(table_data[r][c]): | |
col_length[r] = len(table_data[r][c]) | |
print(col_length) | |
for c in range(4): | |
for r in range(3): | |
lens = col_length[r] + 1 | |
print(table_data[r][c].rjust(lens, ' '), end='') | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment