開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: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 a = some_array[0, some_array.length] result = [] while a.length > 0 i = rand (a.length - 1) result.push a.slice!(i) 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}" puts "シャッフル後: #{shuffled_a}"
入出力結果(Terminal)
$ ./sample.rb シャッフル前: [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "a", "b", "c", "d", "e"] シャッフル後: ["B", 5, 3, "b", "d", "D", 4, "a", "A", "C", "E", "c", 2, 1, "e"] $ ./sample.rb シャッフル前: [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "a", "b", "c", "d", "e"] シャッフル後: [1, "a", "B", "D", 2, 5, 3, "b", "d", "E", "C", "A", "c", 4, "e"] $ ./sample.rb シャッフル前: [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "a", "b", "c", "d", "e"] シャッフル後: ["A", "b", "B", 3, "C", "D", 4, 1, "E", 2, "a", "c", 5, "d", "e"] $ ./sample.rb シャッフル前: [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "a", "b", "c", "d", "e"] シャッフル後: [4, 2, "a", "d", "C", "B", 1, 3, "A", "c", "E", "D", "b", 5, "e"] $ ./sample.rb シャッフル前: [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "a", "b", "c", "d", "e"] シャッフル後: ["C", "A", 5, "b", "c", "D", "B", "d", 2, "E", 1, "a", 3, 4, "e"] $
ちなみにJavaScriptの場合。
コード(BBEdit)
function shuffle( some_array ) { var a = some_array.slice(), result = [], i; while ( a.length > 0 ) { i = Math.floor(Math.random() * a.length); result.push( a.splice(i, 1)[0] ); } return result; } 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 shuffle( some_l ): l = some_l[:] result = [] while len(l) > 0: i = random.randint(0, len(l) - 1) result.append( l.pop(i) ) return result 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'] シャッフル後: ['B', 'e', 'c', 5, 'd', 3, 'b', 'A', 'C', 'E', 2, 'D', 'a', 4, 1] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['a', 5, 'b', 'e', 'c', 3, 2, 1, 'C', 'A', 'B', 'd', 'E', 'D', 4] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['B', 'c', 2, 4, 3, 'e', 'a', 'A', 'b', 'd', 1, 'C', 'E', 5, 'D'] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: ['a', 'c', 'b', 1, 'B', 5, 4, 'E', 'A', 'D', 'C', 2, 'd', 'e', 3] $ ./sample.py シャッフル前: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e'] シャッフル後: [5, 3, 'e', 'c', 'B', 'C', 'a', 'b', 1, 'D', 'A', 2, 4, 'd', 'E'] $
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 $some_l = shift; my @l = @$some_l; my @result = (); my $i; while (@l) { $i = rand @l; push @result, splice(@l, $i, 1); } @result; } 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 シャッフル後: d E e 5 A 2 4 c C D b B 3 a 1 $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: D 1 4 C b e d 5 B a 2 c 3 E A $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: e d 1 C 4 5 2 b 3 c D A a B E $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: B E c C A 2 5 1 4 3 b D d a e $ ./sample.pl シャッフル前: 1 2 3 4 5 A B C D E a b c d e シャッフル後: 2 B b D 5 c a e d 3 1 E C 4 A $
0 コメント:
コメントを投稿