diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-20 14:59:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-20 14:59:45 +0000 |
| commit | 72430eb2bc98c7e256f021d86de709b536be8364 (patch) | |
| tree | 9d9ab50063977b191e3c914bd6a2c4795fc65a51 | |
| parent | aa88708552e4ddd5271bd6790d7502d8f97537fd (diff) | |
| parent | 0e86d0c0240cb6c85a090a55492d2f3d23311498 (diff) | |
| download | perlweeklychallenge-club-72430eb2bc98c7e256f021d86de709b536be8364.tar.gz perlweeklychallenge-club-72430eb2bc98c7e256f021d86de709b536be8364.tar.bz2 perlweeklychallenge-club-72430eb2bc98c7e256f021d86de709b536be8364.zip | |
Merge pull request #3018 from wanderdoc/master
Solutions to challenge-091.
| -rw-r--r-- | challenge-091/wanderdoc/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-091/wanderdoc/perl/ch-2.pl | 47 |
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-091/wanderdoc/perl/ch-1.pl b/challenge-091/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..ab917f5e11 --- /dev/null +++ b/challenge-091/wanderdoc/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a positive number $N. Write a script to count number and display as you read it. +Example 1: Input: $N = 1122234 Output: 21321314 as we read "two 1 three 2 one 3 one 4" +Example 2: Input: $N = 2333445 Output: 12332415 as we read "one 2 three 3 two 4 one 5" +Example 3: Input: $N = 12345 Output: 1112131415 as we read "one 1 one 2 one 3 one 4 one 5" +=cut + + + + +use Test::More; + + + + +sub count_number +{ + my $num = $_[0]; + my @arr = split(//, $num); + + my @data; + + do { ( scalar @data == 0 or $_ != $data[-1][0] ) ? + push @data, [$_, 1] : $data[-1][1]++ } + for @arr; + my $output = join('',map { reverse @$_ } @data); + return $output; +} + + +is(count_number(1122234), 21321314, 'Example 1'); +is(count_number(2333445), 12332415, 'Example 2'); +is(count_number(12345), 1112131415, 'Example 3'); + + + +done_testing();
\ No newline at end of file diff --git a/challenge-091/wanderdoc/perl/ch-2.pl b/challenge-091/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..e474e9b650 --- /dev/null +++ b/challenge-091/wanderdoc/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of positive numbers @N, where value at each index determines how far you are allowed to jump further. Write a script to decide if you can jump to the last index. Print 1 if you are able to reach the last index otherwise 0. +Example 1: Input: @N = (1, 2, 1, 2) Output: 1 as we jump one place from index 0 and then two places from index 1 to reach the last index. +Example 2: Input: @N = (2,1,1,0,2) Output: 0 it is impossible to reach the last index. as we jump two places from index 0 to reach index 2, followed by one place jump from index 2 to reach the index 3. once you reached the index 3, you can't go any further because you can only jump 0 position further. +=cut + + + + + +use Test::More; + + + + + + +sub jump +{ + my @arr = @_; + die "Array with at least two elements required!$/" if $#arr == 0; + + my $i = 0; + while ( $i < $#arr ) + { + my $old_i = $i; + $i += $arr[$i]; + return 0 if ($i == $old_i or $i > $#arr); + } + return 1; +} + + + + + +is(jump(1, 2, 1, 2), 1, 'Example 1'); +is(jump(2, 1, 1, 0, 2), 0, 'Example 2'); +is(jump(1, 3, 2), 0, 'Example 3'); +is(jump(1, 1, 3, 2, 1, 0), 1, 'Example 4'); +is(jump(1, 0), 1, 'Example 5'); +is(jump(0, 1), 0, 'Example 5'); +done_testing();
\ No newline at end of file |
