2013年4月5日金曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の5章(リスト)の5.13(練習問題)を解いてみる。

5.13(練習問題).

コード(BBEdit)

sample.py

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

alkaline_earth_metals = [4, 12, 20, 38, 56, 80]

print(alkaline_earth_metals[5])
print(alkaline_earth_metals[-1])
print(len(alkaline_earth_metals))
print(max(alkaline_earth_metals))

half_lives = [87.74, 24110.0, 6537.0, 14.4, 376000.0]
for x in half_lives:
    print(x)

for x in half_lives:
    print(x, end=" ")

print()

country_populations = [1295, 23, 7, 3, 47, 21]

total = 0
for x in country_populations:
    total += x
print(total)

temps = [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6]
print("temps",temps)
temps.sort()
print("temps",temps)

cool_temps = temps[0:2]
warm_temps = temps[2:]
print("cool_temps", cool_temps)
print("warm_temps", warm_temps)

temps_in_celsius = cool_temps + warm_temps
print("temp_in_celsius", temps_in_celsius)

temps_in_fahrenheit = []
for x in temps_in_celsius:
    temps_in_fahrenheit.append( 9 * x / 5 + 32)
print("temps_in_fahrenheit", temps_in_fahrenheit)

alkaline_earth_metals = [[4, 9.012], [12, 24.305], [20, 40.078],[38, 87.62],
    [56, 137.325],[88, 226]]

for x in alkaline_earth_metals:
    print(x[0], x[1])

number_and_weight = []
for x in alkaline_earth_metals:
    number_and_weight.append(x[0])
    number_and_weight.append(x[1])
print(number_and_weight)

alkaline_earth_metals = []
with open('alkaline_metals.txt') as f:
    for line in f:
        x = line.strip().split()
        alkaline_earth_metals.append([int(x[0]), float(x[1])])
print(alkaline_earth_metals)

def mystery_function(values):
    "転置行列を求める関数"
    result = []
    for i in range(len(values[0])):
        result.append([values[0][i]])
        for j in range(1, len(values)):
            result[-1].append(values[j][i])
    return result

l0 = [[]]
l1 = [[1,2]]
l2 = [[1], [2]]
l3 = [[1,2],[3,4]]
l4 = [[1,2,3],[4,5,6]]
l5 = [[1,2],[3,4],[5,6]]
l6 = [[1,2,3],[4,5,6], [7,8,0]]
for x in [l1, l2, l3, l4, l5, l6]:
    print("行列: {0}\n転置行列: {1}".format(x, mystery_function(x)))

入出力結果(Terminal)

$ ./sample.py
80
80
6
80
87.74
24110.0
6537.0
14.4
376000.0
87.74 24110.0 6537.0 14.4 376000.0 
1396
temps [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6]
temps [16.8, 19.6, 22.5, 23.9, 25.2, 28, 31.4]
cool_temps [16.8, 19.6]
warm_temps [22.5, 23.9, 25.2, 28, 31.4]
temp_in_celsius [16.8, 19.6, 22.5, 23.9, 25.2, 28, 31.4]
temps_in_fahrenheit [62.24, 67.28, 72.5, 75.02, 77.36, 82.4, 88.52]
4 9.012
12 24.305
20 40.078
38 87.62
56 137.325
88 226
[4, 9.012, 12, 24.305, 20, 40.078, 38, 87.62, 56, 137.325, 88, 226]
[[4, 9.012], [12, 24.305], [20, 40.078], [38, 87.62], [56, 137.327], [88, 226.0]]
行列: [[1, 2]]
転置行列: [[1], [2]]
行列: [[1], [2]]
転置行列: [[1, 2]]
行列: [[1, 2], [3, 4]]
転置行列: [[1, 3], [2, 4]]
行列: [[1, 2, 3], [4, 5, 6]]
転置行列: [[1, 4], [2, 5], [3, 6]]
行列: [[1, 2], [3, 4], [5, 6]]
転置行列: [[1, 3, 5], [2, 4, 6]]
行列: [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
転置行列: [[1, 4, 7], [2, 5, 8], [3, 6, 0]]
$

0 コメント:

コメントを投稿