diff options
| author | Packy Anderson <packy@cpan.org> | 2024-02-08 23:24:12 -0500 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-02-08 23:24:12 -0500 |
| commit | bd4587f8255829c614760ed8739ed8a97aa3624a (patch) | |
| tree | ae7a03d717655362aa54d7bf4ab6977acc38250e | |
| parent | 1f309d1dfc8eb0a2ba3c5dc2338e22d95b5efaeb (diff) | |
| download | perlweeklychallenge-club-bd4587f8255829c614760ed8739ed8a97aa3624a.tar.gz perlweeklychallenge-club-bd4587f8255829c614760ed8739ed8a97aa3624a.tar.bz2 perlweeklychallenge-club-bd4587f8255829c614760ed8739ed8a97aa3624a.zip | |
Challenge 255 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
| -rw-r--r-- | challenge-255/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-255/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/perl/ch-2.pl | 58 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/python/ch-1.py | 35 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/python/ch-2.py | 59 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/raku/ch-1.raku | 35 | ||||
| -rwxr-xr-x | challenge-255/packy-anderson/raku/ch-2.raku | 61 |
8 files changed, 285 insertions, 1 deletions
diff --git a/challenge-255/packy-anderson/README.md b/challenge-255/packy-anderson/README.md index e6afe2677b..a3f30107c3 100644 --- a/challenge-255/packy-anderson/README.md +++ b/challenge-255/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Reverse Vowels by the Power of Three](https://packy.dardan.com/b/H8) +[Reverse Vowels by the Power of Three](https://packy.dardan.com/b/HM) diff --git a/challenge-255/packy-anderson/blog.txt b/challenge-255/packy-anderson/blog.txt new file mode 100644 index 0000000000..6ead488ffa --- /dev/null +++ b/challenge-255/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/HM
\ No newline at end of file diff --git a/challenge-255/packy-anderson/perl/ch-1.pl b/challenge-255/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..b9b774fa72 --- /dev/null +++ b/challenge-255/packy-anderson/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use v5.38; + +sub oddChar($s, $t) { + # count the characters in $s + my %count; + foreach my $c ( split(//, lc($s)) ) { + $count{$c}++; + } + # find the character in $t that's been added to $s + foreach my $c ( split(//, lc($t)) ) { + if ( ! exists $count{$c} ) { + # we found the added character! + return $c + } + $count{$c}--; + delete $count{$c} if $count{$c} == 0; + } + die "No odd character found!"; +} + +sub solution($s, $t) { + say qq/Input: \$s = "$s" \$t = "$t"/; + my $o = oddChar($s, $t); + say qq/Output: "$o"/; +} + +say "Example 1:"; +solution("Perl", "Preel"); + +say "\nExample 2:"; +solution("Weekly", "Weeakly"); + +say "\nExample 3:"; +solution("Box", "Boxy");
\ No newline at end of file diff --git a/challenge-255/packy-anderson/perl/ch-2.pl b/challenge-255/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..4d9aa8d56e --- /dev/null +++ b/challenge-255/packy-anderson/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +use v5.38; + +sub frequentWord($p, $w) { + # first, remove punctuation from the paragraph + # (but we'll leave apostrophes for contractions) + $p =~ s/[^a-z'\s]//ig; + + # count the words in $paragraph + my %count; + foreach my $pw ( split(/\s+/, lc($p)) ) { + $count{$pw}++; + } + + # generate the output about the banned word + my $bannedCount = $count{$w} // 0; + my $output = qq/The banned word "$w" occurs $bannedCount /; + $output .= ($bannedCount == 1) ? 'time' : 'times'; + $output .= ".\n"; + + # delete the banned word from the word count + delete $count{$w}; + + # now find the most frequent word left and report on that + my @sorted = sort { + # sort by count + $count{$b} <=> $count{$a} + } keys %count; + my $freqWord = $sorted[0]; + my $freqCount = $count{$freqWord}; + + $output .= qq/The other word "$freqWord" /; + $output .= qq/occurs $freqCount /; + $output .= ($freqCount == 1) ? 'time.' : 'times.'; + + return $freqWord, $output; +} + +sub solution($p, $w) { + say qq/Input: \$p = "$p"/; + say qq/ \$w = "$w"/; + my ($freqWord, $output) = frequentWord($p, $w); + say qq/Output: "$freqWord"/; + say ""; + say $output; +} + +say "Example 1:"; +solution( + "Joe hit a ball, the hit ball flew far after it was hit.", + "hit" +); + +say "\nExample 2:"; +solution( + "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + "the" +); diff --git a/challenge-255/packy-anderson/python/ch-1.py b/challenge-255/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..8d4e8b5ab7 --- /dev/null +++ b/challenge-255/packy-anderson/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import sys +from collections import Counter + +def oddChar(s, t): + # count the characters in s + count = Counter() + for c in s.lower(): + count[c] += 1 + + # find the character in t that's been added to s + for c in t.lower(): + if c not in count: + # we found the added character! + return c + count[c] -= 1 + if count[c] == 0: + del count[c] + + sys.exit("No odd character found!") + +def solution(s, t): + print(f'Input: $s = "{s}" $t = "{t}"') + o = oddChar(s, t) + print(f'Output: "{o}"') + +print('Example 1:') +solution("Perl", "Preel") + +print('\nExample 2:') +solution("Weekly", "Weeakly") + +print('\nExample 3:') +solution("Box", "Boxy")
\ No newline at end of file diff --git a/challenge-255/packy-anderson/python/ch-2.py b/challenge-255/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..5e34538e2a --- /dev/null +++ b/challenge-255/packy-anderson/python/ch-2.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +import re +from collections import Counter + +def frequentWord(p, w): + # first, remove punctuation from the paragraph + # (but we'll leave apostrophes for contractions) + p = re.sub(r'[^a-z\'\s]', '', p.lower()) + + # count the words in $paragraph + count = Counter() + for pw in p.split(): + count[pw] += 1 + + # generate the output about the banned word + bannedCount = count[w] if w in count else 0 + output = f'The banned word "{w}" occurs {bannedCount} '; + output += 'time' if bannedCount == 1 else 'times' + output += ".\n" + + # delete the banned word from the word count + del count[w] + + # now find the most frequent word left and report on that + decorated = [ ( count[w], w ) for w in count.keys() ] + sorted_tuples = sorted( + decorated, + # the - before the first element sorts descending + key=lambda k: -k[0] + ) + freqWord = sorted_tuples[0][1] + freqCount = count[freqWord] + + output += f'The other word "{freqWord}" ' + output += f'occurs {freqCount} ' + output += 'time.' if freqCount == 1 else 'times.' + + return (freqWord, output) + +def solution(p, w): + print(f'Input: $p = "{p}"') + print(f' $w = "{w}"') + (freqWord, output) = frequentWord(p, w) + print(f'Output: "{freqWord}"') + print('') + print(output) + +print('Example 1:') +solution( + "Joe hit a ball, the hit ball flew far after it was hit.", + "hit" +) + +print('\nExample 2:') +solution( + "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + "the" +)
\ No newline at end of file diff --git a/challenge-255/packy-anderson/raku/ch-1.raku b/challenge-255/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..4404a8c7c6 --- /dev/null +++ b/challenge-255/packy-anderson/raku/ch-1.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use v6; + +sub oddChar($s, $t) { + # count the characters in $s + my %count; + for $s.lc.split('', :skip-empty) -> $c { + %count{$c}++; + } + # find the character in $t that's been added to $s + for $t.lc.split('', :skip-empty) -> $c { + if ( %count{$c}:!exists ) { + # we found the added character! + return $c + } + %count{$c}--; + %count{$c}:delete if %count{$c} == 0; + } + die "No odd character found!"; +} + +sub solution($s, $t) { + say qq/Input: \$s = "$s" \$t = "$t"/; + my $o = oddChar($s, $t); + say qq/Output: "$o"/; +} + +say "Example 1:"; +solution("Perl", "Preel"); + +say "\nExample 2:"; +solution("Weekly", "Weeakly"); + +say "\nExample 3:"; +solution("Box", "Boxy");
\ No newline at end of file diff --git a/challenge-255/packy-anderson/raku/ch-2.raku b/challenge-255/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..1395e625be --- /dev/null +++ b/challenge-255/packy-anderson/raku/ch-2.raku @@ -0,0 +1,61 @@ +#!/usr/bin/env raku +use v6; + +sub frequentWord($p, $w) { + # assign the value to a container so we can modify it + my $paragraph = $p; + + # first, remove punctuation from the paragraph + # (but we'll leave apostrophes for contractions) + $paragraph ~~ s:i:g/<-[a..z'\s]>//; + + # count the words in $paragraph + my %count; + for $paragraph.lc.split(' ', :skip-empty) -> $pw { + %count{$pw}++; + } + + # generate the output about the banned word + my $bannedCount = %count{$w} // 0; + my $output = qq/The banned word "$w" occurs $bannedCount /; + $output ~= ($bannedCount == 1) ?? 'time' !! 'times'; + $output ~= ".\n"; + + # delete the banned word from the word count + %count{$w}:delete; + + # now find the most frequent word left and report on that + my @sorted = %count.keys.sort: { + # sort by count + %count{$^b} <=> %count{$^a} + }; + my $freqWord = @sorted[0]; + my $freqCount = %count{$freqWord}; + + $output ~= qq/The other word "$freqWord" /; + $output ~= qq/occurs $freqCount /; + $output ~= ($freqCount == 1) ?? 'time.' !! 'times.'; + + return $freqWord, $output; +} + +sub solution($p, $w) { + say qq/Input: \$p = "$p"/; + say qq/ \$w = "$w"/; + my ($freqWord, $output) = frequentWord($p, $w); + say qq/Output: "$freqWord"/; + say ""; + say $output; +} + +say "Example 1:"; +solution( + "Joe hit a ball, the hit ball flew far after it was hit.", + "hit" +); + +say "\nExample 2:"; +solution( + "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + "the" +); |
