diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-05-29 18:46:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-29 18:46:09 +0100 |
| commit | a93e7617c70633327c59176e4002f49332d64fa0 (patch) | |
| tree | 9af3ad9954de27450ab7f5b00a7b1ca6be06e3ec | |
| parent | dd248c018eaa8d8a104c033154bf7e3b1ef18828 (diff) | |
| parent | f54f73668bf30ba3d0bc377aaad70b2274677528 (diff) | |
| download | perlweeklychallenge-club-a93e7617c70633327c59176e4002f49332d64fa0.tar.gz perlweeklychallenge-club-a93e7617c70633327c59176e4002f49332d64fa0.tar.bz2 perlweeklychallenge-club-a93e7617c70633327c59176e4002f49332d64fa0.zip | |
Merge pull request #6175 from mattneleigh/pwc166
new file: challenge-166/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-166/mattneleigh/perl/ch-1.pl | 141 | ||||
| -rwxr-xr-x | challenge-166/mattneleigh/perl/ch-2.pl | 94 |
2 files changed, 235 insertions, 0 deletions
diff --git a/challenge-166/mattneleigh/perl/ch-1.pl b/challenge-166/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..443fdd1887 --- /dev/null +++ b/challenge-166/mattneleigh/perl/ch-1.pl @@ -0,0 +1,141 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my %words_by_length; +my @words; + +# Read in all lines of input from STDIN- must +# be one word per line +while(<>){ + my $length; + my $hexed; + + chomp; + + # Skip 7 as there will be no words of + # length 1 to add + $length = length($_); + next + if(7 == $length); + + # Catalog each successfully hexified word + # by its length; store both the hexified + # and original (but capitalized) versions + $hexed = hexify_text($_); + push(@{$words_by_length{$length}}, [$hexed, uc($_)]) + if(defined($hexed)); +} + +# Now, make some 8-character combos... +# General cases- 2, 3 +for(2 .. 3){ + foreach my $a (@{$words_by_length{$_}}){ + foreach my $b (@{$words_by_length{8 - $_}}){ + # Add both possible combinations + push( + @words, + [$a->[0].$b->[0], $a->[1].$b->[1]], + [$b->[0].$a->[0], $b->[1].$a->[1]] + ); + } + } +} + +# Special case- 4 +for my $a (0 .. ($#{$words_by_length{4}} - 1)){ + for my $b ($a .. $#{$words_by_length{4}}){ + push( + @words, + [ + $words_by_length{4}[$a][0].$words_by_length{4}[$b][0], + $words_by_length{4}[$a][1].$words_by_length{4}[$b][1] + ], + # If $a and $b are the same, the combo only + # needs to be added once... + ($a == $b) ? () : + [ + $words_by_length{4}[$b][0].$words_by_length{4}[$a][0], + $words_by_length{4}[$b][1].$words_by_length{4}[$a][1] + ] + ); + } +} + +# Special case- 8 +push(@words, splice(@{$words_by_length{8}}, 0)); + +# Sort the finished word list in order by the +# un-hexed string +@words = sort({ $a->[1] cmp $b->[1] } @words); + +# Produce output +foreach(@words){ + print($_->[0], " (", $_->[1], ")\n"); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Convert a word into a hexadecimal-friendly version, if possible +# Takes one required argument and one optional argument: +# * The string to convert, which must be at least two (2) characters long, but +# not more than eight (8) characters long (e.g. "accolade") +# * An optional argument that, if defined, enables expanded hex conversions +# (see below) +# Returns on success: +# * The converted string (e.g. "ACC01ADE"); the alphabetic characters in this +# string will be uppper case +# Returns on error: +# * undef if the string contains characters for which no conversion is +# specified, or is not of the correct length +# NOTE: The conversions performed are: +# O -> 0 +# L -> 1 +# I -> 1 +# S -> 5 +# T -> 7 +# Expanded conversions also include: +# Z -> 2 +# G -> 6 +################################################################################ +sub hexify_text{ + my $text = uc(shift()); + my $expanded = shift(); + + # Return undef if the text is not within + # specified length bounds + return(undef) + if((length($text) < 2) || (length($text) > 8)); + + # Substitute some characters + if(defined($expanded)){ + # Expanded substitutions + $text =~ tr/[OLIZSGT]/[0112567]/; + } else{ + # Standard substitutions + $text =~ tr/[OLIST]/[01157]/; + } + + # Return undef if here were non-hex + # characters after substitution + return(undef) + if($text =~ m/[^0-9A-F]/); + + return($text); + +} + + + diff --git a/challenge-166/mattneleigh/perl/ch-2.pl b/challenge-166/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..c02ed6b5ca --- /dev/null +++ b/challenge-166/mattneleigh/perl/ch-2.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @dirs = splice(@ARGV, 0); +my %dir_contents_table; +my $max_length = 0; +my $output_format; + +# The original problem specified three directories but +# this system will work with two or more +die("E: must provide at least two directories\n") + if(scalar(@dirs) < 2); + +# Alphabetize directories +@dirs = sort(@dirs); + +# Loop over the specified directories +foreach my $dir (@dirs){ + my $dirhandle; + my $length = length($dir); + + # Update the max file/dir name length seen + $max_length = $length + if($length > $max_length); + + opendir($dirhandle, $dir) + or die("E: Could not open directory $dir: $!"); + + # Loop over the contents of the directory + while(my $file = readdir($dirhandle)){ + # Skip "." and ".." + next if($file =~ m/^\.{1,2}$/); + + # If the file is a directory, append a slash + $file .= "/" + if( -d $dir."/".$file); + + # Update the max file/dir name length seen + $length = length($file); + $max_length = $length + if($length > $max_length); + + # Note that we saw a file with this name in this dir + $dir_contents_table{$file}{$dir} = 1; + } + + closedir($dirhandle) + or die("E: Could not close directory $dir: $!"); + +} + + +# Set up an output format befitting the size and amount +# of data we've examined +$output_format = join( + " | ", + map("%-" . $max_length . "s", 0 .. $#dirs) +) . "\n"; + +# Print out a header with dir names +print("\n"); +printf($output_format, @dirs); +printf($output_format, map("-" x $max_length, 0 .. $#dirs)); + +# Look up and output the filenames that were not found +# in every specified directory +foreach my $file (sort(keys(%dir_contents_table))){ + if(scalar(keys(%{$dir_contents_table{$file}})) != scalar(@dirs)){ + # There was at least one dir that did not have this file + printf( + $output_format, + # If $dir_contents_table{$file}{$_} is true, that file + # had been seen in the directory named in $_ + map($dir_contents_table{$file}{$_} ? $file : "", @dirs) + ); + } +} + +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + |
