2013年3月14日木曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 13章(新しいクラスの作成と既存クラスの変更), 13.1(練習問題)組み込みクラスの拡張 を解いてみる。

その他参考書籍

組み込みクラスの拡張

コード(BBEdit)

sample.rb

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

class Array
    def shuffle
        some_array = self
        result = []
        a = some_array[0..some_array.length - 1]
        while a.length > 0
            r = rand a.length
            result.push a[r]
            a.delete_at r
        end
        result
    end
end

class Integer
    @@facs = {0 => 1,1 => 1}
    @@romans = {}
    def factorial
        n = self
        return "負の数" if n < 0
        return @@facs[n] if @@facs[n]
        @@facs[n] = n * ( n - 1 ).factorial
    end
    def to_roman
        n = self
        return @@romans[n] if @@romans[n]
        result = 'M' * (n / 1000)
        n = n % 1000
        h = n / 100
        n = n % 100
        t = n / 10
        n = n % 10
        o = n
        if h == 9
            result += 'CM'
        elsif h == 4
            result += 'CD'
        else
            result += 'D' * (h / 5)
            result += 'C' * (h % 5)
        end
        if t == 9
            result += 'XC'
        elsif t == 4
            result += 'XL'
        else
            result += 'L' * (t / 5)
            result += 'X' * (t % 5)
        end
        if o == 9
            result += 'IX'
        elsif o == 4
            result += 'IV'
        else
            result += 'V' * (o / 5)
            result += 'I' * (o % 5)
        end
        @@romans[n] = result
    end
end

a = [1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e']
5.times do 
    shuffled_a = a.shuffle
    puts "シャッフル前: #{a.join " "}"
    puts "シャッフル後: #{shuffled_a.join " "}"
end
n = 10
puts "#{n}! = #{n.factorial}"
puts 4.to_roman
puts 1234.to_roman

入出力結果(Terminal)

$ ./sample.rb
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: 3 c 4 5 b 2 A 1 d e D B a C E
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: 4 D C 3 b 1 E c d A B e 5 2 a
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: a D 5 e 2 d b c 4 1 C 3 B A E
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: C a 3 d 4 2 D e B 1 A b c 5 E
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: D B e 3 c 4 2 C 5 d 1 E a A b
10! = 3628800
IV
MCCXXXIV
$

ちなみにJavaScriptの場合。

コード(BBEdit)

Array.prototype.shuffle = function(  ) {
        var a = this.valueOf().slice(0),
            result = [],
            r;
        while ( a.length > 0 ) {
            r = Math.floor(Math.random() * a.length);
            result.push(a[r]);
            a.splice(r, 1);
        }
        return result;
};
Number.prototype.facs = {0:1, 1:1};
Number.prototype.factorial = function( ) {
    var n = this.valueOf();
    if((n).facs[n]) {
        return (n).facs[n];
    }
    (n).facs[n] = n * (n - 1).factorial();
    return (n).facs[n];
};
Number.prototype.to_roman = function (  ) {
    var n = this.valueOf(),
        result = new Array(Math.floor(n / 1000)  + 1).join('M'),
        h, t, o;
    n %= 1000;
    h = Math.floor( n / 100);
    n %= 100;
    t = Math.floor(n / 10);
    n %= 10;
    o = n;
    switch ( h ) {
        case 9:
            result += 'CM';
            break;
        
        case 4:
            result += 'CD';
            break;
        
        default:
            result += new Array(Math.floor(h / 5) + 1).join('D');
            result += new Array(h % 5 + 1).join('C');
    }
    switch ( t ) {
        case 9:
            result += 'XC';
            break;
        
        case 4:
            result += 'XL';
            break;
        
        default:
            result += new Array(Math.floor( t / 5) + 1).join('L');
            result += new Array(Math.floor( t % 5 ) + 1).join('X');
    }
    switch ( o ) {
        case 9:
            result += 'IX';
            break;
        
        case 4:
            result += 'IV';
            break;
        
        default:
            result += new Array(Math.floor( o / 5 ) + 1).join('V');
            result += new Array(Math.floor( o % 5 ) + 1).join('I');
    }
    return result;
};
var a = [1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e'],
    shuffled_a = a.shuffle(),
    n = 10,
    result = "シャッフル前: " + a.join(" ") + "\n" +
        "シャッフル後: " + shuffled_a.join(" ") + "\n" +
        n + "! = " + n.factorial() + "\n" +
        (4).to_roman() + "\n" +
        (1234).to_roman() + "\n";
$('#pre0').text(result);













						

0 コメント:

コメントを投稿