2012年12月14日金曜日

開発環境

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

その他参考書籍

目次改訂版

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

contents = [["start", 1],
            ["number", 11],
            ["string", 15]]

width = 40
puts "table of contents".center(width)
puts
i = 1
contents.each do |content|
  puts "chapter #{i}: #{content[0]}".ljust(width / 2) +
    "p.#{content[1]}".rjust(width / 2)
  i += 1
end

入出力結果(Terminal)

$ ./sample.rb
           table of contents            

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

ちなみにJavaScriptの場合。

コード(TextWrangler)

var result = "";
String.prototype.center = function(n){
  var result = "";
  for(var i = 0; i < (n - this.length) / 2; i++){
    result += " ";
  }
  result += this;
  return result;
};
String.prototype.ljust = function(n){
  var result = this;
  for(var i = 0; i < n - this.length; i++){
    result += " ";
  }
  return result;
};
String.prototype.rjust = function(n){
  var result = "";
  for(var i = 0; i < n - this.length; i++){
    result += " ";
  }
  result += this;
  return result;
};
var contents = [["start", 1],
               ["number", 11],
               ["string", 15]];
var width = 40;
result += "table of contents".center(width) + "\n\n";
for(var i = 0; i < contents.length; i++){
  result += 
    ("chapter " + (i + 1) + ": " + contents[i][0]).ljust(width / 2) + 
    ("p." + contents[i][1]).rjust(width / 2) + "\n";
}
$('#pre0').text(result);


pythonの場合。

sample.py

コード(TextWrangler)

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

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

入出力結果(Terminal)

$ ./sample.py
           table of contents            
chapter 1: start                     p.1
chapter 2: number                   p.11
chapter 3: string                   p.15
$

perlの場合。

sample.pl

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';

my %contents = (1 => "start",
                11 => "number",
                15 => "string");
my $width = 40;
my $str = "table of contents";
print " " x (($width - length $str) / 2) . "$str\n";
print "\n";
my $i = 1;
my $half = $width / 2;
for(sort keys %contents){
  printf "%-${half}s%${half}s\n", "chapter " . $i . ": " . $contents{$_}, 
    "p." . $_;
  $i += 1;
}

入出力結果(Terminal)

$ ./sample.pl
           table of contents

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

なんかJavaScriptとPerlですっきり書けた気がしない。。標準の関数とかモジュールとかでもっとすっきり書く方法あるのかな〜

0 コメント:

コメントを投稿