2014年9月5日金曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の6章(データ構造と動的メモリ: 架け橋を築く)、自分で考えてみよう(p.289)をpythonで考えてみる。

自分で考えてみよう(p.289)

コード(BBEdit, Emacs)

sample289.py

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

# GC(ガベージコレクション)があるから、特に修正は無し

class Island:
    def __init__(self, name, opens, closes, next=None):
        self.name = name
        self.opens = opens
        self.closes = closes
        self.next = next
    
    def display(self):
        print('名前:{0}\n 営業時間:{1}-{2}'.format(
            self.name, self.opens, self.closes))
        if self.next:
            self.next.display()

start = None
i = None
next = None

while True:
    try:
        name = input()
        next = Island(name, '9:00', '17:00')
        if not start:
            start = next
        else:
            i.next = next
        i = next
    except EOFError:
        if start:
            start.display()
        break

入出力結果(Terminal, IPython)

$ ./sample289.py
アトランティス
ティッチマーシュ島
名前:アトランティス
 営業時間:9:00-17:00
名前:ティッチマーシュ島
 営業時間:9:00-17:00
$

0 コメント:

コメントを投稿