2013年3月5日火曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 10章(章全部で復習), 10.3(練習問題)シャッフル(再帰) を解いてみる。

その他参考書籍

シャッフル(再帰)

コード(BBEdit)

sample.rb

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

def shuffle some_array
    recursive_shuffle some_array, []
end

def recursive_shuffle some_array, shuffled
    return shuffled if some_array.length == 0
    a = some_array[0..some_array.length - 1]
    r = rand a.length
    shuffled.push a[r]
    a.delete_at r
    recursive_shuffle a, shuffled
end

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

入出力結果(Terminal)

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

ちなみにJavaScriptの場合。

コード(BBEdit)

var a = [1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e'],
    recursiveShuffle = function ( some_array, shuffled ) {
        if (some_array.length === 0) {
            return shuffled;
        }
        var a = some_array.slice(0),
            r = Math.floor(Math.random() * a.length);
        shuffled.push(a[r]);
        a.splice(r, 1);
        return recursiveShuffle(a, shuffled);
    },
    shuffle = function ( some_array ) {
        return recursiveShuffle(some_array, []);
    },
    shuffled_a = shuffle( a ),
    result = "シャッフル前: " + a.join(" ") + "\n" +
        "シャッフル後: " + shuffled_a.join(" ");
$('#pre0').text(result);


pythonの場合。

sample.py

コード(BBEdit)

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

import random

def shuffle( a, shuffled = [] ):
    if len(a) == 0:
        return shuffled
    b = a[:]
    r = random.randint(0, len(b) - 1)
    shuffled.append(b[r])
    del b[r]
    return shuffle( b, shuffled )

a = [1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e']
shuffled = shuffle( a )
for x, y in [("シャッフル前", a), ("シャッフル後", shuffled)]:
    print("{0}: {1}".format(x, y))

入出力結果(Terminal)

$ ./sample.py
シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
シャッフル後: ['a', 'B', 'D', 'e', 'A', 'b', 'C', 5, 4, 2, 'd', 'c', 3, 'E', 1]
$ ./sample.py
シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
シャッフル後: [3, 1, 'a', 2, 'c', 'E', 'C', 'B', 4, 'A', 'b', 'e', 5, 'D', 'd']
$ ./sample.py
シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
シャッフル後: ['d', 4, 'c', 'B', 5, 'C', 'b', 'a', 1, 2, 'E', 'e', 'A', 'D', 3]
$ ./sample.py
シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
シャッフル後: ['c', 'D', 'C', 5, 'b', 'B', 2, 'd', 1, 4, 'e', 3, 'E', 'a', 'A']
$ ./sample.py
シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
シャッフル後: [2, 'B', 'C', 'D', 'd', 3, 1, 'a', 4, 5, 'e', 'c', 'E', 'b', 'A']
$

perlの場合。

sample.pl

コード(BBEdit)

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

sub recursiveShuffle {
    my($some_array, $shuffled) = @_;
    return @$shuffled unless @$some_array;
    my @a = @$some_array;
    my $r = floor(rand @a);
    push $shuffled, $a[$r];
    splice @a, $r, 1;
    recursiveShuffle( \@a, $shuffled);
}
sub shuffle {
    my $some_array = shift;
    recursiveShuffle($some_array, []);
}
my @a = (1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e');
my @b = shuffle \@a;
print "シャッフル前: @a\n";
print "シャッフル後: @b\n";

入出力結果(Terminal)

$ ./sample.pl
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: C e 4 1 A B d 5 3 a c b 2 D E
$ ./sample.pl
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: 5 1 3 e b C c E D 4 B a d A 2
$ ./sample.pl
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: c d E a D A e 3 B 5 4 b 2 1 C
$ ./sample.pl
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: 4 3 E d D 2 A a B b C c 1 e 5
$ ./sample.pl
シャッフル前: 1 2 3 4 5 A B C D E a b c d e
シャッフル後: e 3 E a 5 d 2 4 D b A c C B 1
$

0 コメント:

コメントを投稿