2014年9月25日木曜日

開発環境

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) 3.を解いてみる。

10.10(Exercises) 3.

コード(BBEdit)

sample3.py

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

import sys

def readBackward(file):
    lines = [line for line in file]
    for line in reversed(lines):
        for ch in reversed(line):
            print(ch, end='')
    
with open(sys.argv[1]) as f:
    readBackward(f)
    

入出力結果(Terminal, IPython)

$ ./sample3.py alkaline_metals.txt 

622 88 muidar
723.731 65 muirab
26.78 83 muitnorts
870.02 02 muiclac
503.42 21 muisengam
210.9 4 muillyreb$ cat alkaline_metals.txt 
beryllium 4 9.012
magnesium 12 24.305
calcium 20 20.078
strontium 38 87.62
barium 56 137.327
radium 88 226
$

0 コメント:

コメントを投稿