diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-02 08:13:13 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-02 08:13:13 +0000 |
| commit | c80093e548d1d73ed772b2f9fd2ff3ec77685745 (patch) | |
| tree | 388dafb580bdb8cd97618df66963c7f8e5277946 | |
| parent | d18f32d599adb617e7b7bba36d69f06e04db7844 (diff) | |
| parent | 33d1cfc2f6cbf0d363bac5aa8be81c505c4af008 (diff) | |
| download | perlweeklychallenge-club-c80093e548d1d73ed772b2f9fd2ff3ec77685745.tar.gz perlweeklychallenge-club-c80093e548d1d73ed772b2f9fd2ff3ec77685745.tar.bz2 perlweeklychallenge-club-c80093e548d1d73ed772b2f9fd2ff3ec77685745.zip | |
Merge pull request #7189 from zapwai/new-branch
Week 193
| -rw-r--r-- | challenge-193/zapwai/README | 1 | ||||
| -rwxr-xr-x | challenge-193/zapwai/perl/ch-1.pl | 10 | ||||
| -rwxr-xr-x | challenge-193/zapwai/perl/ch-2.pl | 76 |
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."; |
