2013年10月2日水曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 6章(メソッドの詳細), 6.1(文字列の面白いメソッド)、6.2(練習問題)、目次、を解いてみる。

その他参考書籍

目次

コード(BBEdit)

sample.rb

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

cols = 40
cols_half = cols / 2
puts 'table of contents'.center(cols)
puts
puts 'chapter 1: start'.ljust(cols_half) + 'p.1'.rjust(cols_half)
puts 'chapter 2: number'.ljust(cols_half) + 'p.11'.rjust(cols_half)
puts 'chapter 3: string'.ljust(cols_half) + 'p.15'.rjust(cols_half)

入出力結果(Terminal)

$ ./sample.rb
           table of contents            

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

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

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

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

入出力結果(Terminal)

$ ./sample.py
           table of contents            

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

0 コメント:

コメントを投稿