2013年11月4日月曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の6章(条件分岐)、6.5(練習問題)、1から4を解いてみる。

6.5(練習問題)、1から4。

コード(BBEdit)

sample.py

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

print('1.')
for x, y in [('True', True and not False), ('True', True or True and False),
          ('True', not True or not False), ('True', True and not 0),
          ('True', 52 < 52.3), ('False', 1 + 52 < 52.3), ('False', 4 != 4.0)]:
    print('{0} {1}'.format(x, y))

print('2.')
print('2つ目の振る舞いの定義の反例(第2被演算子の値(False)になる): {0}'.format(
    True and False))

print('3.')
print('a.')
for x in [True, False]:
    for y in [True, False]:
        print('x = {0}, y = {1}, x and y: {2}'.format(x, y, x and y))

print('b.')
for x in [True, False]:
    print('x = {0}, not x: {1}'.format(x, not x))

print('c.')
for x in [True, False]:
    for y in [True, False]:
        print('x = {0}, y = {1}, x or y: {2}'.format(x, y, x or y))

print('4.')
for full in [True, False]:
    for empty in [True, False]:
        print(('full = {0}, empty = {1}, ' + 
            '(full and not empty) or (not full and  empty): {2}').format(
            full, empty, (full and not empty) or (not full and empty)))

入出力結果(Terminal)

$ ./sample.py
1.
True True
True True
True True
True True
True True
False False
False False
2.
2つ目の振る舞いの定義の反例(第2被演算子の値(False)になる): False
3.
a.
x = True, y = True, x and y: True
x = True, y = False, x and y: False
x = False, y = True, x and y: False
x = False, y = False, x and y: False
b.
x = True, not x: False
x = False, not x: True
c.
x = True, y = True, x or y: True
x = True, y = False, x or y: True
x = False, y = True, x or y: True
x = False, y = False, x or y: False
4.
full = True, empty = True, (full and not empty) or (not full and  empty): False
full = True, empty = False, (full and not empty) or (not full and  empty): True
full = False, empty = True, (full and not empty) or (not full and  empty): True
full = False, empty = False, (full and not empty) or (not full and  empty): False
$

0 コメント:

コメントを投稿