開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)の15章(スマートマッチとgiven-when)の15.6(練習問題)2.をPythonで考えてみる。
15.6(練習問題)2.
コード(BBEdit, Emacs)
fizz_buzz.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- for num in range(1, 106): result = '' if num % 3 == 0: result += 'Fizz ' if num % 5 == 0: result += 'Bin ' if num % 7 == 0: result += 'Sausage' print('{0}: {1}'.format(num, result))
入出力結果(Terminal, IPython)
$ ./fizz_buzz.py 1: 2: 3: Fizz 4: 5: Bin 6: Fizz 7: Sausage 8: 9: Fizz 10: Bin 11: 12: Fizz 13: 14: Sausage 15: Fizz Bin 16: 17: 18: Fizz 19: 20: Bin 21: Fizz Sausage 22: 23: 24: Fizz 25: Bin 26: 27: Fizz 28: Sausage 29: 30: Fizz Bin 31: 32: 33: Fizz 34: 35: Bin Sausage 36: Fizz 37: 38: 39: Fizz 40: Bin 41: 42: Fizz Sausage 43: 44: 45: Fizz Bin 46: 47: 48: Fizz 49: Sausage 50: Bin 51: Fizz 52: 53: 54: Fizz 55: Bin 56: Sausage 57: Fizz 58: 59: 60: Fizz Bin 61: 62: 63: Fizz Sausage 64: 65: Bin 66: Fizz 67: 68: 69: Fizz 70: Bin Sausage 71: 72: Fizz 73: 74: 75: Fizz Bin 76: 77: Sausage 78: Fizz 79: 80: Bin 81: Fizz 82: 83: 84: Fizz Sausage 85: Bin 86: 87: Fizz 88: 89: 90: Fizz Bin 91: Sausage 92: 93: Fizz 94: 95: Bin 96: Fizz 97: 98: Sausage 99: Fizz 100: Bin 101: 102: Fizz 103: 104: 105: Fizz Bin Sausage $
0 コメント:
コメントを投稿