開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、Exercise 15の解答を求めてみる。
コード
#!/usr/bin/env python3
import random
from typing import List
print('5.')
rat1weight: float = float(random.randrange(10, 15))
rat1rate: float = 4 / 100
print('a.')
weeks: int = 0
print(f'weeks: {weeks}\trat1: {rat1weight:.2f}g')
rat1weight_original = rat1weight
while rat1weight < rat1weight_original * 1.25:
rat1weight *= 1 + rat1rate
weeks += 1
print(f'weeks: {weeks}\trat1: {rat1weight:.2f}g')
print('b.')
rat1weight = 10
rat2weight: float = 10
rat2rate: float = 3 / 100
weeks = 0
print(f'{weeks} weeks\trat1: {rat1weight:.2f}g, rat2: {rat2weight:.2f}')
while rat1weight < rat2weight * 1.1:
rat1weight *= 1 + rat1rate
rat2weight *= 1 + rat2rate
weeks += 1
print(f'{weeks} weeks\trat1: {rat1weight:.2f}, rat2: {rat2weight:.2f}')
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% mypy sample15.py
Success: no issues found in 1 source file
% ./sample15.py
5.
a.
weeks: 0 rat1: 11.00g
weeks: 1 rat1: 11.44g
weeks: 2 rat1: 11.90g
weeks: 3 rat1: 12.37g
weeks: 4 rat1: 12.87g
weeks: 5 rat1: 13.38g
weeks: 6 rat1: 13.92g
b.
0 weeks rat1: 10.00g, rat2: 10.00
1 weeks rat1: 10.40, rat2: 10.30
2 weeks rat1: 10.82, rat2: 10.61
3 weeks rat1: 11.25, rat2: 10.93
4 weeks rat1: 11.70, rat2: 11.26
5 weeks rat1: 12.17, rat2: 11.59
6 weeks rat1: 12.65, rat2: 11.94
7 weeks rat1: 13.16, rat2: 12.30
8 weeks rat1: 13.69, rat2: 12.67
9 weeks rat1: 14.23, rat2: 13.05
10 weeks rat1: 14.80, rat2: 13.44
%
0 コメント:
コメントを投稿