diff options
| author | Flavio Poletti <flavio@polettix.it> | 2020-12-15 23:14:50 +0100 |
|---|---|---|
| committer | Flavio Poletti <flavio@polettix.it> | 2020-12-15 23:14:50 +0100 |
| commit | 71c43e79d03679056e97d698f3e452729dcf0759 (patch) | |
| tree | 3d10f60a3c456b73b07b6f78cef9eea3a15f6a59 | |
| parent | 05b96afc64fbcae0ef11b10fd4bd60814cc79211 (diff) | |
| download | perlweeklychallenge-club-71c43e79d03679056e97d698f3e452729dcf0759.tar.gz perlweeklychallenge-club-71c43e79d03679056e97d698f3e452729dcf0759.tar.bz2 perlweeklychallenge-club-71c43e79d03679056e97d698f3e452729dcf0759.zip | |
Add polettix's solution to PWC091
| -rw-r--r-- | challenge-091/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-091/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-091/polettix/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-091/polettix/perl/ch-2.pl | 14 |
4 files changed, 50 insertions, 0 deletions
diff --git a/challenge-091/polettix/blog.txt b/challenge-091/polettix/blog.txt new file mode 100644 index 0000000000..8a0c74e5c9 --- /dev/null +++ b/challenge-091/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2020/12/15/pwc091-count-number/ diff --git a/challenge-091/polettix/blog1.txt b/challenge-091/polettix/blog1.txt new file mode 100644 index 0000000000..7de637fc35 --- /dev/null +++ b/challenge-091/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2020/12/16/jump-game/ diff --git a/challenge-091/polettix/perl/ch-1.pl b/challenge-091/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..30abdd0ef2 --- /dev/null +++ b/challenge-091/polettix/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub count_number ($N) { + my $retval = ''; + while (length $N) { + my ($sequence, $char) = $N =~ m{((.)\2*)}mxs; + my $n = length $sequence; + $retval .= $n . $char; + substr $N, 0, $n, ''; + } + return $retval; +} + +sub count_number_2 ($N) { + my $len = length $N; + my ($retval, $previous, $count) = ('', '', 0); + for my $i (0 .. $len) { + my $c = $i < $len ? substr($N, $i, 1) : ''; + if ($c eq $previous) { ++$count } + else { + $retval .= $count . $previous if $count; + ($previous, $count) = ($c, 1); + } + } + return $retval; +} + +my $input = shift // '1122234'; +say count_number($input); +say count_number_2($input); diff --git a/challenge-091/polettix/perl/ch-2.pl b/challenge-091/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..c3759fd93f --- /dev/null +++ b/challenge-091/polettix/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub jump_game ($N) { + my $position = 0; + $position += $N->[$position] while $position < $#$N && $N->[$position]; + return $position == $#$N ? 1 : 0; +} + +my @sequence = @ARGV ? @ARGV : (1, 2, 1, 2); +say jump_game(\@sequence); |
