2020年1月3日金曜日

開発環境

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 6、7、8の解答を求めてみる。

コード

#!/usr/bin/env python3
import random
from unittest import TestCase, main
from typing import List

print('6.')

while True:
    text = input("Please enter a chemical formula (or 'quit' to exit): ")
    if text.lower() == 'quit':
        print('…exiting program')
        break
    if text == 'H2O':
        print('Water')
    elif text == 'NH3':
        print('Ammonia')
    else:
        print('Unknown compound')


class MyTestCase(TestCase):

    def test7(self):
        country_populations = [1295, 23, 7, 3, 47, 21]
        total = 0
        for country_population in country_populations:
            total += country_population
        self.assertEqual(total, sum(country_populations))


print('8.')

days = 10
rat1 = [random.randrange(10) for _ in range(days)]
rat2 = [random.randrange(10) for _ in range(days)]
print(f'rat1: {rat1}\nrat2: {rat2}')
print('a.')
if rat1[0] > rat2[0]:
    print('Rat1 weighed more than rat 2 in day1')
else:
    print('Rat 1 weighed less than rat 2 on day 1.')
print('b.')
if rat1[0] > rat2[0] and rat1[-1] > rat2[-1]:
    print('Rat 1 remained heavier than Rat 2.')
else:
    print('Rat 2 became heavier than Rat 1.')
print('c.')
if rat1[0] > rat2[0]:
    if rat1[-1] > rat2[-1]:
        print('Rat 1 remained heavier than Rat 2.')
    else:
        print('Rat 2 became heavier than Rat 1.')
else:
    print('Rat 2 became heavier than Rat 1.')

if __name__ == '__main__':
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample6.py 
6.
Please enter a chemical formula (or 'quit' to exit): quit
…exiting program
8.
rat1: [0, 3, 9, 9, 6, 5, 5, 1, 6, 5]
rat2: [3, 8, 8, 2, 0, 6, 9, 5, 7, 6]
a.
Rat 1 weighed less than rat 2 on day 1.
b.
Rat 2 became heavier than Rat 1.
c.
Rat 2 became heavier than Rat 1.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
% ./sample6.py  
6.
Please enter a chemical formula (or 'quit' to exit): Quit
…exiting program
8.
rat1: [7, 8, 3, 1, 9, 1, 2, 2, 9, 0]
rat2: [7, 0, 0, 3, 5, 7, 0, 0, 4, 0]
a.
Rat 1 weighed less than rat 2 on day 1.
b.
Rat 2 became heavier than Rat 1.
c.
Rat 2 became heavier than Rat 1.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
% ./sample6.py 
6.
Please enter a chemical formula (or 'quit' to exit): QUiT
…exiting program
8.
rat1: [9, 3, 9, 0, 2, 8, 3, 6, 2, 1]
rat2: [4, 5, 7, 0, 5, 3, 8, 9, 8, 9]
a.
Rat1 weighed more than rat 2 in day1
b.
Rat 2 became heavier than Rat 1.
c.
Rat 2 became heavier than Rat 1.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
% ./sample6.py 
6.
Please enter a chemical formula (or 'quit' to exit): NH3
Ammonia
Please enter a chemical formula (or 'quit' to exit): H2O
Water
Please enter a chemical formula (or 'quit' to exit): CO2
Unknown compound
Please enter a chemical formula (or 'quit' to exit): quit
…exiting program
8.
rat1: [5, 1, 8, 9, 1, 3, 5, 8, 0, 5]
rat2: [4, 4, 5, 3, 4, 4, 7, 6, 6, 1]
a.
Rat1 weighed more than rat 2 in day1
b.
Rat 1 remained heavier than Rat 2.
c.
Rat 1 remained heavier than Rat 2.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
% ./sample6.py -v
6.
Please enter a chemical formula (or 'quit' to exit): NH3
Ammonia
Please enter a chemical formula (or 'quit' to exit): H2O
Water
Please enter a chemical formula (or 'quit' to exit): CO2
Unknown compound
Please enter a chemical formula (or 'quit' to exit): quit
…exiting program
8.
rat1: [4, 0, 7, 3, 9, 0, 3, 8, 0, 7]
rat2: [4, 6, 6, 6, 5, 4, 2, 2, 5, 3]
a.
Rat 1 weighed less than rat 2 on day 1.
b.
Rat 2 became heavier than Rat 1.
c.
Rat 2 became heavier than Rat 1.
test7 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
%

0 コメント:

コメントを投稿