diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-02 01:49:57 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-02 01:49:57 +0100 |
| commit | e4ae800ce520b56acb0205aad310a74ff95adc0c (patch) | |
| tree | a6f48cca103f3233d9a274837b5d8658a3210ae6 /challenge-071 | |
| parent | e53debb47f8cf75edd3f5b3551dc8f6631e7edbe (diff) | |
| download | perlweeklychallenge-club-e4ae800ce520b56acb0205aad310a74ff95adc0c.tar.gz perlweeklychallenge-club-e4ae800ce520b56acb0205aad310a74ff95adc0c.tar.bz2 perlweeklychallenge-club-e4ae800ce520b56acb0205aad310a74ff95adc0c.zip | |
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-071')
| -rw-r--r-- | challenge-071/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/perl/peak-element-perl | 34 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/perl/peak-element-perl-map | 32 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/ch-1.raku | 22 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/ch-2.raku | 94 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/peak-element | 22 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/peak-element-if | 32 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/tll-class | 94 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/tll-class-wrong | 95 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/tll-hybrid | 92 | ||||
| -rwxr-xr-x | challenge-071/arne-sommer/raku/tll-proc | 90 |
12 files changed, 642 insertions, 0 deletions
diff --git a/challenge-071/arne-sommer/blog.txt b/challenge-071/arne-sommer/blog.txt new file mode 100644 index 0000000000..c9e250cd76 --- /dev/null +++ b/challenge-071/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/peaked-trim.html diff --git a/challenge-071/arne-sommer/perl/ch-1.pl b/challenge-071/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..59d4723efc --- /dev/null +++ b/challenge-071/arne-sommer/perl/ch-1.pl @@ -0,0 +1,34 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; + +my $N = shift(@ARGV) // die 'Please specify $N'; +my $verbose; + +if ($N eq "--verbose" || $N eq "--v") +{ + $verbose++; + $N = shift(@ARGV) // die 'Please specify $N'; +} + +die '$N must be an integer >= 1' unless int($N) == $N && $N >= 1; + +my @array; + +push(@array, int(rand(50) +1)) for 1 .. $N; + +say ": Values: " , join(", ", @array) if $verbose; + +my @peak; + +push(@array, -1); +unshift(@array, -1); + +for my $index (1 .. $N) +{ + say ": Checking at position $index: (left:" . $array[$index-1] . ", value: " . $array[$index] . ", right:" . $array[$index+1] . ")" if $verbose; + push(@peak, @array[$index]) if $array[$index] > $array[$index-1] && $array[$index] > $array[$index+1]; +} + +say "[ ", join(", ", @peak), " ]"; diff --git a/challenge-071/arne-sommer/perl/peak-element-perl b/challenge-071/arne-sommer/perl/peak-element-perl new file mode 100755 index 0000000000..59d4723efc --- /dev/null +++ b/challenge-071/arne-sommer/perl/peak-element-perl @@ -0,0 +1,34 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; + +my $N = shift(@ARGV) // die 'Please specify $N'; +my $verbose; + +if ($N eq "--verbose" || $N eq "--v") +{ + $verbose++; + $N = shift(@ARGV) // die 'Please specify $N'; +} + +die '$N must be an integer >= 1' unless int($N) == $N && $N >= 1; + +my @array; + +push(@array, int(rand(50) +1)) for 1 .. $N; + +say ": Values: " , join(", ", @array) if $verbose; + +my @peak; + +push(@array, -1); +unshift(@array, -1); + +for my $index (1 .. $N) +{ + say ": Checking at position $index: (left:" . $array[$index-1] . ", value: " . $array[$index] . ", right:" . $array[$index+1] . ")" if $verbose; + push(@peak, @array[$index]) if $array[$index] > $array[$index-1] && $array[$index] > $array[$index+1]; +} + +say "[ ", join(", ", @peak), " ]"; diff --git a/challenge-071/arne-sommer/perl/peak-element-perl-map b/challenge-071/arne-sommer/perl/peak-element-perl-map new file mode 100755 index 0000000000..441c2804f6 --- /dev/null +++ b/challenge-071/arne-sommer/perl/peak-element-perl-map @@ -0,0 +1,32 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; + +my $N = shift(@ARGV) // die 'Please specify $N'; +my $verbose; + +if ($N eq "--verbose" || $N eq "--v") +{ + $verbose++; + $N = shift(@ARGV) // die 'Please specify $N'; +} + +die '$N must be an integer >= 1' unless int($N) == $N && $N >= 1; + +my @array = map { int(rand(50) +1) } 1 .. $N; + +say ": Values: " , join(", ", @array) if $verbose; + +my @peak; + +push(@array, -1); +unshift(@array, -1); + +for my $index (1 .. $N) +{ + say ": Checking at position $index: (left:" . $array[$index-1] . ", value: " . $array[$index] . ", right:" . $array[$index+1] . ")" if $verbose; + push(@peak, @array[$index]) if $array[$index] > $array[$index-1] && $array[$index] > $array[$index+1]; +} + +say "[ ", join(", ", @peak), " ]"; diff --git a/challenge-071/arne-sommer/raku/ch-1.raku b/challenge-071/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..ba26305dd4 --- /dev/null +++ b/challenge-071/arne-sommer/raku/ch-1.raku @@ -0,0 +1,22 @@ +#! /usr/bin/env raku + +subset VeryPosInt of Int where * > 1; + +unit sub MAIN (VeryPosInt $N, :$v, :$verbose = $v); + +my @array = (1..50).roll($N); + +say ": Values: @array[]" if $verbose; + +@array.push: -1; +@array.unshift: -1; + +my @peak; + +for 1 .. $N -> $index +{ + say ": Checking at position $index: (left:@array[$index-1], value: @array[$index], right:@array[$index+1])" if $verbose; + @peak.push: @array[$index] if @array[$index] > @array[$index-1] && @array[$index] > @array[$index+1]; +} + +say "[ ", @peak.join(", "), " ]"; diff --git a/challenge-071/arne-sommer/raku/ch-2.raku b/challenge-071/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..0004329180 --- /dev/null +++ b/challenge-071/arne-sommer/raku/ch-2.raku @@ -0,0 +1,94 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = :$v); + +class LinkedElement +{ + has $.value is rw; + has $.next is rw; + + method print-list + { + print self.value; + if self.next + { + print " -> "; + self.next.print-list; + } + else + { + print "\n"; + } + } + + method list-length + { + my $length = 1; + + my $current = self.next; + + while ($current) + { + $current = $current.next; + $length++; + } + return $length; + } + + method remove-from-end($from-the-end) + { + my $length = self.list-length; + + if $length == 1 + { + die "[]"; + } + elsif $from-the-end > $length + { + self.value = self.next.value; + self.next = self.next.next; + } + else + { + my $current = self; + + for 1 .. ($length - $from-the-end -1) + { + $current = $current.next; + } + + $current.next = $current.next.next; + } + } +} + +my $length = (1..50).pick; + +my $head; +my $current; + +for 1..$length -> $value +{ + my $new = LinkedElement.new(value => $value); + + if $current + { + $current.next = $new; + $current = $current.next; + } + else # Initially + { + $head = $new; + $current = $head; + } +} + +$head.print-list; + +say "Length: ", $head.list-length if $verbose; + +$head.remove-from-end($N); + +$head.print-list; diff --git a/challenge-071/arne-sommer/raku/peak-element b/challenge-071/arne-sommer/raku/peak-element new file mode 100755 index 0000000000..ba26305dd4 --- /dev/null +++ b/challenge-071/arne-sommer/raku/peak-element @@ -0,0 +1,22 @@ +#! /usr/bin/env raku + +subset VeryPosInt of Int where * > 1; + +unit sub MAIN (VeryPosInt $N, :$v, :$verbose = $v); + +my @array = (1..50).roll($N); + +say ": Values: @array[]" if $verbose; + +@array.push: -1; +@array.unshift: -1; + +my @peak; + +for 1 .. $N -> $index +{ + say ": Checking at position $index: (left:@array[$index-1], value: @array[$index], right:@array[$index+1])" if $verbose; + @peak.push: @array[$index] if @array[$index] > @array[$index-1] && @array[$index] > @array[$index+1]; +} + +say "[ ", @peak.join(", "), " ]"; diff --git a/challenge-071/arne-sommer/raku/peak-element-if b/challenge-071/arne-sommer/raku/peak-element-if new file mode 100755 index 0000000000..481c91543e --- /dev/null +++ b/challenge-071/arne-sommer/raku/peak-element-if @@ -0,0 +1,32 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = $v); + +my @array = (1..50).roll($N); + +say ": Values: @array[]" if $verbose; + +my @peak; + +for ^$N-> $index +{ + if $index == 0 + { + say ": Checking at index $index: (value: @array[$index], right:@array[$index+1])" if $verbose; + @peak.push: @array[$index] if @array[$index] > @array[$index+1]; + } + elsif $index <= $N -2 + { + say ": Checking at index $index: (left:@array[$index-1], value: @array[$index], right:@array[$index+1])" if $verbose; + @peak.push: @array[$index] if @array[$index] > @array[$index-1] && @array[$index] > @array[$index+1]; + } + else # if $index == $N -2 + { + say ": Checking at index $index: (left:@array[$index-1], value: @array[$index])" if $verbose; + @peak.push: @array[$index] if @array[$index] > @array[$index-1]; + } +} + +say "[ ", @peak.join(", "), " ]"; diff --git a/challenge-071/arne-sommer/raku/tll-class b/challenge-071/arne-sommer/raku/tll-class new file mode 100755 index 0000000000..0004329180 --- /dev/null +++ b/challenge-071/arne-sommer/raku/tll-class @@ -0,0 +1,94 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = :$v); + +class LinkedElement +{ + has $.value is rw; + has $.next is rw; + + method print-list + { + print self.value; + if self.next + { + print " -> "; + self.next.print-list; + } + else + { + print "\n"; + } + } + + method list-length + { + my $length = 1; + + my $current = self.next; + + while ($current) + { + $current = $current.next; + $length++; + } + return $length; + } + + method remove-from-end($from-the-end) + { + my $length = self.list-length; + + if $length == 1 + { + die "[]"; + } + elsif $from-the-end > $length + { + self.value = self.next.value; + self.next = self.next.next; + } + else + { + my $current = self; + + for 1 .. ($length - $from-the-end -1) + { + $current = $current.next; + } + + $current.next = $current.next.next; + } + } +} + +my $length = (1..50).pick; + +my $head; +my $current; + +for 1..$length -> $value +{ + my $new = LinkedElement.new(value => $value); + + if $current + { + $current.next = $new; + $current = $current.next; + } + else # Initially + { + $head = $new; + $current = $head; + } +} + +$head.print-list; + +say "Length: ", $head.list-length if $verbose; + +$head.remove-from-end($N); + +$head.print-list; diff --git a/challenge-071/arne-sommer/raku/tll-class-wrong b/challenge-071/arne-sommer/raku/tll-class-wrong new file mode 100755 index 0000000000..019fb1f19b --- /dev/null +++ b/challenge-071/arne-sommer/raku/tll-class-wrong @@ -0,0 +1,95 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = :$v); + +class LinkedElement +{ + has $.value; + has $.next is rw; + + method print-list + { + print self.value; + if self.next + { + print " -> "; + self.next.print-list; + } + else + { + print "\n"; + } + } + + method list-length + { + my $length = 1; + + my $current = self.next; + + while ($current) + { + $current = $current.next; + $length++; + } + return $length; + } + + method remove-from-end($from-the-end) + { + my $length = self.list-length; + + if $length == 1 + { + die "[]"; + } + elsif $from-the-end > $length + { + self = self.next; + } + else + { + my $current = self; + + for 1 .. ($length - $from-the-end -1) + { + $current = $current.next; + } + + $current.next = $current.next.next; + } + } +} + + + +my $length = (1..50).pick; + +my $head; +my $current; + +for 1..$length -> $value +{ + my $new = LinkedElement.new(value => $value); + + if $current + { + $current.next = $new; + $current = $current.next; + } + else # Initially + { + $head = $new; + $current = $head; + } +} + +$head.print-list; + +say "Length: ", $head.list-length if $verbose; + +$head.remove-from-end($N); + +$head.print-list; diff --git a/challenge-071/arne-sommer/raku/tll-hybrid b/challenge-071/arne-sommer/raku/tll-hybrid new file mode 100755 index 0000000000..cf703da48d --- /dev/null +++ b/challenge-071/arne-sommer/raku/tll-hybrid @@ -0,0 +1,92 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = :$v, :$limit = 50); + +class LinkedElement +{ + has $.value is rw; + has $.next is rw; + + method print-list + { + print self.value; + if self.next + { + print " -> "; + self.next.print-list; + } + else + { + print "\n"; + } + } + + method list-length + { + my $length = 1; + + my $current = self.next; + + while ($current) + { + $current = $current.next; + $length++; + } + return $length; + } +} + +my $length = (1..$limit).pick; + +my $head; +my $current; + +for 1..$length -> $value +{ + my $new = LinkedElement.new(value => $value); + + if $current + { + $current.next = $new; + $current = $current.next; + } + else # Initially + { + $head = $new; + $current = $head; + } +} + +$head.print-list; + +say "Length: ", $head.list-length if $verbose; + +remove-element($head, $N); + +$head + ?? $head.print-list + !! say "[]"; + + +sub remove-element ($list is rw, $from-the-end) +{ + my $length = $list.list-length; + + if $from-the-end > $length + { + $list = $list.next; + } + else + { + my $current = $list; + + for 1 .. ($length - $from-the-end -1) + { + $current = $current.next; + } + + $current.next = $current.next.next; + } +}
\ No newline at end of file diff --git a/challenge-071/arne-sommer/raku/tll-proc b/challenge-071/arne-sommer/raku/tll-proc new file mode 100755 index 0000000000..e331fba227 --- /dev/null +++ b/challenge-071/arne-sommer/raku/tll-proc @@ -0,0 +1,90 @@ +#! /usr/bin/env raku + +subset PosInt of Int where * >= 1; + +unit sub MAIN (PosInt $N, :$v, :$verbose = :$v); + +class LinkedElement +{ + has $.value; + has $.next is rw; +} + +my $length = (1..50).pick; + +my $head; +my $current; + +for 1..$length -> $value +{ + my $new = LinkedElement.new(value => $value); + + if $current + { + $current.next = $new; + $current = $current.next; + } + else # Initially + { + $head = $new; + $current = $head; + } +} + +print-list($head); + +sub print-list ($list) +{ + print $list.value; + if $list.next + { + print " -> "; + print-list($list.next); + } + else + { + print "\n"; + } +} + +sub get-list-length ($list) +{ + return 0 unless $list; + my $length = 1; + + $current = $list.next; + + while ($current) + { + $current = $current.next; + $length++; + } + return $length; +} + +say "Length: ", get-list-length($head) if $verbose; + +remove-element($head, $N); + +print-list($head); + +sub remove-element ($list is rw, $from-the-end) +{ + my $length = get-list-length($list); + + if $from-the-end > $length + { + $list = $list.next; + } + else + { + my $current = $list; + + for 1 .. ($length - $from-the-end -1) + { + $current = $current.next; + } + + $current.next = $current.next.next; + } +}
\ No newline at end of file |
