aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-20 01:47:59 +0000
committerGitHub <noreply@github.com>2020-12-20 01:47:59 +0000
commitf79bd46f030a18b06956edaaac67efe4c167d5b0 (patch)
tree9f5ad5b1fb745b1bcc437d3ff7b9d3c7fcb66300
parentd231faead6bc3472337e7c8d51b097bd9939a203 (diff)
parent433ea876d5b7ed88ca03c2d3c6ee358d5ab078c6 (diff)
downloadperlweeklychallenge-club-f79bd46f030a18b06956edaaac67efe4c167d5b0.tar.gz
perlweeklychallenge-club-f79bd46f030a18b06956edaaac67efe4c167d5b0.tar.bz2
perlweeklychallenge-club-f79bd46f030a18b06956edaaac67efe4c167d5b0.zip
Merge pull request #3008 from jcrosswh/feature/c091
Feature/c091
-rw-r--r--challenge-091/jcrosswh/perl/ch-1.pl45
-rw-r--r--challenge-091/jcrosswh/perl/ch-2.pl62
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-091/jcrosswh/perl/ch-1.pl b/challenge-091/jcrosswh/perl/ch-1.pl
new file mode 100644
index 0000000000..d9e833e49d
--- /dev/null
+++ b/challenge-091/jcrosswh/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+PWC 091 Challenge 1
+
+=head1 SYNOPSIS
+
+ $ ch-1.pl 1122234
+ 21321314
+
+=head1 DESCRIPTION
+
+Given a positive number $N, this script counts the instances of numbers from 0-9
+and displays the number of occurances.
+
+=head1 SOLUTION
+
+This solution first sanitizes the input to ensure that we are dealing with only
+positive numbers, so any number greater than 0. After that, it loops through
+the digits 0 to 9, and for each digit will fetch out an array of digits if they
+exist using a regex. Finally, simply print the count and the digit if any
+happen to be found.
+
+=head1 AUTHORS
+
+Joel Crosswhite E<lt>joel.crosswhite@ix.netcom.comE<gt>
+
+=cut
+
+my $input = $ARGV[0];
+if (!defined($input) || $input !~ m/^[1-9]\d*$/) {
+ print "Usage: ch-1.pl <positive integer>\n";
+ exit 1;
+}
+
+foreach my $digit (0..9) {
+ my @numbers = ($input =~ /$digit/g);
+ print scalar(@numbers) . $digit if scalar(@numbers) > 0;
+}
+
+exit 0; \ No newline at end of file
diff --git a/challenge-091/jcrosswh/perl/ch-2.pl b/challenge-091/jcrosswh/perl/ch-2.pl
new file mode 100644
index 0000000000..5592ddba70
--- /dev/null
+++ b/challenge-091/jcrosswh/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+PWC 091 Challenge 2
+
+=head1 SYNOPSIS
+
+ $ ch-2.pl 1,2,3
+ 1
+
+ $ ch-2.pl 5,2,3
+ 0
+
+=head1 DESCRIPTION
+
+Given an array of positive numbers @N, where the value at each index determines
+how far you are allowed to jump. This script will decide if you can jump,
+exactly, to the last index. It will print 1 if you are able to reach the last
+index or 0 if you can't.
+
+=head1 SOLUTION
+
+First this script will sanatize the input, making sure that only a list of
+numbers separated by commas was passed in. The script will parse and store
+these numbers in an array. Then we will "walk" the array by trying to jump
+exactly to the end, and if that's not possible, try to jump forward, and if we
+can't do that then inform the user that it's not possible.
+
+=head1 AUTHORS
+
+Joel Crosswhite E<lt>joel.crosswhite@ix.netcom.comE<gt>
+
+=cut
+
+my $input = $ARGV[0];
+if (!defined($input) || $input !~ m/^([1-9],)*[1-9]$/) {
+ print "Usage: ch-2.pl <positive integer>,<positive integer>,...\n";
+ exit 1;
+}
+
+my @N = split(/,/, $input);
+my $length = scalar(@N);
+my $index = 0;
+
+while (1) {
+
+ my $current_number = $N[$index];
+
+ if ($current_number == $length - $index) {
+ print 1 . "\n";
+ exit 0;
+ } elsif ($current_number < $length - $index) {
+ $index += $N[$index];
+ } else {
+ print 0 . "\n";
+ exit 0;
+ }
+}