diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-30 02:27:54 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-30 02:27:54 -0400 |
| commit | 7721dd3f426ad65dde008fde6d2f03243372d7f6 (patch) | |
| tree | 9128284c731e33886fcd8c6d5eccd2c8d5eaf03f | |
| parent | 9a7dfbb26aa64c85ca27a6d18f190b758d7f5c12 (diff) | |
| download | perlweeklychallenge-club-7721dd3f426ad65dde008fde6d2f03243372d7f6.tar.gz perlweeklychallenge-club-7721dd3f426ad65dde008fde6d2f03243372d7f6.tar.bz2 perlweeklychallenge-club-7721dd3f426ad65dde008fde6d2f03243372d7f6.zip | |
new file: challenge-280/mattneleigh/perl/ch-1.pl
new file: challenge-280/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-280/mattneleigh/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-280/mattneleigh/perl/ch-2.pl | 73 |
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-280/mattneleigh/perl/ch-1.pl b/challenge-280/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..fd86cfc08b --- /dev/null +++ b/challenge-280/mattneleigh/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + "acbddbca", + "abccd", + "abcdabbb" +); + +print("\n"); +foreach my $string (@strings){ + printf( + "Input: \$str = \"%s\"\nOutput: \"%s\"\n\n", + $string, + first_duplicate_letter($string) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string of lower-case English letters, determine which letter is the +# first to appear twice +# Takes one argument: +# * A string of lower case English letters (e.g. "abcdabbb") +# Returns: +# * The first letter that appears more than once in the string (e.g. "a") OR +# the empty string if no letter appears at least twice +################################################################################ +sub first_duplicate_letter{ + + my %letters; + + # Loop over each letter + foreach my $letter (split('', shift())){ + # Increment this letter's count + $letters{$letter}++; + + # If this letter has a count of 2, return it + return($letter) + if($letters{$letter} == 2); + } + + # If we got here, no letter appeared twice + return(""); + +} + + + diff --git a/challenge-280/mattneleigh/perl/ch-2.pl b/challenge-280/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..3562b81e62 --- /dev/null +++ b/challenge-280/mattneleigh/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + "p|*e*rl|w**e|*ekly|", + "perl", + "th|ewe|e**|k|l***ych|alleng|e" +); + +print("\n"); +foreach my $string (@strings){ + printf( + "Input: \$str = \"%s\"\nOuput: %d\n\n", + $string, + count_asterix_exclusive_of_pipes($string) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string with sections delimited by pairs of vertical bar/pipe +# characters ('|') count the number of asterisk characters ('*') that are NOT +# between a pair of vertical bars/pipes +# Takes one argument: +# * A string that contains a number of sections denoted by pairs of vertical +# bar/pipe characters, and asterisk characters (e.g. "p|*e*rl|w**e|*ekly|" ) +# Returns: +# * The count of asterisk characters that are not between pairs of vertical +# bar/pipe characters (e.g. 2 ) +################################################################################ +sub count_asterix_exclusive_of_pipes{ + + # Divide the string into 'words' separated by + # the '|' character + my @words = split(/\|/, shift()); + + # Start this at -2 because it gets added to + # at the start of each loop, later + my $i = -2; + + my $limit = $#words; + my $asterix_ct = 0; + + # Loop over each word until we've passed the + # end of the list + while(($i += 2) <= $limit){ + # Loop over each character within the word + foreach my $chr (split(//, $words[$i])){ + # Count each asterix + $asterix_ct++ + if($chr eq "*"); + } + } + + return($asterix_ct); + +} + + + |
