diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-08-17 23:02:49 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-08-17 23:02:49 -0400 |
| commit | 393924c4402cfb724256bf1f21e91e2555bb85d3 (patch) | |
| tree | 8d374864504c2fa5f27e4cf393b95a949ac7bcda | |
| parent | 348c9486780ed811d63fa83ecadaddf2bd540cef (diff) | |
| download | perlweeklychallenge-club-393924c4402cfb724256bf1f21e91e2555bb85d3.tar.gz perlweeklychallenge-club-393924c4402cfb724256bf1f21e91e2555bb85d3.tar.bz2 perlweeklychallenge-club-393924c4402cfb724256bf1f21e91e2555bb85d3.zip | |
new file: challenge-282/mattneleigh/perl/ch-1.pl
new file: challenge-282/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-282/mattneleigh/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-282/mattneleigh/perl/ch-2.pl | 60 |
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-282/mattneleigh/perl/ch-1.pl b/challenge-282/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..baa4a90011 --- /dev/null +++ b/challenge-282/mattneleigh/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @ints = ( + 12344456, + 1233334, + 10020003 +); + +print("\n"); +foreach my $int (@ints){ + printf( + "Input: \$int = %d\nOutput: \"%s\"\n\n", + $int, + find_good_digit($int) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a positive integer with three or more digits, find the first Good +# Integer within- that is, a block of exactly three repeating digits +# Takes one argument: +# * The positive integer to examine (e.g. 12344456 ) +# Returns on success +# * The first Good Integer found within the number (e.g. 444 ) +# Returns on failure +# * -1 if there was no Good Integer within the number +################################################################################ +sub find_good_digit{ + + my $prev_digit = ""; + my $count = 1; + + # Loop over each digit + foreach my $digit (split(//, shift())){ + if($digit eq $prev_digit){ + # This digit matches the previous digit- + # increment the count + $count++; + } else{ + # This digit does not match the previous + # digit- if the count for the previus digit + # was 3, return a block of three of that + # digit + return($prev_digit x 3) + if($count == 3); + + # Store the new digit and reset the + # count + $prev_digit = $digit; + $count = 1; + } + } + + # If we got here, there was no Good Integer + return(-1); + +} + + + diff --git a/challenge-282/mattneleigh/perl/ch-2.pl b/challenge-282/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..8869d0e39e --- /dev/null +++ b/challenge-282/mattneleigh/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + "pPeERrLl", + "rRr", + "GoO" +); + +print("\n"); +foreach my $string (@strings){ + printf( + "Input: \$str = '%s'\nOuput: %d\n\n", + $string, + count_key_changes($string) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given an alphabetic string, determine the number of times the user who typed +# it had to change keys on their keyboard- not counting shift or caps lock- to +# input it +# Takes one argument: +# * The string to examine (e.g. "pPeERrLl") +# Returns: +# * The number of key changes requires to input the string (e.g. 3 ) +################################################################################ +sub count_key_changes{ + + my @chars = split(//, lc(shift())); + my $changes = 0; + + # Loop over each lower-cased character + foreach my $i (1 .. $#chars){ + # Increment the change count if this character + # differs from the previous one + $changes++ + unless($chars[$i - 1] eq $chars[$i]); + } + + return($changes); + +} + + + |
