開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 10章(章全部で復習), 10.3(練習問題)シャッフル(非再帰) を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
シャッフル(非再帰)
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9
#-*- coding: utf-8 -*-
def shuffle some_array
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
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 シャッフル後: b 5 e c C 4 a d B 1 A E D 3 2 $ ./sample.rb シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 1 B e c 4 a C 3 E 2 D A b d 5 $ ./sample.rb シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 3 2 A c a d 1 4 e B E D 5 C b $ ./sample.rb シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 2 d E A 3 4 e 1 B 5 c D a C b $ ./sample.rb シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 2 a c e D A b C E d 3 1 B 4 5 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var a = [1,2,3,4,5,'A','B','C','D','E','a','b','c','d','e'],
shuffle = function ( some_array ) {
var a = some_array.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;
}
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 ):
b = a[:]
result = []
while len(b) > 0:
r = random.randint(0, len(b) -1)
result.append(b[r])
del b[r]
return result
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'] シャッフル後: ['d', 'B', 'C', 2, 1, 5, 3, 'a', 'c', 'E', 'e', 'b', 4, 'D', 'A'] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['C', 'A', 'e', 'd', 4, 2, 1, 5, 'c', 'D', 3, 'B', 'a', 'E', 'b'] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['a', 'B', 3, 'C', 2, 'A', 'd', 'e', 1, 'E', 4, 'c', 5, 'D', 'b'] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['D', 'a', 'b', 'c', 'A', 2, 'd', 1, 5, 'B', 3, 'e', 'C', 4, 'E'] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['E', 2, 'B', 'D', 5, 'e', 'a', 'd', 4, 'C', 3, 'b', 'c', 1, '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 shuffle {
my $some_array = shift;
my @a = @$some_array;
my @result = ();
while ( @a ) {
my $r = floor(rand @a);
push @result, $a[$r];
splice @a, $r, 1;
}
@result;
}
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 シャッフル後: b 3 c 4 a A C d B E 2 5 e 1 D $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: C 2 4 e B A 5 1 E d a b 3 D c $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 3 e D E C 5 d A 2 1 b B 4 c a $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: d E B 5 a D 1 A c 2 C 4 e b 3 $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 2 1 D A E B e 5 3 4 a C b d c $
0 コメント:
コメントを投稿