diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-02-05 03:55:41 -0500 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-02-05 03:55:41 -0500 |
| commit | bc55d00ca1d2896f8f7fe87e7f5b26df1fefcd85 (patch) | |
| tree | 59d5baffc9e705644d4c4cc03f7d6ab43922f850 /challenge-255/mattneleigh/perl/ch-1.pl | |
| parent | d58258463850d2949fe13a4097a83a5d17951548 (diff) | |
| download | perlweeklychallenge-club-bc55d00ca1d2896f8f7fe87e7f5b26df1fefcd85.tar.gz perlweeklychallenge-club-bc55d00ca1d2896f8f7fe87e7f5b26df1fefcd85.tar.bz2 perlweeklychallenge-club-bc55d00ca1d2896f8f7fe87e7f5b26df1fefcd85.zip | |
new file: challenge-255/mattneleigh/perl/ch-1.pl
new file: challenge-255/mattneleigh/perl/ch-2.pl
Diffstat (limited to 'challenge-255/mattneleigh/perl/ch-1.pl')
| -rwxr-xr-x | challenge-255/mattneleigh/perl/ch-1.pl | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/challenge-255/mattneleigh/perl/ch-1.pl b/challenge-255/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..5d10cd0028 --- /dev/null +++ b/challenge-255/mattneleigh/perl/ch-1.pl @@ -0,0 +1,104 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @string_pairs = ( + # Given cases + [ "Perl", "Preel" ], + [ "Weekly", "Weeakly" ], + [ "Box", "Boxy" ], + + # Additional test cases + [ "Pizza", "Pizzeria" ], + [ "No", "Match" ], + [ "Peer", "Peerless" ] +); + +print("\n"); +foreach my $string_pair (@string_pairs){ + printf( + "Input: \$s = \"%s\"\n \$t = \"%s\"\nOutput: %s\n\n", + @{$string_pair}, + join( + ", ", + find_additional_characters(@{$string_pair}) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two strings, find the characters in the second that are NOT present in +# the first, with quantity of characters being taken into account rather than +# mere presence alone (see below) +# Takes two arguments: +# * The first string (e.g. "Peer" ) +# * The second string (e.g. "Peerless" ) +# Returns: +# * A lexicographically sorted list of characters that are present in the +# second string but not in the first (e.g. ( "e", "l", "s", "s" ) ). Note +# that 'e' is included once and 's' is included twice as that represents the +# number of times each appears more in the second string than in the first. +################################################################################ +sub find_additional_characters{ + + my $char; + my %chars; + + # Make a table of character counts from the second + # argument + foreach $char (split('', $ARG[1])){ + if($chars{$char}){ + $chars{$char}++; + } else{ + $chars{$char} = 1; + } + } + + # Decrement (or remove entirely) the counts of + # characters that appear in the first argument + foreach $char (split('', $ARG[0])){ + if(exists($chars{$char})){ + $chars{$char}--; + delete($chars{$char}) + unless($chars{$char}); + } + } + + # Return a lexicographically sorted list of + # remaining characters + return( + sort( + # Make a list of remaining characters + map( + { + $char = $_; + + # Repeat the character the appropriate + # number of times + map( + $char, + 1 .. $chars{$char} + ) + } + keys(%chars) + ) + ) + ); + +} + + + |
