2013年1月26日土曜日

開発環境

『初めてのプログラミング 第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_array
  return shuffled_array if some_array.length == 0
  a = some_array[0, some_array.length]
  i = rand (a.length - 1)
  shuffled_array.push a.slice!(i)
  recursive_shuffle a, shuffled_array
end

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

入出力結果(Terminal)

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

ちなみにJavaScriptの場合。

コード(BBEdit)

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



pythonの場合。

sample.py

コード(BBEdit)

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

import random

def recursive_shuffle( some_l, shuffled_l):
    if len(some_l) == 0: return shuffled_l
    l = some_l[:]
    i = random.randint(0, len(l) - 1)
    shuffled_l.append(l.pop(i))
    return recursive_shuffle(l, shuffled_l)

def shuffle(l):
    return recursive_shuffle(l, [])

l = [1, 2, 3, 4, 5, 'A','B','C','D','E', 'a','b','c','d','e']
shuffled_l = shuffle( l )
print("ソート前: {0}".format( l ))
print("ソート後: {0}".format(shuffled_l))

入出力結果(Terminal)

$ ./sample.py
ソート前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ソート後: ['c', 'a', 1, 4, 5, 'b', 'e', 2, 'C', 3, 'B', 'E', 'A', 'D', 'd']
$ ./sample.py
ソート前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ソート後: [3, 'e', 4, 'D', 'b', 1, 'd', 'a', 'C', 'B', 'A', 5, 2, 'c', 'E']
$ ./sample.py
ソート前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ソート後: [2, 'E', 4, 'b', 'D', 'B', 3, 'A', 'a', 'c', 'e', 'd', 'C', 1, 5]
$ ./sample.py
ソート前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ソート後: ['d', 'E', 'A', 'D', 'B', 3, 'e', 1, 4, 'C', 5, 'a', 2, 'b', 'c']
$ ./sample.py
ソート前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ソート後: ['e', 2, 'D', 'A', 3, 4, 'B', 'd', 5, 'a', 'b', 'C', 'E', 1, 'c']
$

perlの場合。

sample.pl

コード(BBEdit)

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

sub shuffle {
    my $l = shift;
    recursive_shuffle($l, []);
}
sub recursive_shuffle {
    my($some_l, $shuffled_l) = @_;
    return $shuffled_l unless @$some_l;
    my @l = @$some_l;
    my $i = rand @l;
    push @$shuffled_l, splice(@l, $i, 1);
    recursive_shuffle(\@l, $shuffled_l);
}

my @l = (1, 2, 3, 4, 5, 'A','B','C','D','E', 'a','b','c','d','e');
my $shuffled_l = shuffle \@l;
print "ソート前: @l\n";
print "ソート後: @$shuffled_l\n";

入出力結果(Terminal)

$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: 1 C b 3 B 5 2 a 4 c E D e A d
$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: 1 B d b a C 3 E 5 2 e A 4 D c
$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: 2 B C a 5 A b 3 4 c D e d 1 E
$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: E c a 1 D e b A 3 B 5 2 4 d C
$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: B A D C 4 5 d b c E a 3 2 1 e
$ ./sample.pl
ソート前: 1 2 3 4 5 A B C D E a b c d e
ソート後: 2 c E D 5 C a 4 1 A e 3 B b d
$

0 コメント:

コメントを投稿