diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-20 01:55:56 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-20 01:55:56 +0000 |
| commit | 4a2e34ea37373e1b9c698ef3d57d4d342e360e23 (patch) | |
| tree | dceb24a54647b525c2ae5f2f14f5f9cf3a0845bf | |
| parent | 540753e6fd4deff4bb907ee5a04929d41a783dc3 (diff) | |
| parent | a31f812347d1776bbba7a526ccbe55b17b6f480e (diff) | |
| download | perlweeklychallenge-club-4a2e34ea37373e1b9c698ef3d57d4d342e360e23.tar.gz perlweeklychallenge-club-4a2e34ea37373e1b9c698ef3d57d4d342e360e23.tar.bz2 perlweeklychallenge-club-4a2e34ea37373e1b9c698ef3d57d4d342e360e23.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-091/paulo-custodio/README | 1 | ||||
| -rwxr-xr-x | challenge-091/paulo-custodio/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-091/paulo-custodio/perl/ch-1a.pl | 37 | ||||
| -rwxr-xr-x | challenge-091/paulo-custodio/perl/ch-2.pl | 19 | ||||
| -rwxr-xr-x | challenge-091/paulo-custodio/perl/test.pl | 19 |
5 files changed, 92 insertions, 0 deletions
diff --git a/challenge-091/paulo-custodio/README b/challenge-091/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-091/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-091/paulo-custodio/perl/ch-1.pl b/challenge-091/paulo-custodio/perl/ch-1.pl new file mode 100755 index 0000000000..9b587e3867 --- /dev/null +++ b/challenge-091/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +# THE WEEKLY CHALLENGE - 091 +# TASK #1: Count Number +# +# You are given a positive number $N. Write a script to count number and display as you read it. +# Solution with regular expressions +use strict; +use warnings; + +my $N = shift; +while ($N ne '') { + $N =~ s/^((\d)\2*)// or die; + print length($1), $2; +} +print "\n"; diff --git a/challenge-091/paulo-custodio/perl/ch-1a.pl b/challenge-091/paulo-custodio/perl/ch-1a.pl new file mode 100755 index 0000000000..35bdff6ae9 --- /dev/null +++ b/challenge-091/paulo-custodio/perl/ch-1a.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +# THE WEEKLY CHALLENGE - 091 +# TASK #1: Count Number +# +# You are given a positive number $N. Write a script to count number and display as you read it. +# Numeric recursive solution +use strict; +use warnings; + +my $N = shift; +read_number($N); + +sub read_number { + my($n) = @_; + read_number_($n); + print "\n"; +} + +sub read_number_ { + my($n) = @_; + return unless $n > 0; # end condition + + # count number of equal digits at the end + my $digit = $n % 10; + my $count = 0; + while ($n > 0 && ($n % 10) == $digit) { + $n = int($n / 10); + $count++; + } + + # recurse to print top part + read_number_($n); + + # print last digits + print $count,$digit; +} diff --git a/challenge-091/paulo-custodio/perl/ch-2.pl b/challenge-091/paulo-custodio/perl/ch-2.pl new file mode 100755 index 0000000000..73cd42017a --- /dev/null +++ b/challenge-091/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +# THE WEEKLY CHALLENGE - 091 +# TASK #2: Jump Game +# +# 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. +use strict; +use warnings; + +my @N = @ARGV; + +my $pos = 0; +while ($pos < $#N && $N[$pos]) { + $pos += $N[$pos]; +} +print((($pos == $#N) ? 1 : 0), "\n"); diff --git a/challenge-091/paulo-custodio/perl/test.pl b/challenge-091/paulo-custodio/perl/test.pl new file mode 100755 index 0000000000..dc4c10c7cf --- /dev/null +++ b/challenge-091/paulo-custodio/perl/test.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +# perl +is `perl ch-1.pl 1122234`, "21321314\n"; +is `perl ch-1.pl 2333445`, "12332415\n"; +is `perl ch-1.pl 12345`, "1112131415\n"; + +is `perl ch-1a.pl 1122234`, "21321314\n"; +is `perl ch-1a.pl 2333445`, "12332415\n"; +is `perl ch-1a.pl 12345`, "1112131415\n"; + +is `perl ch-2.pl 1 2 1 2`, "1\n"; +is `perl ch-2.pl 2 1 1 0 2`, "0\n"; + +done_testing; |
