diff options
| author | Packy Anderson <packy@cpan.org> | 2024-04-10 23:57:00 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-04-10 23:57:00 -0400 |
| commit | eff4875d6511eb1398a2473bd97df69beb51a002 (patch) | |
| tree | 9c64605365b732587b3cdf2e223510366535d800 | |
| parent | 400ce1c82600761437bbc1e58b598418fdcd2343 (diff) | |
| download | perlweeklychallenge-club-eff4875d6511eb1398a2473bd97df69beb51a002.tar.gz perlweeklychallenge-club-eff4875d6511eb1398a2473bd97df69beb51a002.tar.bz2 perlweeklychallenge-club-eff4875d6511eb1398a2473bd97df69beb51a002.zip | |
Challenge 264 solutions by Packy Anderson
* Raku that looks like Perl
* Perl
* Python that looks like Perl
1 Blog post
| -rw-r--r-- | challenge-264/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-264/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/perl/ch-2.pl | 37 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/python/ch-1.py | 49 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/raku/ch-1.raku | 48 | ||||
| -rwxr-xr-x | challenge-264/packy-anderson/raku/ch-2.raku | 37 |
8 files changed, 257 insertions, 1 deletions
diff --git a/challenge-264/packy-anderson/README.md b/challenge-264/packy-anderson/README.md index 372a505456..69bfe1aeeb 100644 --- a/challenge-264/packy-anderson/README.md +++ b/challenge-264/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Merge the Target Index Items](https://packy.dardan.com/b/KC) +[I'm The Greatest Target!](https://packy.dardan.com/b/KL) diff --git a/challenge-264/packy-anderson/blog.txt b/challenge-264/packy-anderson/blog.txt new file mode 100644 index 0000000000..b21e66c50d --- /dev/null +++ b/challenge-264/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/KL
\ No newline at end of file diff --git a/challenge-264/packy-anderson/perl/ch-1.pl b/challenge-264/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..f20f36cefa --- /dev/null +++ b/challenge-264/packy-anderson/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( maxstr ); + +sub greatestEnglishLetter($str) { + my %seen; + my @greatest; + + # find the characters that exist as both + # upper and lower case in the string + foreach my $c ( split //, $str ) { + + # note that we've seen the character + $seen{$c} = 1; + + # swap the case of the character + my $C = ($c =~ tr/a-zA-Z/A-Za-z/r); + + # if we've seen the swapped case version of the char, + # add the uppercase version to our greatest hits + push @greatest, uc($c) if exists $seen{$C}; + } + + # if we found greatest characters, + # return the greater of them + if (@greatest) { + return maxstr(@greatest); + } + # otherwise, return something that + # represents an empty result + return q{''}; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + my $output = greatestEnglishLetter($str); + say "Output: $output"; +} + +say "Example 1:"; +solution('PeRlwEeKLy'); + +say "\nExample 2:"; +solution('ChaLlenge'); + +say "\nExample 3:"; +solution('The');
\ No newline at end of file diff --git a/challenge-264/packy-anderson/perl/ch-2.pl b/challenge-264/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..4429dbe49c --- /dev/null +++ b/challenge-264/packy-anderson/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use v5.38; + +sub targetArray($source, $indices) { + my @target; + my @explain; + + foreach my $i ( 0 .. $#{$indices}) { + splice(@target, $indices->[$i], 0, $source->[$i]); + push @explain, [ + $source->[$i], $indices->[$i], [ @target ] + ]; + } + return \@target, \@explain; +} + +sub solution($source, $indices) { + say 'Input: @source = (' . join(', ', @$source) . ')'; + say ' @indices = (' . join(', ', @$indices) . ')'; + my ($output, $explain) = targetArray($source, $indices); + say 'Output: (' . join(', ', @$output) . ")\n"; + + say '@source @indices @target'; + foreach my $row ( @$explain ) { + say "$row->[0] $row->[1] (" . + join(', ', @{$row->[2]}) . ')'; + } +} + +say "Example 1:"; +solution([0, 1, 2, 3, 4], [0, 1, 2, 2, 1]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 0], [0, 1, 2, 3, 0]); + +say "\nExample 3:"; +solution([1], [0]);
\ No newline at end of file diff --git a/challenge-264/packy-anderson/python/ch-1.py b/challenge-264/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..694137a256 --- /dev/null +++ b/challenge-264/packy-anderson/python/ch-1.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +# make a translation table to switch the case of +# English letters +transTable = str.maketrans( + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' +) + +def greatestEnglishLetter(strVar): + seen = {} + greatest = [] + + # find the characters that exist as both + # upper and lower case in the string + for c in strVar: + + # note that we've seen the character + seen[c] = 1 + + # swap the case of the character + C = c.translate(transTable) + + # if we've seen the swapped case version of the char, + # add the uppercase version to our greatest hits + if C in seen: + greatest.append(c.upper()) + + # if we found greatest characters, + # return the greater of them + if greatest: + return max(greatest) + + # otherwise, return something that + # represents an empty result + return "''" + +def solution(strVar): + print(f"Input: '{strVar}'") + print(f"Output: {greatestEnglishLetter(strVar)}") + +print('Example 1:') +solution('PeRlwEeKLy') + +print('\nExample 2:') +solution('ChaLlenge') + +print('\nExample 3:') +solution('The')
\ No newline at end of file diff --git a/challenge-264/packy-anderson/python/ch-2.py b/challenge-264/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..2b0fecb467 --- /dev/null +++ b/challenge-264/packy-anderson/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +def targetArray(source, indices): + target = [] + explain = [] + + for i in range(len(indices)): + target.insert(indices[i], source[i]) + explain.append([ + source[i], indices[i], target.copy() + ]) + return target, explain + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(source, indices): + print(f'Input: @source = ({comma_join(source)})') + print(f' @indices = ({comma_join(indices)})') + output, explain = targetArray(source, indices) + print(f'Output: ({comma_join(output)})\n') + print('@source @indices @target') + for row in explain: + print( + f'{row[0]} {row[1]} ' + + f'({comma_join(row[2])})' + ) + +print('Example 1:') +solution([0, 1, 2, 3, 4], [0, 1, 2, 2, 1]) + +print('\nExample 2:') +solution([1, 2, 3, 4, 0], [0, 1, 2, 3, 0]) + +print('\nExample 3:') +solution([1], [0])
\ No newline at end of file diff --git a/challenge-264/packy-anderson/raku/ch-1.raku b/challenge-264/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..396519617a --- /dev/null +++ b/challenge-264/packy-anderson/raku/ch-1.raku @@ -0,0 +1,48 @@ +#!/usr/bin/env raku +use v6; + +sub greatestEnglishLetter($str) { + my %seen; + my @greatest; + + # find the characters that exist as both + # upper and lower case in the string + for $str.split('', :skip-empty) -> $c { + + # note that we've seen the character + %seen{$c} = 1; + + # swap the case of the character + my $C = $c.trans( + ['a' .. 'z', 'A' .. 'Z'] => ['A' .. 'Z', 'a' .. 'z'] + ); + + # if we've seen the swapped case version of the char, + # add the uppercase version to our greatest hits + @greatest.push: $c.uc if %seen{$C}:exists; + } + + # if we found greatest characters, + # return the greater of them + if (@greatest) { + return @greatest.max; + } + # otherwise, return something that + # represents an empty result + return q{''}; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + my $output = greatestEnglishLetter($str); + say "Output: $output"; +} + +say "Example 1:"; +solution('PeRlwEeKLy'); + +say "\nExample 2:"; +solution('ChaLlenge'); + +say "\nExample 3:"; +solution('The');
\ No newline at end of file diff --git a/challenge-264/packy-anderson/raku/ch-2.raku b/challenge-264/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..723dee4850 --- /dev/null +++ b/challenge-264/packy-anderson/raku/ch-2.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku +use v6; + +sub targetArray(@source, @indices) { + my @target; + my @explain; + + for 0..@indices.end -> $i { + @target.splice(@indices[$i], 0, @source[$i]); + @explain.push: [ + @source[$i], @indices[$i], @target.clone + ]; + } + return @target, @explain; +} + +sub solution(@source, @indices) { + say 'Input: @source = (' ~ @source.join(', ') ~ ')'; + say ' @indices = (' ~ @indices.join(', ') ~ ')'; + my (@output, @explain) := targetArray(@source, @indices); + say 'Output: (' ~ @output.join(', ') ~ ")\n"; + + say '@source @indices @target'; + for @explain -> @row { + say "@row[0] @row[1] (" ~ + @row[2].join(', ') ~ ')'; + } +} + +say "Example 1:"; +solution([0, 1, 2, 3, 4], [0, 1, 2, 2, 1]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 0], [0, 1, 2, 3, 0]); + +say "\nExample 3:"; +solution([1], [0]);
\ No newline at end of file |
