diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-01-02 17:50:35 -0500 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-01-02 17:50:35 -0500 |
| commit | 7d5fe26dcbe56945262f6fe651e91dc055c03756 (patch) | |
| tree | 3c891fd7b8c582f47a074a1eee92a9f19991fad8 | |
| parent | 8fc3b368ac11c9b264fc21c5ccf853e4b61118a6 (diff) | |
| download | perlweeklychallenge-club-7d5fe26dcbe56945262f6fe651e91dc055c03756.tar.gz perlweeklychallenge-club-7d5fe26dcbe56945262f6fe651e91dc055c03756.tar.bz2 perlweeklychallenge-club-7d5fe26dcbe56945262f6fe651e91dc055c03756.zip | |
new file: challenge-250/mattneleigh/perl/ch-1.pl
new file: challenge-250/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-250/mattneleigh/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-250/mattneleigh/perl/ch-2.pl | 88 |
2 files changed, 149 insertions, 0 deletions
diff --git a/challenge-250/mattneleigh/perl/ch-1.pl b/challenge-250/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..4f9c1b36e4 --- /dev/null +++ b/challenge-250/mattneleigh/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 0, 1, 2 ], + [ 4, 3, 2, 1 ], + [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: %d\n\n", + join(", ", @{$integer_list}), + match_index_mod_ten(@{$integer_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of integers, determine the first index at which the value stored +# at that index equals the index-mod-10 (i.e. $i % 10 == $ints[$i] ) +# Takes one argument: +# * The list of integers to examine +# Returns on success: +# * The first index at which the value in the list at that index is equal to +# the index mod ten +# Returns on error: +# * -1 if no index/value pair that matches the search criteria is found +################################################################################ +sub match_index_mod_ten{ + + # Loop over every index in the array + for my $i (0 .. $#ARG){ + # Return the first index in which the + # index mod 10 equals the value stored + # at that index + return($i) + if($i % 10 == $ARG[$i]); + } + + # Didn't find a matching value... + return(-1); + +} + + + diff --git a/challenge-250/mattneleigh/perl/ch-2.pl b/challenge-250/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..9e177e4c82 --- /dev/null +++ b/challenge-250/mattneleigh/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @word_lists = ( + [ "perl", "2", "000", "python", "r4ku" ], + [ "001", "1", "000", "0001" ] +); + +print("\n"); +foreach my $word_list (@word_lists){ + printf( + "Input: \@alphanumstr = (%s)\nOutput: %d\n\n", + join( + ", ", + map( + "\"" . $_ . "\"", + @{$word_list} + ) + ), + maximum_alphanumeric_string_value(@{$word_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given an array of alphanumeric strings, determine the highest alphanumeric +# "value" among them, where "value" is defined as the number described by the +# string if it consists solely of digits, OR the length of the string, in +# characters +# Takes one argument: +# * The list of strings to examine (e.g +# ( "perl", "2", "000", "python", "r4ku" ) +# ) +# Returns: +# * The largest numerical "value" of any string in the list, as defined above +# (e.g. 6 ) +################################################################################ +sub maximum_alphanumeric_string_value{ + use List::Util qw(max); + + return( + max( + map( + { + if(m/\D/){ + # String has non-digits... + # Compute its length + length($_); + } else{ + # String only has digits... + # Copy the string so we can modify + # it without affecting @ARG + my $str = $_; + + # Strip leading zeros + $str =~ s/^0+//; + + if(length($str)){ + # Digits remain + $str; + } else{ + # String had only zeros + 0; + } + } + } + @ARG + ) + ) + ); + +} + + + |
