開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の6章(条件分岐)の6.5(練習問題)を解いてみる。
1, 2, 3, 4, 5, 6, 7, 8.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- l = [(True, True), (True, False), (False, True), (False, False)] for x, y in l: print("x = {0}, y = {1}, x and y = {2}".format(x, y, x and y)) for x, y in l: print("x = {0}, not x = {1}".format(x, not x)) for x, y in l: print("x = {0}, y = {1}, x or y = {2}".format(x, y, x or y)) for full, empty in l: 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))) for x in [10, -10]: print("{0} == abs({0}): ".format(x), end="") if x == abs(x): print(True) else: print(False) def different(a, b): if a != b: return True return False a = 10 b = 20 for x, y in [(a, a), (a, b), (b, a), (b, b)]: print("different({0}, {1}): {2}".format(x, y, different(x, y))) population_and_land_area = [(10000000, 999999), (20000000, 20000), (35000000, 100)] for population, land_area in population_and_land_area: print("population = {0}, land_area = {1}".format(population, land_area)) if population <= 10000000: print("a") if 10000000 < population < 35000000: print("b") a = population / land_area if a > 100: print("{0} c: 過密".format(a)) if a > 100: print("{0} d: 過密".format(a)) else: print("{0} d: 過疎".format(a))
入出力結果(Terminal)
$ python Python 3.3.0 (default, Sep 29 2012, 08:16:08) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> True and not False # True True >>> True or True and False # True True >>> not True or not False # True True >>> True and not 0 # True True >>> 52 < 52.3 # True True >>> 1 + 52 < 52.3 # False False >>> 4 != 4.0 # False False >>> True and False False >>> x = True >>> quit() $ ./sample.py 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 x = True, not x = False x = True, not x = False x = False, not x = True x = False, not x = True 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 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 10 == abs(10): True -10 == abs(-10): False different(10, 10): False different(10, 20): True different(20, 10): True different(20, 20): False population = 10000000, land_area = 999999 a 10.00001000001 d: 過疎 population = 20000000, land_area = 20000 b 1000.0 c: 過密 1000.0 d: 過密 population = 35000000, land_area = 100 350000.0 c: 過密 350000.0 d: 過密 $
9.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- # ディクショナリを使えばもっとすっきり書けるけど、本章までには # まだ出てきてない… def to_celsius(t, source): if source == "Kelvin": return t - 273.15 if source == "Celsius": return t if source == "Fahreheit": return (t - 32) * 5 / 9 if source == "Rankine": return (t - 491.67) * 5 / 9 if source == "Delisle": return 100 - t * 3 / 2 if source == "Newton": return t * 100 / 33 if source == "Reaumur": return t * 5 / 4 if source == "Romer": return (t - 7.5) * 40 / 21 def from_celsius(t, target): if target == "Kelvin": return t + 273.15 if target == "Celsius": return t if target == "Fahrenheit": return t * 9 / 5 + 32 if target == "Rankine": return (t + 273.15) * 9 / 5 if target == "Delisle": return (100 - t) * 3 / 2 if target == "Newton": return t * 33 / 100 if target == "Reaumur": return t * 4 / 5 if target == "Romer": return t * 21 / 40 + 7.5 def convert_temperatures(t, source, target): return from_celsius(to_celsius(t, source), target) l = ["Fahrenheit","Kelvin","Rankine", "Delisle", "Newton", "Reaumur","Romer"] print("{0:10s} ".format("Celsius"), end="") for x in l: print("{0} ".format(x.center(10)), end="") print() for x in range(300, -261, -10): print("{0: 10.2f} ".format(x), end="") for y in l: if y != "Celsius": print("{0:10.2f} ".format(convert_temperatures(x, "Celsius", y)), end="") print() a = -459.67 print("Absolute zero") for x in l: print("{0}: {1:.2f}".format(x, convert_temperatures(a, "Fahreheit", x)))
入出力結果(Terminal)
$ ./sample.py Celsius Fahrenheit Kelvin Rankine Delisle Newton Reaumur Romer 300.00 572.00 573.15 1031.67 -300.00 99.00 240.00 165.00 290.00 554.00 563.15 1013.67 -285.00 95.70 232.00 159.75 280.00 536.00 553.15 995.67 -270.00 92.40 224.00 154.50 270.00 518.00 543.15 977.67 -255.00 89.10 216.00 149.25 260.00 500.00 533.15 959.67 -240.00 85.80 208.00 144.00 250.00 482.00 523.15 941.67 -225.00 82.50 200.00 138.75 240.00 464.00 513.15 923.67 -210.00 79.20 192.00 133.50 230.00 446.00 503.15 905.67 -195.00 75.90 184.00 128.25 220.00 428.00 493.15 887.67 -180.00 72.60 176.00 123.00 210.00 410.00 483.15 869.67 -165.00 69.30 168.00 117.75 200.00 392.00 473.15 851.67 -150.00 66.00 160.00 112.50 190.00 374.00 463.15 833.67 -135.00 62.70 152.00 107.25 180.00 356.00 453.15 815.67 -120.00 59.40 144.00 102.00 170.00 338.00 443.15 797.67 -105.00 56.10 136.00 96.75 160.00 320.00 433.15 779.67 -90.00 52.80 128.00 91.50 150.00 302.00 423.15 761.67 -75.00 49.50 120.00 86.25 140.00 284.00 413.15 743.67 -60.00 46.20 112.00 81.00 130.00 266.00 403.15 725.67 -45.00 42.90 104.00 75.75 120.00 248.00 393.15 707.67 -30.00 39.60 96.00 70.50 110.00 230.00 383.15 689.67 -15.00 36.30 88.00 65.25 100.00 212.00 373.15 671.67 0.00 33.00 80.00 60.00 90.00 194.00 363.15 653.67 15.00 29.70 72.00 54.75 80.00 176.00 353.15 635.67 30.00 26.40 64.00 49.50 70.00 158.00 343.15 617.67 45.00 23.10 56.00 44.25 60.00 140.00 333.15 599.67 60.00 19.80 48.00 39.00 50.00 122.00 323.15 581.67 75.00 16.50 40.00 33.75 40.00 104.00 313.15 563.67 90.00 13.20 32.00 28.50 30.00 86.00 303.15 545.67 105.00 9.90 24.00 23.25 20.00 68.00 293.15 527.67 120.00 6.60 16.00 18.00 10.00 50.00 283.15 509.67 135.00 3.30 8.00 12.75 0.00 32.00 273.15 491.67 150.00 0.00 0.00 7.50 -10.00 14.00 263.15 473.67 165.00 -3.30 -8.00 2.25 -20.00 -4.00 253.15 455.67 180.00 -6.60 -16.00 -3.00 -30.00 -22.00 243.15 437.67 195.00 -9.90 -24.00 -8.25 -40.00 -40.00 233.15 419.67 210.00 -13.20 -32.00 -13.50 -50.00 -58.00 223.15 401.67 225.00 -16.50 -40.00 -18.75 -60.00 -76.00 213.15 383.67 240.00 -19.80 -48.00 -24.00 -70.00 -94.00 203.15 365.67 255.00 -23.10 -56.00 -29.25 -80.00 -112.00 193.15 347.67 270.00 -26.40 -64.00 -34.50 -90.00 -130.00 183.15 329.67 285.00 -29.70 -72.00 -39.75 -100.00 -148.00 173.15 311.67 300.00 -33.00 -80.00 -45.00 -110.00 -166.00 163.15 293.67 315.00 -36.30 -88.00 -50.25 -120.00 -184.00 153.15 275.67 330.00 -39.60 -96.00 -55.50 -130.00 -202.00 143.15 257.67 345.00 -42.90 -104.00 -60.75 -140.00 -220.00 133.15 239.67 360.00 -46.20 -112.00 -66.00 -150.00 -238.00 123.15 221.67 375.00 -49.50 -120.00 -71.25 -160.00 -256.00 113.15 203.67 390.00 -52.80 -128.00 -76.50 -170.00 -274.00 103.15 185.67 405.00 -56.10 -136.00 -81.75 -180.00 -292.00 93.15 167.67 420.00 -59.40 -144.00 -87.00 -190.00 -310.00 83.15 149.67 435.00 -62.70 -152.00 -92.25 -200.00 -328.00 73.15 131.67 450.00 -66.00 -160.00 -97.50 -210.00 -346.00 63.15 113.67 465.00 -69.30 -168.00 -102.75 -220.00 -364.00 53.15 95.67 480.00 -72.60 -176.00 -108.00 -230.00 -382.00 43.15 77.67 495.00 -75.90 -184.00 -113.25 -240.00 -400.00 33.15 59.67 510.00 -79.20 -192.00 -118.50 -250.00 -418.00 23.15 41.67 525.00 -82.50 -200.00 -123.75 -260.00 -436.00 13.15 23.67 540.00 -85.80 -208.00 -129.00 Absolute zero Fahrenheit: -459.67 Kelvin: 0.00 Rankine: 0.00 Delisle: 559.72 Newton: -90.14 Reaumur: -218.52 Romer: -135.90 $
10.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- def f(ph): if ph < 3.0: print("{0}は強酸性です。注意してください。".format(ph)) elif ph < 7.0: print("{0}は酸性です。".format(ph)) for ph in range(10): f(ph + 0.5)
入出力結果(Terminal)
$ ./sample.py 0.5は強酸性です。注意してください。 1.5は強酸性です。注意してください。 2.5は強酸性です。注意してください。 3.5は酸性です。 4.5は酸性です。 5.5は酸性です。 6.5は酸性です。 $
11.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- def f(ph): if ph < 4.0: print("{0}は強酸性です。".format(ph)) elif ph < 7.0: print("{0}は酸性です。".format(ph)) while True: ph = input("ph値を入力: ") if ph == "": break ph = float(ph) f(ph) def g(ph): if ph < 4.0: print("{0}は強酸性です。".format(ph)) if ph < 7.0: print("{0}は酸性です。".format(ph)) while True: ph = input("ph値を入力: ") if ph == "": break ph = float(ph) g(ph)
入出力結果(Terminal)
$ ./sample.py ph値を入力: 6.4 6.4は酸性です。 ph値を入力: 3.6 3.6は強酸性です。 ph値を入力: ph値を入力: 3.6 3.6は強酸性です。 3.6は酸性です。 ph値を入力: $
12.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- table = [['高', '中'], ['中', '低']] while True: age = input("年齢: ") bmi = input("bmi: ") if age == "" or bmi == "": break age = int(age) bmi = float(bmi) young = age < 45 light = bmi < 22.0 risk = table[young][light] print(risk)
入出力結果(Terminal)
$ ./sample.py 年齢: 40 bmi: 20 低 年齢: 40 bmi: 22 中 年齢: 45 bmi: 20 中 年齢: 45 bmi: 22 高 年齢: bmi: $
0 コメント:
コメントを投稿