2013年3月7日木曜日

開発環境

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

その他参考書籍

辞書順ソート(再帰)

コード(BBEdit)

sample.rb

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

def dictionary_sort some_array
    recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array
    return sorted_array if unsorted_array.length == 0
    a = unsorted_array[0..unsorted_array.length]
    min = a.pop
    tmp = []
    a.each do |item|
        if min.to_s.upcase <= item.to_s.upcase
            tmp.push item
        else
            tmp.push min
            min = item
        end
    end
    sorted_array.push min
    recursive_sort tmp, sorted_array
end

a = %w[e a d b c E A D B C]
sorted_a = dictionary_sort a
puts "辞書順ソート前: #{a.join " "}"
puts "辞書順ソート後: #{sorted_a.join " "}"

入出力結果(Terminal)

$ ./sample.rb
辞書順ソート前: e a d b c E A D B C
辞書順ソート後: a A b B C c D d E e
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var a = ['e','a','d','b','c','E','A','D','B','C'],
    dictionarySort = function ( some_array ) {
        return recursiveSort(some_array, []);
    },
    recursiveSort = function ( unsorted_array, sorted_array ) {
        if (unsorted_array.length === 0) {
            return sorted_array;
        }
        var a = unsorted_array.slice(0),
            min = a.pop(),
            tmp = [],
            i, max;
        for (i = 0, max = a.length; i < max; i += 1) {
            if ( (min).toString().toUpperCase() <= 
                 (a[i]).toString().toUpperCase() ) {
                tmp.push( a[i] );
            } else {
                tmp.push( min );
                min = a[i];
            }
        }
        sorted_array.push(min);
        return recursiveSort(tmp, sorted_array);
    },
    sorted_a = dictionarySort( a ),
    result = "辞書順ソート前: " + a.join(" ") + "\n" +
             "辞書順ソート後: " + sorted_a.join(" ");
$('#pre0').text(result);


pythonの場合。

sample.py

コード(BBEdit)

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

def dictionarySort(a):
    return recursiveSort(a, [])

def recursiveSort(unsorted, a):
    if len(unsorted) == 0:
        return a
    b = unsorted[:]
    tmp = []
    min = b.pop()
    for x in b:
        if str(min).upper() <= str(x).upper():
            tmp.append(x)
        else:
            tmp.append(min)
            min = x
    a.append(min)
    return recursiveSort(tmp, a)

a = ['e','a','d','b','c', 'E','A','D','B','C']
b = dictionarySort(a)
for x, y in [("ソート前", a), ("ソート後", b)]:
    print("{0}: {1}".format(x, y))

入出力結果(Terminal)

$ ./sample.py
ソート前: ['e', 'a', 'd', 'b', 'c', 'E', 'A', 'D', 'B', 'C']
ソート後: ['a', 'A', 'b', 'B', 'C', 'c', 'D', 'd', 'E', '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 dictionarySort{
    my $some_array = shift;
    my @empty = ();
    recursiveSort($some_array, \@empty);
}
sub recursiveSort {
    my $some_array = shift;
    my $sorted_array = shift;
    my $unsorted_array = [@$some_array];
    return @$sorted_array unless @$unsorted_array;
    my $min = pop @$unsorted_array;
    my @tmp = ();
    for  (@$unsorted_array) {
        if (uc $min le uc $_) {
            push @tmp, $_;
        } else {
            push @tmp, $min;
            $min = $_;
        }
    }
    push @$sorted_array, $min;
    recursiveSort(\@tmp, $sorted_array);
}
my @a = ('e','a','d','b','c','E','A','D','B','C');
my @b = dictionarySort \@a;
print "ソート前: @a\n";
print "ソート後: @b\n";

入出力結果(Terminal)

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

0 コメント:

コメントを投稿