2014年9月21日日曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 16.を解いてみる。

9.10(Exercises) 16.

コード(BBEdit)

sample16.py

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

rat_1_weight = 1
rat_2_weight = 1
rat_1_rate = 4

print('a.')
weeks = 0
temp = rat_1_weight
rat_1_inc = 1 + rat_1_rate / 100
while temp < rat_1_weight * 1.25:
    temp *= rat_1_inc
    weeks += 1

print(weeks)

print('b.')
rat_2_rate = rat_1_rate - 1
rat_2_inc = 1 + rat_2_rate / 100
weeks = 0

while rat_1_weight < rat_2_weight * 1.1:
    rat_1_weight *= rat_1_inc
    rat_2_weight *= rat_2_inc
    weeks += 1

print('rat_1_weight:{0:.2f}, rat_2_weight:{1:.2f}, weeks:{2}'.format(
    rat_1_weight, rat_2_weight, weeks))

入出力結果(Terminal, IPython)

$ ./sample16.py
a.
6
b.
rat_1_weight:1.48, rat_2_weight:1.34, weeks:10
$

0 コメント:

コメントを投稿