2014年6月12日木曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 5(Comprehending data: Work that data)、SHARPEN YOUR PENCIL(p.157)を解いてみる。

SHARPEN YOUR PENCIL(p.157)

コード(BBEdit)

sample157.py

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

def sanitize(time_string):
    if '-' in time_string:
        spliter = '-'
    elif ':' in time_string:
        spliter = ':'
    else:
        return (time_string)
    (mins, secs) = time_string.split(spliter)
    return (mins + '.' + secs)
    
with open('james.txt') as f_james, open('sarah.txt') as f_sarah, \
     open('julie.txt') as f_julie, open('mikey.txt') as f_mikey:
    james = f_james.readline().strip().split(',')
    sarah = f_sarah.readline().strip().split(',')
    julie = f_julie.readline().strip().split(',')
    mikey = f_mikey.readline().strip().split(',')

print(sorted([sanitize(time_string) for time_string in james]))
print(sorted([sanitize(time_string) for time_string in sarah]))
print(sorted([sanitize(time_string) for time_string in julie]))
print(sorted([sanitize(time_string) for time_string in mikey]))

入出力結果(Terminal)

$ ./sample157.py
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
$

0 コメント:

コメントを投稿