2014年9月10日水曜日

開発環境

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) 8-a, b, c.を解いてみる。

9.10(Exercises) 8-a, b, c.

コード(BBEdit)

sample8.py

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

import random

rat_1 = [random.randint(1, 10) for x in range(10)]
rat_2 = [random.randint(1, 10) for x in range(10)]

print('rat_1 = {0}'.format(rat_1))
print('rat_2 = {0}'.format(rat_2))

print('a.')
if rat_1[0] > rat_2[0]:
    print('Rat 1 weighed more than Rat 2 on day 1.')
else:
    print('Rat 1 weighed less than Rat 2 on day 1')

print('b.')
if rat_1[0] > rat_2[0] and rat_1[-1] > rat_2[-1]:
    print('Rat 1 remained heavier than Rat 2')
else:
    print('Rat 2 became heavier than Rat 1')

print('c.')
if rat_1[0] > rat_2[0]:
    if rat_1[-1] > rat_2[-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')

入出力結果(Terminal, IPython)

$ ./sample8.py
rat_1 = [3, 8, 6, 3, 6, 8, 4, 9, 2, 7]
rat_2 = [10, 10, 5, 4, 9, 10, 6, 3, 7, 8]
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
$ ./sample8.py
rat_1 = [9, 4, 4, 10, 7, 2, 7, 8, 8, 7]
rat_2 = [1, 10, 7, 2, 4, 7, 4, 10, 5, 6]
a.
Rat 1 weighed more than Rat 2 on day 1.
b.
Rat 1 remained heavier than Rat 2
c.
Rat 1 remained heavier than Rat 2
$ ./sample8.py
rat_1 = [1, 2, 5, 10, 4, 7, 8, 7, 4, 1]
rat_2 = [10, 10, 5, 7, 5, 10, 2, 9, 6, 9]
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
$ ./sample8.py
rat_1 = [4, 6, 4, 7, 3, 7, 10, 6, 3, 8]
rat_2 = [4, 9, 10, 10, 9, 3, 9, 3, 4, 10]
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
$ ./sample8.py
rat_1 = [7, 2, 10, 10, 1, 5, 4, 10, 9, 9]
rat_2 = [9, 1, 8, 1, 9, 9, 10, 6, 7, 8]
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
$ 

0 コメント:

コメントを投稿