diff options
author | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2018-12-26 11:52:05 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2018-12-26 11:52:05 +0700 |
commit | d0ce1ee82b03ab0e0b9bf0184abb7ce0d0add00f (patch) | |
tree | 8a93094065ead1d7abbcfcd67c48245d0ed7b1ec /thinkperl6 | |
parent | 8d090c241f25b226a6533e066e5e7e21738d70be (diff) | |
download | cp-d0ce1ee82b03ab0e0b9bf0184abb7ce0d0add00f.tar.gz |
Practikal Informatiques
Diffstat (limited to 'thinkperl6')
-rwxr-xr-x | thinkperl6/draft.p6 | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/thinkperl6/draft.p6 b/thinkperl6/draft.p6 index 7f74238..a8add76 100755 --- a/thinkperl6/draft.p6 +++ b/thinkperl6/draft.p6 @@ -84,3 +84,48 @@ sub estimate-pi { 1 / $factor / Srinivasa-Ramanujan } #put abs estimate-pi() - pi; + +sub ducks { map * ~ 'ack' , flat('J'..'N', 'Ou', 'P', 'Qu') } +#put ducks; + +sub count(Str $string, Str $substr, Int $index = 0, Int $result = 0) { + my $i = index $string, $substr, $index; + return $result if $i === Any; + count $string, $substr, $i + 1, $result + 1 +} +#put count 'banana', 'na'; + +sub bubble-sort(@seq is copy) { + my $done; + repeat { + $done = True; + for ^(@seq.elems - 1) -> $i { + if @seq[$i] > @seq[$i+1] { + (@seq[$i], @seq[$i+1]) = @seq[$i+1], @seq[$i]; + $done = False + } + } + } until $done; + @seq +} +#put bubble-sort <4 2 6 5 3 9 1>; + +sub select-sort(@seq is copy) { + for ^(@seq.elems - 1) -> $i { + for $i..^@seq.elems -> $j { + (@seq[$i], @seq[$j]) = @seq[$j], @seq[$i] if @seq[$i] > @seq[$j] + } + } + @seq +} +#put select-sort <4 2 6 5 3 9 1>; + +sub insert-sort(@seq is copy) { + for 1..^@seq.elems -> $i { + loop (my $j = $i; $j and @seq[$j] < @seq[$j - 1]; $j--) { + (@seq[$j], @seq[$j - 1]) = @seq[$j - 1], @seq[$j] + } + } + @seq +} +#put insert-sort <4 2 6 5 3 9 1>; |