2013年11月8日金曜日

開発環境

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

6.5(練習問題)、9.

コード(BBEdit)

sample.py

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

# 新しい単位を追加する時、to_celsius, from_celsiusそれぞれにif文を追加すればいい。
# ということで、新しい単位を追加した時、追加しなければならないif文は2つ。
def to_celsius(t, source):
    if source == 'Kelvin':
        return t - 273.15
    if source == 'Celsius':
        return t
    if source == 'Fahrenheit':
        return(t - 32) * 5 / 9
    if source == 'Rankine':
        return (t - 491.67) * 5 / 9
    if source == 'Delisle':
        return 100 - t * 2 / 3
    if source == 'Newton':
        return t * 100 / 33
    if source == 'Reaumur':
        return t * 5 / 4
    if source == 'Romer':
        return (t - 7.5) * 40 / 21
    raise Exception('{0}は変換できない。'.format(source))

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
    raise Exception('{0}は変換できない。'.format(target))

def temperatures(t, source, target):
    return from_celsius(to_celsius(t, source), target)

print('Comparison')
print('Celsius Fahrenheit Kelvin Rankine Delisle Newton Réaumur Rømer')
for t in range(300, -270, - 10):
    l = list(map(lambda target: temperatures(t, "Celsius", target), 
                ['Fahrenheit', 'Kelvin', 'Rankine', 'Delisle', 'Newton',
                 'Reaumur', 'Romer']))
    print(('{0:7.2f} {1:10.2f} {2:6.2f} {3:7.2f} {4:7.2f} {5:6.2f} {6:7.2f}' +
        '{7:8.2f}').format(t, l[0], l[1], l[2], l[3], l[4], l[5], l[6]))

入出力結果(Terminal)

$ ./sample.py
Comparison
Celsius Fahrenheit Kelvin Rankine Delisle Newton Réaumur Rømer
 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
$

0 コメント:

コメントを投稿