2013年4月8日月曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の8章(ファイル処理)の8.8(練習問題)を解いてみる。

1.

コード(BBEdit)

sample.py

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

import sys

lines = []
with open(sys.argv[1]) as f:
    for line in f:
        res = ""
        for i in range(-1, -1-len(line), -1):
            res += line[i]
        lines.append(res)

lines.reverse()
for line in lines:
    print(line, end="")
print()

入出力結果(Terminal)

$ ./sample.py sample.py
)(tnirp
)""=dne ,enil(tnirp    
:senil ni enil rof
)(esrever.senil

)ser(dneppa.senil        
]i[enil =+ ser            
:)1- ,)enil(nel-1- ,1-(egnar ni i rof        
"" = ser        
:f ni enil rof    
:f sa )]1[vgra.sys(nepo htiw
][ = senil

sys tropmi

-*- 8-ftu :gnidoc -*-#
3.3nohtyp vne/nib/rsu/!#
$

2.

コード(BBEdit)

sample.py

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

def read_weather_data(r):
    fields = (((4, int), (2, int), (2, int)),
              ((2, int), (2, int), (2, int)),
              ((2, int), (2, int), (2, int)),
              ((6, float), (6, float), (6, float)))
    result = []
    for line in r:
        start = 0
        record = []
        for group in fields:
            data = []
            for width, target_type in group:
                text = line[start:start+width]
                field = target_type(text)
                data.append(field)
                start += width
            record.append(data)
        result.append(record)
    return result

if __name__ == '__main__':
    import sys
    with open(sys.argv[1]) as f:
        for x in read_weather_data(f):
            print(x)

入出力結果(Terminal)

$ cat tmp.txt
12341212121212121212123456123456123456
12341212121212121212123456123456123456
12341212121212121212123456123456123456
12341212121212121212123456123456123456
12341212121212121212123456123456123456
$ ./sample.py tmp.txt
[[1234, 12, 12], [12, 12, 12], [12, 12, 12], [123456.0, 123456.0, 123456.0]]
[[1234, 12, 12], [12, 12, 12], [12, 12, 12], [123456.0, 123456.0, 123456.0]]
[[1234, 12, 12], [12, 12, 12], [12, 12, 12], [123456.0, 123456.0, 123456.0]]
[[1234, 12, 12], [12, 12, 12], [12, 12, 12], [123456.0, 123456.0, 123456.0]]
[[1234, 12, 12], [12, 12, 12], [12, 12, 12], [123456.0, 123456.0, 123456.0]]
$

3.

コード(BBEdit)

sample.py

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

def read_weather_data(r):
    fields1 = ((0, int), (4, int), (6, int),
               (8, int), (10, int), (12, int),
               (14, int), (16, int), (18, int),
               (20, float), (26, float), (32, float))
    
    fields = []
    i = 0
    for n in range(len(fields1) - 1):
        fields.append((fields1[n+1][0] - fields1[n][0], fields1[n][1]))
    fields.append((-1, fields1[len(fields)-1][1]))

    result = []
    for line in r:
        start = 0
        record = []
        for width, target_type in fields:
            if width == -1:
                text = line[start:]
            else:
                text = line[start:start+width]
                start += width
            field = target_type(text)
            record.append(field)
        result.append(record)
    return result

if __name__ == '__main__':
    import sys
    with open(sys.argv[1]) as f:
        for x in read_weather_data(f):
            print(x)

入出力結果(Terminal)

$ ./sample.py tmp.txt
[1234, 12, 12, 12, 12, 12, 12, 12, 12, 123456.0, 123456.0, 123456.0]
[1234, 12, 12, 12, 12, 12, 12, 12, 12, 123456.0, 123456.0, 123456.0]
[1234, 12, 12, 12, 12, 12, 12, 12, 12, 123456.0, 123456.0, 123456.0]
[1234, 12, 12, 12, 12, 12, 12, 12, 12, 123456.0, 123456.0, 123456.0]
[1234, 12, 12, 12, 12, 12, 12, 12, 12, 123456.0, 123456.0, 123456.0]
$

4.

コード(BBEdit)

sample.py

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

import sys

def skip_header(r):
    line = r.readline()
    line = r.readline()
    while line.startswith('#'):
        line = r.readline()
    return line

def smallest_value_skip(r):
    line = skip_header(r).strip()
    
    if not line:
        return 0
        
    smallest = int(line)
    
    for line in r:
        line = line.strip()
        
        if line != '-':
            value = int(line)
            
            if value < smallest:
                smallest = value

    return smallest

if __name__ == "__main__":
    with open(sys.argv[1], "r") as f:
        print(smallest_value_skip(f))

入出力結果(Terminal)

$ ./sample.py tmp.txt
0
$ ./sample.py hebron.txt
55
$ cat tmp.txt
Coloured fox fur production, Hebron, Labrador, 1834-1839
#Source: C. Elton (1942) "Voles, Mice and Lemmings", Oxford Univ. Press
#Table 17, p.265--266
#remark: missing value for 1836
$

5.

コード(BBEdit)

sample.py

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

import sys

def skip_header(r):
    line = r.readline()
    line = r.readline()
    while line.startswith('#'):
        line = r.readline()
    return line

def smallest_value_skip(r):
    line = skip_header(r).strip()
    
    if not line:
        return 0
        
    smallest = int(line)
    
    for line in r:
        line = line.strip()
        
        if line == '-':
            continue # こっちの方が読みやすい印象。好みの問題かも。
        value = int(line)
            
        if value < smallest:
            smallest = value

    return smallest

if __name__ == "__main__":
    with open(sys.argv[1], "r") as f:
        print(smallest_value_skip(f))

入出力結果(Terminal)

$ ./sample.py hebron.txt
55
$

6.

コード(BBEdit)

sample.py

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

def read_molecule(r):
    line = r.readline().strip()
    if not line or line[:4] == "CMNT":
        return None
    # 以下同じ
    key, name = line.split()…

7.

コード(BBEdit)

sample.py

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

def read_molecule(r):
    
    # ここまでは同じ
    # ここから
    i = 1
    while reading:
        line = r.readling()
        if line.startswith('END'):
            reading = False
        else:
            key, num, type, x, y, z = line.split()
            if key != i:
                molecule.append("シリアルナンバーがおかしい")
            molecule.append((type, x, y, z))
    return molecule

0 コメント:

コメントを投稿