diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-09-23 03:12:36 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-09-23 03:12:36 -0400 |
| commit | d0ea8aaf1b1f7d4a7c813f01a801714a7ab6d5cf (patch) | |
| tree | f0f8eaad15c38f49e5e5246e7257d4f3815d26f6 /challenge-340 | |
| parent | c410a0181e730b574b7c8f80700d62d3cfb6fdc8 (diff) | |
| download | perlweeklychallenge-club-d0ea8aaf1b1f7d4a7c813f01a801714a7ab6d5cf.tar.gz perlweeklychallenge-club-d0ea8aaf1b1f7d4a7c813f01a801714a7ab6d5cf.tar.bz2 perlweeklychallenge-club-d0ea8aaf1b1f7d4a7c813f01a801714a7ab6d5cf.zip | |
new file: challenge-340/mattneleigh/perl/ch-1.pl
new file: challenge-340/mattneleigh/perl/ch-2.pl
Diffstat (limited to 'challenge-340')
| -rwxr-xr-x | challenge-340/mattneleigh/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-340/mattneleigh/perl/ch-2.pl | 88 |
2 files changed, 149 insertions, 0 deletions
diff --git a/challenge-340/mattneleigh/perl/ch-1.pl b/challenge-340/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..c25026044f --- /dev/null +++ b/challenge-340/mattneleigh/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + "abbaca", + "azxxzy", + "aaaaaaaa", + "aabccba", + "abcddcba" +); + +print("\n"); +foreach my $string (@strings){ + printf( + "Input: \$str = '%s'\nOutput: '%s'\n\n", + $string, + make_duplicate_free($string) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string, repeatedly remove adjacent duplicate letters until no +# adjacent duplicates remain, even those produced by previous removals; if this +# results in the removal of all letters, an empty string will be returned +# Takes one argument: +# * The string to process (e.g. "azxxzy") +# Returns: +# * The string with all duplicates removed as described above (e.g. "ay") +################################################################################ +sub make_duplicate_free{ + my $str = shift(); + + # Substitute an empty string (as opposed to + # a blank expression, to suppress 'uninitialized + # value' warnings) for all instances of two + # adjacent characters; keep doing so until no + # substitutions are made + while($str =~ s/(.)\1/""/ge){ + ;; + } + + return($str); + +} + + + diff --git a/challenge-340/mattneleigh/perl/ch-2.pl b/challenge-340/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..6ac829b19a --- /dev/null +++ b/challenge-340/mattneleigh/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + # Given cases + "The cat has 3 kittens 7 toys 10 beds", + "Alice bought 5 apples 2 oranges 9 bananas", + "I ran 1 mile 2 days 3 weeks 4 months", + "Bob has 10 cars 10 bikes", + "Zero is 0 one is 1 two is 2", + + # Additional test cases + "There are no numbers here" +); + +print("\n"); +foreach my $string (@strings){ + my $rval = all_numbers_ascending($string); + + printf( + "Input: \$str = '%s'\nOutput: %s\n\n", + $string, + $rval ? + "true" + : + defined($rval) ? + "false" + : + "no numbers present" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string, determine whether positive integers therein appear in +# ascending order from left to right +# Takes one argument: +# * The string to examine (e.g. "The cat has 3 kittens 7 toys 10 beds") +# Returns: +# * 0 if all the numbers in the string do NOT appear in ascending order +# * 1 if all the numbers in the string appear in ascending order (as it would +# be in the case of the example above) +# * undef if there are no numbers in the string +################################################################################ +sub all_numbers_ascending{ + + my $previous = undef; + + # Loop over only the numbers in the string + foreach my $number (grep(/^[0-9]+$/, split(" ", shift()))){ + if(defined($previous)){ + # There is a previously-defined number; bail + # if the current number is not greater than + # the previous one + return(0) + unless($number > $previous); + } + + # Store this number to compare with the next + # one + $previous = $number + } + + # Make sure we saw at least one number + return(undef) + unless(defined($previous)); + + # Got here- every number was more than the + # one that preceded it + return(1); + +} + + + |
