2014年9月27日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 10(Reading and Writing Files)、10.10(Exercises) 5.を解いてみる。

10.10(Exercises) 5.

コード(BBEdit)

sample5.py

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

import time_series

def smallestValue(reader):
    line = time_series.skipHeader(reader).strip()
    if line != '':
        smallest = int(line)
        for line in reader:
            value = int(line.strip())
            if value < smallest:
                smallest = value
        return smallest

if __name__ == '__main__':
    import sys
    with open(sys.argv[1], 'r') as input_file:
        print(smallestValue(input_file))

入出力結果(Terminal, IPython)

$ cat hopedale.txt 
      22
      29
       2
      16
      12
      35
       8
      83
     166
$ cat temp.txt
# header
$ ./sample5.py hopedale.txt 
2
$ ./sample5.py temp.txt
None
$

0 コメント:

コメントを投稿