aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Crosswhite <joel.crosswhite@ix.netcom.com>2020-12-19 07:44:06 -0700
committerJoel Crosswhite <joel.crosswhite@ix.netcom.com>2020-12-19 07:44:06 -0700
commitdae8b253d0ac8bc1e0e46432ee81e59d07b2fa94 (patch)
tree174c334c3219db0011b0d16376782ab9cb94f448
parent8503dc5936c2dbd8e990ac86030b1e34cfe51a5d (diff)
downloadperlweeklychallenge-club-dae8b253d0ac8bc1e0e46432ee81e59d07b2fa94.tar.gz
perlweeklychallenge-club-dae8b253d0ac8bc1e0e46432ee81e59d07b2fa94.tar.bz2
perlweeklychallenge-club-dae8b253d0ac8bc1e0e46432ee81e59d07b2fa94.zip
Added solution for challenge 1 for week 91.
-rw-r--r--challenge-091/jcrosswh/perl/ch-1.pl45
1 files changed, 45 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