aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-04 22:17:58 +0100
committerGitHub <noreply@github.com>2020-10-04 22:17:58 +0100
commit647d63353c140756bb03fb176661c760f2a8c1a9 (patch)
tree46ba21b695466146991a66ae12a2e05500637769
parente90a25bce6d034f0c6fc1367449463b4901a789a (diff)
parent9629424bea12d411932645719c774a8a5f7a1b58 (diff)
downloadperlweeklychallenge-club-647d63353c140756bb03fb176661c760f2a8c1a9.tar.gz
perlweeklychallenge-club-647d63353c140756bb03fb176661c760f2a8c1a9.tar.bz2
perlweeklychallenge-club-647d63353c140756bb03fb176661c760f2a8c1a9.zip
Merge pull request #2446 from arnesom/branch-for-challenge-080
Arne Sommer
-rw-r--r--challenge-080/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-080/arne-sommer/perl/ch-1.pl32
-rwxr-xr-xchallenge-080/arne-sommer/perl/ch-2.pl56
-rwxr-xr-xchallenge-080/arne-sommer/perl/count-candies-sort-perl56
-rwxr-xr-xchallenge-080/arne-sommer/perl/smallest-positive-integer-perl32
-rwxr-xr-xchallenge-080/arne-sommer/raku/ch-1.p618
-rwxr-xr-xchallenge-080/arne-sommer/raku/ch-2.p635
-rwxr-xr-xchallenge-080/arne-sommer/raku/count-candies-loop42
-rwxr-xr-xchallenge-080/arne-sommer/raku/count-candies-sort35
-rwxr-xr-xchallenge-080/arne-sommer/raku/count-candies-wrong22
-rwxr-xr-xchallenge-080/arne-sommer/raku/smallest-positive-integer18
11 files changed, 347 insertions, 0 deletions
diff --git a/challenge-080/arne-sommer/blog.txt b/challenge-080/arne-sommer/blog.txt
new file mode 100644
index 0000000000..534e5fd33c
--- /dev/null
+++ b/challenge-080/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/positively-candy.html
diff --git a/challenge-080/arne-sommer/perl/ch-1.pl b/challenge-080/arne-sommer/perl/ch-1.pl
new file mode 100755
index 0000000000..c2b36a4832
--- /dev/null
+++ b/challenge-080/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#! /usr/bin/env perl
+
+use Perl6::Junction 'all';
+use List::MoreUtils qw(uniq);
+use Getopt::Long;
+use feature 'say';
+use strict;
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my @A = @ARGV;
+
+die "Please specify at least one element" unless @A;
+
+die "Integers only" unless all(@A) == qr/^\d+$/;
+
+my @positively_sorted = uniq sort { $a <=> $b } grep { $_ > 0 } @A;
+
+say ": Positively Sorted: " . join(", ", @positively_sorted) if $verbose;
+
+my $expected = 1;
+
+for my $current (@positively_sorted)
+{
+ say ": Current value: $current - expected $expected" if $verbose;
+ last if $current != $expected;
+ $expected++;
+}
+
+say $expected; \ No newline at end of file
diff --git a/challenge-080/arne-sommer/perl/ch-2.pl b/challenge-080/arne-sommer/perl/ch-2.pl
new file mode 100755
index 0000000000..c019e9391d
--- /dev/null
+++ b/challenge-080/arne-sommer/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#! /usr/bin/env perl
+
+use strict;
+use bigrat; # -> inf
+use feature 'signatures';
+
+use Perl6::Junction 'all';
+use List::Util qw/sum max/;
+use Getopt::Long;
+use feature 'say';
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my @N = @ARGV;
+
+die "Please specify at least one element" unless @N;
+
+die "Integers only" unless all(@N) == qr/^\d+$/;
+
+push(@N, inf); unshift(@N, inf);
+
+my %M = map { $_ => $N[$_] } 0 .. (@N -1);
+
+my @C = (); ## x (length(@N));
+
+my $N_end = @N -2;
+
+for my $index (sort { $M{$a} <=> $M{$b} } keys %M)
+{
+ $C[$index] = candy_count($index);
+ say ": Index $index with value $M{$index} and candies $C[$index]" if $verbose;
+}
+
+if ($verbose)
+{
+ say ": Ranking w/border: " . join(", ", @N);
+ say ": Candies w/border: " . join(", ", @C);
+
+ say ": Ranking: " . join(", ", @N[1..$N_end]);
+ say ": Candies: " . join(", ", @C[1..$N_end]);
+}
+
+say sum(@C);
+
+sub candy_count ($index)
+{
+ return 0 if $index == 0 || $index == @N -1;
+
+ return max($C[$index-1], $C[$index+1]) +1 if $N[$index] > $N[$index-1] || $N[$index] > $N[$index+1];
+
+ return 1;
+}
diff --git a/challenge-080/arne-sommer/perl/count-candies-sort-perl b/challenge-080/arne-sommer/perl/count-candies-sort-perl
new file mode 100755
index 0000000000..c019e9391d
--- /dev/null
+++ b/challenge-080/arne-sommer/perl/count-candies-sort-perl
@@ -0,0 +1,56 @@
+#! /usr/bin/env perl
+
+use strict;
+use bigrat; # -> inf
+use feature 'signatures';
+
+use Perl6::Junction 'all';
+use List::Util qw/sum max/;
+use Getopt::Long;
+use feature 'say';
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my @N = @ARGV;
+
+die "Please specify at least one element" unless @N;
+
+die "Integers only" unless all(@N) == qr/^\d+$/;
+
+push(@N, inf); unshift(@N, inf);
+
+my %M = map { $_ => $N[$_] } 0 .. (@N -1);
+
+my @C = (); ## x (length(@N));
+
+my $N_end = @N -2;
+
+for my $index (sort { $M{$a} <=> $M{$b} } keys %M)
+{
+ $C[$index] = candy_count($index);
+ say ": Index $index with value $M{$index} and candies $C[$index]" if $verbose;
+}
+
+if ($verbose)
+{
+ say ": Ranking w/border: " . join(", ", @N);
+ say ": Candies w/border: " . join(", ", @C);
+
+ say ": Ranking: " . join(", ", @N[1..$N_end]);
+ say ": Candies: " . join(", ", @C[1..$N_end]);
+}
+
+say sum(@C);
+
+sub candy_count ($index)
+{
+ return 0 if $index == 0 || $index == @N -1;
+
+ return max($C[$index-1], $C[$index+1]) +1 if $N[$index] > $N[$index-1] || $N[$index] > $N[$index+1];
+
+ return 1;
+}
diff --git a/challenge-080/arne-sommer/perl/smallest-positive-integer-perl b/challenge-080/arne-sommer/perl/smallest-positive-integer-perl
new file mode 100755
index 0000000000..c2b36a4832
--- /dev/null
+++ b/challenge-080/arne-sommer/perl/smallest-positive-integer-perl
@@ -0,0 +1,32 @@
+#! /usr/bin/env perl
+
+use Perl6::Junction 'all';
+use List::MoreUtils qw(uniq);
+use Getopt::Long;
+use feature 'say';
+use strict;
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my @A = @ARGV;
+
+die "Please specify at least one element" unless @A;
+
+die "Integers only" unless all(@A) == qr/^\d+$/;
+
+my @positively_sorted = uniq sort { $a <=> $b } grep { $_ > 0 } @A;
+
+say ": Positively Sorted: " . join(", ", @positively_sorted) if $verbose;
+
+my $expected = 1;
+
+for my $current (@positively_sorted)
+{
+ say ": Current value: $current - expected $expected" if $verbose;
+ last if $current != $expected;
+ $expected++;
+}
+
+say $expected; \ No newline at end of file
diff --git a/challenge-080/arne-sommer/raku/ch-1.p6 b/challenge-080/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..f1507df1a9
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,18 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @positively_sorted = @N.grep( * > 0 ).sort.squish;
+
+say ": Positively Sorted: { @positively_sorted.join(", ") }" if $verbose;
+
+my $expected = 1;
+
+for @positively_sorted -> $current
+{
+ say ": Current value: $current - expected $expected" if $verbose;
+ last if $current != $expected;
+ $expected++;
+}
+
+say $expected; \ No newline at end of file
diff --git a/challenge-080/arne-sommer/raku/ch-2.p6 b/challenge-080/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..9b12933ae1
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,35 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @C = 1 xx @N.elems;
+@N.push: Inf; @N.unshift: Inf;
+@C.push: 0; @C.unshift: 0;
+
+my %M = (0..@N.end).map({ $_ => @N[$_] });
+
+for %M.keys.sort({ %M{$^a} <=> %M{$^b}}) -> $index
+{
+ @C[$index] = candy-count($index);
+ say ": Index $index with value %M{$index} and candies @C[$index]" if $verbose;
+}
+
+if $verbose
+{
+ say ": Ranking w/border: { @N.join(", ") }";
+ say ": Candies w/border: { @C.join(", ") }";
+
+ say ": Ranking: { @N[1..@N.end -1].join(", ") }";
+ say ": Candies: { @C[1..@N.end -1].join(", ") }";
+}
+
+say @C.sum;
+
+sub candy-count ($index)
+{
+ return 0 if $index == 0|@N.end;
+
+ return max(@C[$index-1], @C[$index+1]) +1 if @N[$index] > @N[$index-1] || @N[$index] > @N[$index+1];
+
+ return 1;
+}
diff --git a/challenge-080/arne-sommer/raku/count-candies-loop b/challenge-080/arne-sommer/raku/count-candies-loop
new file mode 100755
index 0000000000..81a743df34
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/count-candies-loop
@@ -0,0 +1,42 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @C = 1 xx @N.elems;
+@N.push: Inf; @N.unshift: Inf;
+@C.push: 0; @C.unshift: 0;
+
+say ": \@N Ranking w/border: { @N.join(", ") }" if $verbose;
+say ": \@C Candies w/border: { @C.join(", ") }" if $verbose;
+
+my $loop = 1;
+
+loop
+{
+ my $changes = 0;
+ for 1 .. @N.end -1 -> $index
+ {
+ if @N[$index] > @N[$index-1] && @C[$index] <= @C[$index-1]
+ {
+ @C[$index]++;
+ $changes++;
+ say ": \@C Candies w/border: { @C.join(", ") } (left) Iteration:$loop" if $verbose;
+ }
+ if @N[$index] > @N[$index+1] && @C[$index] <= @C[$index+1]
+ {
+ @C[$index]++;
+ $changes++;
+ say ": \@C Candies w/border: { @C.join(", ") } (right) Iteration:$loop" if $verbose;
+ }
+ }
+ last if $changes == 0;
+ $loop++;
+}
+
+if $verbose
+{
+ say ": \@N Ranking: { @N[1..@N.end -1].join(", ") }";
+ say ": \@C Candies: { @C[1..@N.end -1].join(", ") }";
+}
+
+say @C.sum;
diff --git a/challenge-080/arne-sommer/raku/count-candies-sort b/challenge-080/arne-sommer/raku/count-candies-sort
new file mode 100755
index 0000000000..9b12933ae1
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/count-candies-sort
@@ -0,0 +1,35 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @C = 1 xx @N.elems;
+@N.push: Inf; @N.unshift: Inf;
+@C.push: 0; @C.unshift: 0;
+
+my %M = (0..@N.end).map({ $_ => @N[$_] });
+
+for %M.keys.sort({ %M{$^a} <=> %M{$^b}}) -> $index
+{
+ @C[$index] = candy-count($index);
+ say ": Index $index with value %M{$index} and candies @C[$index]" if $verbose;
+}
+
+if $verbose
+{
+ say ": Ranking w/border: { @N.join(", ") }";
+ say ": Candies w/border: { @C.join(", ") }";
+
+ say ": Ranking: { @N[1..@N.end -1].join(", ") }";
+ say ": Candies: { @C[1..@N.end -1].join(", ") }";
+}
+
+say @C.sum;
+
+sub candy-count ($index)
+{
+ return 0 if $index == 0|@N.end;
+
+ return max(@C[$index-1], @C[$index+1]) +1 if @N[$index] > @N[$index-1] || @N[$index] > @N[$index+1];
+
+ return 1;
+}
diff --git a/challenge-080/arne-sommer/raku/count-candies-wrong b/challenge-080/arne-sommer/raku/count-candies-wrong
new file mode 100755
index 0000000000..daa5f752bb
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/count-candies-wrong
@@ -0,0 +1,22 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @C;
+
+say ": Ranking: { @N.join(", ") }" if $verbose;
+
+(0..@N.end).map({ @C[$_] = candy-count($_) });
+
+say ": Candies: { @C.join(", ") }" if $verbose;
+
+say @C.sum;
+
+sub candy-count ($index)
+{
+ my $count = 1;
+ $count++ if $index > 0 && @N[$index] > @N[$index-1];
+ $count++ if $index < @N.end && @N[$index] > @N[$index+1];
+
+ return $count;
+}
diff --git a/challenge-080/arne-sommer/raku/smallest-positive-integer b/challenge-080/arne-sommer/raku/smallest-positive-integer
new file mode 100755
index 0000000000..f1507df1a9
--- /dev/null
+++ b/challenge-080/arne-sommer/raku/smallest-positive-integer
@@ -0,0 +1,18 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int, :v(:$verbose));
+
+my @positively_sorted = @N.grep( * > 0 ).sort.squish;
+
+say ": Positively Sorted: { @positively_sorted.join(", ") }" if $verbose;
+
+my $expected = 1;
+
+for @positively_sorted -> $current
+{
+ say ": Current value: $current - expected $expected" if $verbose;
+ last if $current != $expected;
+ $expected++;
+}
+
+say $expected; \ No newline at end of file