2013年10月8日火曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 8章(配列とイテレータ), 8.3(練習問題)、目次改訂版、を解いてみる。

その他参考書籍

目次改訂版

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby2.0
#-*- coding: utf-8 -*-

contents = [['start', 1], ['string', 11], ['number', 15]]
cols = 40
cols_half = cols / 2

puts 'table of contents'.center(cols)
puts
i = 1
contents.each do |item|
    puts "chapter #{contents.index(item) + 1}: #{item[0]}".ljust(cols_half) +
         "p.#{item[1]}".rjust(cols_half)
end

入出力結果(Terminal)

$ ./sample.rb
           table of contents            

chapter 1: start                     p.1
chapter 2: string                   p.11
chapter 3: number                   p.15
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

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

contents = [['start', 1], ['string', 11], ['number', 15]]
cols = 40
cols_half = cols // 2
print('table of contents'.center(cols))
print()
for content in contents:
    print('chapter {0}: {1}'.format(
              contents.index(content) + 1, content[0]).ljust(cols_half) +
          'p.{0}'.format(content[1]).rjust(cols_half))

入出力結果(Terminal)

$ ./sample.py
           table of contents            

chapter 1: start                     p.1
chapter 2: string                   p.11
chapter 3: number                   p.15
$

0 コメント:

コメントを投稿