2013年11月3日日曜日

開発環境

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

5.13(練習問題)、4から17.

コード(BBEdit)

sample.py

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

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

print('14. {0}'.format(alkaline_earth_metals))

print('15.')
for x in alkaline_earth_metals:
    print('原子番号: {0}, 原子量: {1}'.format(x[0], x[1]))

print('16.')
numbers_and_weight = []
for x in alkaline_earth_metals:
    numbers_and_weight.append(x[0])
    numbers_and_weight.append(x[1])
print(numbers_and_weight)

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

入出力結果(Terminal)

$ ./sample.py
14. [[4, 9.012], [12, 24.305], [20, 40.078], [38, 87.62], [56, 137.327], [88, 226]]
15.
原子番号: 4, 原子量: 9.012
原子番号: 12, 原子量: 24.305
原子番号: 20, 原子量: 40.078
原子番号: 38, 原子量: 87.62
原子番号: 56, 原子量: 137.327
原子番号: 88, 原子量: 226
16.
[4, 9.012, 12, 24.305, 20, 40.078, 38, 87.62, 56, 137.327, 88, 226]
17.
[['4', '9.012'], ['12', '24.305'], ['20', '40.078'], ['38', '87.62'], ['56', '137.327'], ['88', '226']]
$

0 コメント:

コメントを投稿