基本。
小さい頃から「漫画家になるー」と言いながら、絵ばっか描いてて
まさか将来自分がプログラマーになるなんざー思ってなかったので、
10歳の頃にいじったPC-6001の時の経験と知識が、
私にとってPCの元祖の姿で、それに肉付けして今がある訳だけど。
限界があるわなぁ…それだけじゃ orz
かくして、昨年夏もアセンブリとか勉強しようとしてたし、
時間があればやりたいと思ってた事ってーのは
この「基本を勉強する」って事なのでした。
だから、借りてきた本は「プログラマの完全常識 開発者が知っておくべきプロの知恵」。
勿論、知ってる事も沢山入ってるけど、飽きっぽい私には丁度良い感じです。
何事も基本って大事だし、武器になると思うのよね。
だから、語源が好きなんだろうなぁ、私。
そんな訳で、この本で出てきた基本のパターンを perl で書いて遊ぶ。
・bubble sort(バブルソート)
もっと良いルーチンがあるような気がするけど、とりあえず。#!/usr/bin/perl -w
# ----------------------------------------
# sample code for bubble sort
# 20080626 create anigon
# ----------------------------------------
use strict;
# ----------------------------------------
# main
# ----------------------------------------
my @data = qw(27 13 4 98 3 456);
&sort(0, $#data, \@data);
printf "%s\n", join ',', @data;
exit;
# ----------------------------------------
# function
# ----------------------------------------
sub sort($$\@) {
my $targetPosition = shift;
my $maxIndex = shift;
my $refData = shift;
my $beforeIndex = 0;
my $beforeValue = '';
my $currentValue = '';
for (my $index = $maxIndex; $index > $targetPosition; $index--) {
$beforeIndex = $index - 1;
$beforeValue = $refData->[$beforeIndex];
$currentValue = $refData->[$index];
if ($beforeValue < $currentValue) {
$refData->[$index] = $currentValue;
$refData->[$beforeIndex] = $beforeValue;
} else {
$refData->[$beforeIndex] = $currentValue;
$refData->[$index] = $beforeValue;
}
}
my $nextTargetPosition = $targetPosition + 1;
return if $nextTargetPosition > $maxIndex;
&sort($nextTargetPosition, $maxIndex, $refData);
}
# end of this script
・sentinel(番兵)
#!/usr/bin/perl -w# ----------------------------------------
# sample code for sentinel
# 20080626 create anigon
# ----------------------------------------
use strict;
# ----------------------------------------
# main
# ----------------------------------------
my @data = qw(178 273 123 3 17 39 50 287 13 98 321 1023 6 22);
my $target =($ARGV[0]) ? $ARGV[0] : 32;
my $index = 0;
my $maxIndex = $#data;
push @data, $target;
for ($index = 0; $index <= $maxIndex; $index++) {
last if $data[$index] == $target;
}
if ($index <= $maxIndex) {
print "RESULT:\t${index}\t${target}\n";
} else {
print "RESULT:\t${target} is not found.\n";
}
exit;
# end of this script
・binary search(二分探索)
#!/usr/bin/perl -w# ----------------------------------------
# sample code for binary search
# 20080626 create anigon
# ----------------------------------------
use strict;
# ----------------------------------------
# main
# ----------------------------------------
my @data = qw(1 5 6 7 9 11 24 87 123 176 198 237 287 312 376 396 399 472 481 503 526 531 540);
my $target = ($ARGV[0]) ? $ARGV[0] : 92;
my $result = &binsearch(0, $#data, $target, \@data);
if (defined $result) {
print "RESULT:\t${result}\t".$data[$result]."\n";
} else {
print "RESULT:\t${target} is not found.\n";
}
exit;
# ----------------------------------------
# function
# ----------------------------------------
sub binsearch($$$\@) {
my $left = shift;
my $right = shift;
my $target = shift;
my $refArray = shift;
return undef if $left > $right;
my $middle = int(($right + $left)/2);
my $diff = $refArray->[$middle] - $target;
return $middle if $diff == 0;
if ($diff > 0) {
$right = $middle -1;
} elsif ($diff < 0) {
$left = $middle +1;
}
&binsearch($left, $right, $target, $refArray);
}
# end of this script
それにしても、プロトタイプの指定が間違ってるような気も。
\@ は 配列のリファレンスで渡ってきた時もOKなんだろか?
って、warning 出ないから、おっけーなのかな。
\@ と指定してるけど、渡す側が \@ とやってようが、
既に $data = []; で設定した $ を指定しようが、どっちでもいいんだろーか。
まぁ、どっちも同じだから、いいのか。