aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2022-12-01 21:45:21 -0500
committerDavid Ferrone <zapwai@gmail.com>2022-12-01 21:45:21 -0500
commit33d1cfc2f6cbf0d363bac5aa8be81c505c4af008 (patch)
tree162c910c2360ded36b78adcff8b7a7b14513e65c
parenta21adbf5d755f19bbcd6361ca5454336e72acfca (diff)
downloadperlweeklychallenge-club-33d1cfc2f6cbf0d363bac5aa8be81c505c4af008.tar.gz
perlweeklychallenge-club-33d1cfc2f6cbf0d363bac5aa8be81c505c4af008.tar.bz2
perlweeklychallenge-club-33d1cfc2f6cbf0d363bac5aa8be81c505c4af008.zip
Week 193
-rw-r--r--challenge-193/zapwai/README1
-rwxr-xr-xchallenge-193/zapwai/perl/ch-1.pl10
-rwxr-xr-xchallenge-193/zapwai/perl/ch-2.pl76
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-193/zapwai/README b/challenge-193/zapwai/README
new file mode 100644
index 0000000000..707f6658ea
--- /dev/null
+++ b/challenge-193/zapwai/README
@@ -0,0 +1 @@
+Solution by David Ferrone.
diff --git a/challenge-193/zapwai/perl/ch-1.pl b/challenge-193/zapwai/perl/ch-1.pl
new file mode 100755
index 0000000000..78d7f49bb6
--- /dev/null
+++ b/challenge-193/zapwai/perl/ch-1.pl
@@ -0,0 +1,10 @@
+#!/usr/bin/env perl
+my $n = $ARGV[0] || 3;
+die ("Not positive enough") if $n < 1;
+my $N = 2**$n - 1;
+my $ops = "%0".$n."b, ";
+print "Input: \$n = $n\n";
+print "Output: ";
+printf $ops, $_ for 0..$N-1;
+printf "%0".$n."b", $N;
+print "\n";
diff --git a/challenge-193/zapwai/perl/ch-2.pl b/challenge-193/zapwai/perl/ch-2.pl
new file mode 100755
index 0000000000..b16da729fb
--- /dev/null
+++ b/challenge-193/zapwai/perl/ch-2.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+#use v5.30.0;
+use feature 'say';
+no warnings;
+## Task 2, find the odd string in the list of strings.
+## Find the difference array of each string, then pick the strange one.
+# https://gist.github.com/manwar/51d6144d28cf984da81645f779ce2932
+
+my @s = ("adc", "wzy", "abc");
+#my @s = ("aaa", "bob", "ccc", "ddd");
+#my @s = ("adccc", "wzyyy", "abcde");
+
+print "Input: \@s = (";
+for (0 .. $#s - 1) {
+ print '"'.$s[$_].'", ';
+}
+print '"'.$s[$#s].'"';
+say ")";
+
+my $alphabet = "abcdefghijklmnopqrstuvwxyz";
+my @alph = split //, $alphabet;
+
+my %hash = map { $alph[$_] => $_ + 1 } 0..25;
+
+sub diff {
+ my ($let1, $let2) = @_;
+ $hash{$let1} - $hash{$let2}
+}
+
+my $output; # For pretty formatting
+sub array { # Returns a difference array
+ my $word = shift;
+ $output .= "Difference array for \"$word\"";
+ my $gap = length($s[0]) - 1;
+ my $spacer = "\t" x 3;
+ $spacer .= " " x $gap;
+ $spacer .= " => [ ";
+ my @letters = split //, $word;
+ $output .= " => [ ";
+ for (1..$#letters-1) {
+ $output .= $letters[$_]." - ".$letters[$_ - 1].", ";
+ }
+ $output .= $letters[$#letters]." - ".$letters[$#letters - 1]." ]\n";
+ $output .= $spacer;
+ for (1..$#letters-1) {
+ $output .= "$hash{$letters[$_]} - $hash{$letters[$_ - 1]}, ";
+ }
+ $output .= "$hash{$letters[$#letters]} - $hash{$letters[$#letters - 1]} ]\n";
+ my @diff_array;
+ $output .= $spacer;
+ for (1..$#letters-1) {
+ $output .= $hash{$letters[$_]} - $hash{$letters[$_ - 1]}.", ";
+ push @diff_array, $hash{$letters[$_]} - $hash{$letters[$_ - 1]};
+ }
+ $output .= $hash{$letters[$#letters]} - $hash{$letters[$#letters - 1]}." ]\n\n";
+ push @diff_array, $hash{$letters[$#letters]} - $hash{$letters[$#letters - 1]};
+ @diff_array
+}
+
+my (@common_array, @odd_array);
+my $odd_index=0;
+for (0 .. $#s) {
+ my @new_array = array($s[$_]);
+ if ($_ == 0) {
+ @common_array = @new_array;
+ @odd_array = @new_array;
+ }
+ next if (@new_array ~~ @common_array);
+ @odd_array = @new_array;
+ $odd_index = $_;
+}
+
+my $ans = $s[$odd_index];
+say "Output: \"$ans\"\n";
+print $output;
+say "The difference array for \"$ans\" is the odd one.";