2016年2月22日月曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 5.(Playing with Sets and Probability)、Programming Challenges #3: How Many Tosses Before You Run Out of Money?(No. 3622)を解いてみる。

#4: How Many Tosses Before You Run Out of Money?(No. 3622)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import random


toss = [1, -1.5]

if __name__ == '__main__':
    amount = int(input('Enter your starting amount: '))
    count = 0
    while True:
        result = random.randrange(0, 2)
        if result == 0:
            print('Tails! ', end='')
        else:
            print('Heads! ', end='')
        amount += toss[result]
        print('amount: {0}'.format(amount))
        count += 1
        if amount <= 0:
            print('Game over :( Current amount: {0}. Coin tosses : {1}'. \
                  format(amount, count))
            break

入出力結果(Terminal, IPython)

$ ./sample3.py
Enter your starting amount: 10
Tails! amount: 11
Heads! amount: 9.5
Heads! amount: 8.0
Tails! amount: 9.0
Tails! amount: 10.0
Tails! amount: 11.0
Tails! amount: 12.0
Tails! amount: 13.0
Tails! amount: 14.0
Tails! amount: 15.0
Tails! amount: 16.0
Tails! amount: 17.0
Tails! amount: 18.0
Heads! amount: 16.5
Tails! amount: 17.5
Heads! amount: 16.0
Heads! amount: 14.5
Heads! amount: 13.0
Tails! amount: 14.0
Tails! amount: 15.0
Tails! amount: 16.0
Heads! amount: 14.5
Tails! amount: 15.5
Heads! amount: 14.0
Heads! amount: 12.5
Heads! amount: 11.0
Tails! amount: 12.0
Heads! amount: 10.5
Heads! amount: 9.0
Heads! amount: 7.5
Tails! amount: 8.5
Heads! amount: 7.0
Heads! amount: 5.5
Tails! amount: 6.5
Heads! amount: 5.0
Heads! amount: 3.5
Heads! amount: 2.0
Tails! amount: 3.0
Heads! amount: 1.5
Heads! amount: 0.0
Game over :( Current amount: 0.0. Coin tosses : 40
$ ./sample3.py
Enter your starting amount: 10
Tails! amount: 11
Tails! amount: 12
Tails! amount: 13
Heads! amount: 11.5
Heads! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Heads! amount: 8.0
Heads! amount: 6.5
Tails! amount: 7.5
Heads! amount: 6.0
Heads! amount: 4.5
Heads! amount: 3.0
Tails! amount: 4.0
Heads! amount: 2.5
Tails! amount: 3.5
Heads! amount: 2.0
Tails! amount: 3.0
Tails! amount: 4.0
Heads! amount: 2.5
Heads! amount: 1.0
Tails! amount: 2.0
Heads! amount: 0.5
Tails! amount: 1.5
Tails! amount: 2.5
Heads! amount: 1.0
Heads! amount: -0.5
Game over :( Current amount: -0.5. Coin tosses : 27
$ ./sample3.py
Enter your starting amount: 10
Heads! amount: 8.5
Heads! amount: 7.0
Tails! amount: 8.0
Heads! amount: 6.5
Tails! amount: 7.5
Heads! amount: 6.0
Heads! amount: 4.5
Tails! amount: 5.5
Tails! amount: 6.5
Tails! amount: 7.5
Heads! amount: 6.0
Tails! amount: 7.0
Tails! amount: 8.0
Heads! amount: 6.5
Heads! amount: 5.0
Heads! amount: 3.5
Tails! amount: 4.5
Heads! amount: 3.0
Tails! amount: 4.0
Heads! amount: 2.5
Heads! amount: 1.0
Tails! amount: 2.0
Heads! amount: 0.5
Heads! amount: -1.0
Game over :( Current amount: -1.0. Coin tosses : 24
$ ./sample3.py
Enter your starting amount: 10
Heads! amount: 8.5
Tails! amount: 9.5
Tails! amount: 10.5
Tails! amount: 11.5
Tails! amount: 12.5
Tails! amount: 13.5
Heads! amount: 12.0
Tails! amount: 13.0
Heads! amount: 11.5
Heads! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Tails! amount: 10.5
Tails! amount: 11.5
Heads! amount: 10.0
Heads! amount: 8.5
Tails! amount: 9.5
Tails! amount: 10.5
Heads! amount: 9.0
Tails! amount: 10.0
Heads! amount: 8.5
Tails! amount: 9.5
Tails! amount: 10.5
Tails! amount: 11.5
Tails! amount: 12.5
Tails! amount: 13.5
Tails! amount: 14.5
Tails! amount: 15.5
Tails! amount: 16.5
Tails! amount: 17.5
Heads! amount: 16.0
Heads! amount: 14.5
Heads! amount: 13.0
Heads! amount: 11.5
Tails! amount: 12.5
Tails! amount: 13.5
Heads! amount: 12.0
Tails! amount: 13.0
Heads! amount: 11.5
Tails! amount: 12.5
Heads! amount: 11.0
Tails! amount: 12.0
Tails! amount: 13.0
Heads! amount: 11.5
Heads! amount: 10.0
Heads! amount: 8.5
Tails! amount: 9.5
Tails! amount: 10.5
Tails! amount: 11.5
Heads! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Tails! amount: 10.5
Tails! amount: 11.5
Heads! amount: 10.0
Heads! amount: 8.5
Heads! amount: 7.0
Heads! amount: 5.5
Tails! amount: 6.5
Heads! amount: 5.0
Tails! amount: 6.0
Tails! amount: 7.0
Tails! amount: 8.0
Heads! amount: 6.5
Tails! amount: 7.5
Tails! amount: 8.5
Tails! amount: 9.5
Heads! amount: 8.0
Heads! amount: 6.5
Heads! amount: 5.0
Heads! amount: 3.5
Tails! amount: 4.5
Heads! amount: 3.0
Heads! amount: 1.5
Tails! amount: 2.5
Tails! amount: 3.5
Tails! amount: 4.5
Tails! amount: 5.5
Heads! amount: 4.0
Tails! amount: 5.0
Heads! amount: 3.5
Tails! amount: 4.5
Tails! amount: 5.5
Heads! amount: 4.0
Tails! amount: 5.0
Tails! amount: 6.0
Tails! amount: 7.0
Tails! amount: 8.0
Tails! amount: 9.0
Tails! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Heads! amount: 8.0
Tails! amount: 9.0
Tails! amount: 10.0
Heads! amount: 8.5
Tails! amount: 9.5
Heads! amount: 8.0
Heads! amount: 6.5
Tails! amount: 7.5
Heads! amount: 6.0
Heads! amount: 4.5
Tails! amount: 5.5
Heads! amount: 4.0
Tails! amount: 5.0
Tails! amount: 6.0
Heads! amount: 4.5
Tails! amount: 5.5
Heads! amount: 4.0
Heads! amount: 2.5
Tails! amount: 3.5
Tails! amount: 4.5
Tails! amount: 5.5
Tails! amount: 6.5
Heads! amount: 5.0
Tails! amount: 6.0
Heads! amount: 4.5
Tails! amount: 5.5
Tails! amount: 6.5
Tails! amount: 7.5
Heads! amount: 6.0
Heads! amount: 4.5
Tails! amount: 5.5
Tails! amount: 6.5
Heads! amount: 5.0
Heads! amount: 3.5
Tails! amount: 4.5
Heads! amount: 3.0
Tails! amount: 4.0
Heads! amount: 2.5
Heads! amount: 1.0
Heads! amount: -0.5
Game over :( Current amount: -0.5. Coin tosses : 132
$ ./sample3.py
Enter your starting amount: 10
Tails! amount: 11
Tails! amount: 12
Heads! amount: 10.5
Heads! amount: 9.0
Tails! amount: 10.0
Tails! amount: 11.0
Tails! amount: 12.0
Tails! amount: 13.0
Tails! amount: 14.0
Heads! amount: 12.5
Tails! amount: 13.5
Heads! amount: 12.0
Heads! amount: 10.5
Heads! amount: 9.0
Tails! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Tails! amount: 10.5
Heads! amount: 9.0
Tails! amount: 10.0
Heads! amount: 8.5
Tails! amount: 9.5
Heads! amount: 8.0
Tails! amount: 9.0
Heads! amount: 7.5
Tails! amount: 8.5
Tails! amount: 9.5
Heads! amount: 8.0
Tails! amount: 9.0
Tails! amount: 10.0
Tails! amount: 11.0
Heads! amount: 9.5
Heads! amount: 8.0
Heads! amount: 6.5
Heads! amount: 5.0
Tails! amount: 6.0
Tails! amount: 7.0
Heads! amount: 5.5
Tails! amount: 6.5
Heads! amount: 5.0
Heads! amount: 3.5
Heads! amount: 2.0
Tails! amount: 3.0
Heads! amount: 1.5
Tails! amount: 2.5
Tails! amount: 3.5
Heads! amount: 2.0
Heads! amount: 0.5
Tails! amount: 1.5
Heads! amount: 0.0
Game over :( Current amount: 0.0. Coin tosses : 50
$ 

0 コメント:

コメントを投稿

Comments on Google+: