Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -1,10 +1,10 @@
sub pancake_sort ( @a is copy ) {
sub pancake_sort(@a is copy) {
my $endpoint = @a.end;
while $endpoint > 0 and not [<] @a {
my $max_i = [0..$endpoint].max: { @a[$_] };
my $max = @a[$max_i];
if @a[$endpoint] == $max {
$endpoint-- while @a[$endpoint] == $max;
$endpoint-- while $endpoint >= 0 and @a[$endpoint] == $max;
next;
}
# @a[$endpoint] is not $max, so it needs flipping;
@ -15,6 +15,10 @@ sub pancake_sort ( @a is copy ) {
}
return @a;
}
my @data = 6, 7, 2, 1, 8, 9, 5, 3, 4;
say 'input = ' ~ @data;
say 'output = ' ~ @data.&pancake_sort;
for <6 7 2 1 8 9 5 3 4>,
<4 5 7 1 46 78 2 2 1 9 10>,
<0 -9 -8 2 -7 8 6 -2 -8 3> -> @data {
say 'input = ' ~ @data;
say 'output = ' ~ @data.&pancake_sort
}

View file

@ -0,0 +1,23 @@
# 20240908 Updated Raku programming solution
sub pancake-sort(@data is copy) { # imperative
for @data.elems - 1 ... 1 {
@data[0 .. @data[0..$_].maxpairs[*-1].key] .= reverse;
@data[0 .. $_] .= reverse;
}
return @data
}
sub pancake_sort(@data is copy) { # recursive
return @data if @data.elems <= 1;
@data[0 .. @data.maxpairs[*-1].key] .= reverse;
return pancake_sort(@data[1..*-1]).append: @data[0]
}
for <6 7 2 1 8 9 5 3 4>,
<4 5 7 1 46 78 2 2 1 9 10>,
<0 -9 -8 2 -7 8 6 -2 -8 3> -> @data {
say 'input = ' ~ @data;
say 'output = ' ~ pancake-sort(@data);
say 'output = ' ~ pancake_sort(@data) ~ "\n"
}