diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-11 01:48:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-11 01:48:05 +0100 |
| commit | 56ebf950a135997674f31e7cba625dad284edc04 (patch) | |
| tree | 73d6150a3b105ddfbd86add254a4d3dec598bf78 /challenge-233 | |
| parent | 2833200e5df296ca7f95f7f4738b3f804b719111 (diff) | |
| parent | bf9c0e828d2b2988ebf3a896b45ece047780b1e0 (diff) | |
| download | perlweeklychallenge-club-56ebf950a135997674f31e7cba625dad284edc04.tar.gz perlweeklychallenge-club-56ebf950a135997674f31e7cba625dad284edc04.tar.bz2 perlweeklychallenge-club-56ebf950a135997674f31e7cba625dad284edc04.zip | |
Merge pull request #8665 from packy/master
Challenge 233 solutions by Packy Anderson
Diffstat (limited to 'challenge-233')
| -rw-r--r-- | challenge-233/packy-anderson/README.md | 104 | ||||
| -rw-r--r-- | challenge-233/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/perl/ch-1.pl | 77 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/perl/ch-2.pl | 65 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/python/ch-1.py | 56 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/python/ch-2.py | 67 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/raku/ch-1.raku | 69 | ||||
| -rwxr-xr-x | challenge-233/packy-anderson/raku/ch-2.raku | 62 |
8 files changed, 460 insertions, 41 deletions
diff --git a/challenge-233/packy-anderson/README.md b/challenge-233/packy-anderson/README.md index 1fd2b48e17..01386371ca 100644 --- a/challenge-233/packy-anderson/README.md +++ b/challenge-233/packy-anderson/README.md @@ -8,18 +8,23 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @ints = (3, 2, 1, 4) -Output: (3, 2) -The minimum is 1 and maximum is 4 in the given array. So (3, 2) are neither min nor max. +Input: @words = ("aba", "aabb", "abcd", "bac", "aabc") +Output: 2 + +Pair 1: similar words ("bac", "aabc") +Pair 2: similar words ("aba", "aabb") Example 2: -Input: @ints = (3, 1) -Output: -1 +Input: @words = ("aabb", "ab", "ba") +Output: 3 + +Pair 1: similar words ("aabb", "ab") +Pair 2: similar words ("aabb", "ba") +Pair 3: similar words ("ab", "ba") Example 3: -Input: @ints = (2, 1, 3) -Output: (2) -The minimum is 1 and maximum is 3 in the given array. So (2) is neither min nor max. +Input: @words = ("nba", "cba", "dba") +Output: 0 ``` * [Task 2](perl/ch-2.pl) @@ -28,25 +33,29 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: @list = ("7868190130M7522", "5303914400F9211", "9273338290F4010") -Output: 2 +Input: @ints = (1, 1, 2, 2, 2, 3) +Output: (3, 1, 1, 2, 2, 2) + +'3' has a frequency of 1 +'1' has a frequency of 2 +'2' has a frequency of 3 -The ages of the passengers in the given list are 75, 92, and 40. -So we have 2 senior citizens. Example 2: -Input: @list = ("1313579440F2036", "2921522980M5644") -Output: 0 +Input: @ints = (2, 3, 1, 3, 2) +Output: (1, 3, 3, 2, 2) + +'1' has a frequency of 1 +'2' and '3' both have a frequency of 2, so they are sorted in decreasing order -The ages of the passengers in the given list are 20 and 56. -So we have no senior citizens. Example 3: -Input: @list = ("5188675309F6002") -Output: 1 +Input: @ints = (-1, 1, -6, 4, 5, -6, 1, 4, 1) +Output: (5, -1, 4, 4, -6, -6, 1, 1, 1) -The age of the passenger in the given list is 60. -So we have 1 senior citizen. +'-1' and '5' both have a frequency of 1, so they are sorted in decreasing order +'-6' and '4' both have a frequency of 2, so they are sorted in decreasing order +'1' has a frequency of 3 ``` ## Raku @@ -57,18 +66,23 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @ints = (3, 2, 1, 4) -Output: (3, 2) -The minimum is 1 and maximum is 4 in the given array. So (3, 2) are neither min nor max. +Input: @words = ("aba", "aabb", "abcd", "bac", "aabc") +Output: 2 + +Pair 1: similar words ("bac", "aabc") +Pair 2: similar words ("aba", "aabb") Example 2: -Input: @ints = (3, 1) -Output: -1 +Input: @words = ("aabb", "ab", "ba") +Output: 3 + +Pair 1: similar words ("aabb", "ab") +Pair 2: similar words ("aabb", "ba") +Pair 3: similar words ("ab", "ba") Example 3: -Input: @ints = (2, 1, 3) -Output: (2) -The minimum is 1 and maximum is 3 in the given array. So (2) is neither min nor max. +Input: @words = ("nba", "cba", "dba") +Output: 0 ``` * [Task 2](raku/ch-2.raku) @@ -77,27 +91,35 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: @list = ("7868190130M7522", "5303914400F9211", "9273338290F4010") -Output: 2 +Input: @ints = (1, 1, 2, 2, 2, 3) +Output: (3, 1 1, 2 2 2) + +'3' has a frequency of 1 +'1' has a frequency of 2 +'2' has a frequency of 3 -The ages of the passengers in the given list are 75, 92, and 40. -So we have 2 senior citizens. Example 2: -Input: @list = ("1313579440F2036", "2921522980M5644") -Output: 0 +Input: @ints = (2, 3, 1, 3, 2) +Output: (1, 3 3, 2 2) + +'1' has a frequency of 1 +'2' and '3' both have a frequency of 2, so they are sorted in decreasing order -The ages of the passengers in the given list are 20 and 56. -So we have no senior citizens. Example 3: -Input: @list = ("5188675309F6002") -Output: 1 +Input: @ints = (-1, 1, -6, 4, 5, -6, 1, 4, 1) +Output: (5, -1, 4 4, -6 -6, 1 1 1) -The age of the passenger in the given list is 60. -So we have 1 senior citizen. +'-1' and '5' both have a frequency of 1, so they are sorted in decreasing order +'-6' and '4' both have a frequency of 2, so they are sorted in decreasing order +'1' has a frequency of 3 ``` +## Guest Language: Python +* [Task 1](python/ch-1.py) +* [Task 2](python/ch-1.py) + ## Blog Post -[Perl Weekly Challenge: Min/Maxing your Senior Citizens](https://packy.dardan.com/2023/08/23/perl-weekly-challenge-min-maxing-your-senior-citizens/)
\ No newline at end of file +[Perl Weekly Challenge: What's the Frequency, Kenneth?](https://packy.dardan.com/2023/09/05/perl-weekly-challenge-whats-the-frequency-kenneth/)
\ No newline at end of file diff --git a/challenge-233/packy-anderson/blog.txt b/challenge-233/packy-anderson/blog.txt new file mode 100644 index 0000000000..4dd1a8b9eb --- /dev/null +++ b/challenge-233/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/09/05/perl-weekly-challenge-whats-the-frequency-kenneth/
\ No newline at end of file diff --git a/challenge-233/packy-anderson/perl/ch-1.pl b/challenge-233/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..dc95ab1e74 --- /dev/null +++ b/challenge-233/packy-anderson/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl + +use v5.38; + +sub charsInWord { + my $word = shift; + # split the word into characters, then map those + # characters to a hash so we only have unique ones + my %charset = map { $_ => 1 } split //, $word; + # return the set of characters as a string, sorted + return join q{}, sort keys %charset; +} + +sub findSimilarWordPairs { + my @words = @_; + + # get the set of characters in each word, + # store each word in an array reference under + # the hash key for its character set + my %similar; + foreach my $word ( @words ) { + my $charset = charsInWord($word); + # if $similar{$charset} is undefined when we + # try to use it as an array reference to store + # a value, Perl will "autovivify" a reference + # to an empty array + push @{ $similar{$charset} }, $word; + } + + # filter out character sets that only have one word + my @multiples = grep { + scalar( @{ $similar{$_} } ) > 1 + } keys %similar; + + # make pairs by looping over the list + # of letter sets that had multiple entries + my @pairs; + foreach my $charset ( @multiples ) { + my @list = @{ $similar{$charset} }; + + while ( scalar(@list) >= 2 ) { + # remove the first word from the list of words + my $first = shift @list; + # pair it with each of the remaining words + foreach my $second ( @list ) { + push @pairs, [ $first, $second ]; + } + } + } + return @pairs; +} + +sub solution { + my @words = @_; + say 'Input: @words = ("' . join('", "', @words) . '")'; + + my @pairs = findSimilarWordPairs(@words); + + say 'Output: ' . scalar(@pairs); + my $count = 0; + foreach my $pair ( @pairs ) { + say "" if $count == 0; + say 'Pair ' . ++$count . ': similar words ("' + . join('", "', @$pair) . '")'; + } +} + +say "Example 1:"; +solution("aba", "aabb", "abcd", "bac", "aabc"); + +say ""; +say "Example 2:"; +solution("aabb", "ab", "ba"); + +say ""; +say "Example 3:"; +solution("nba", "cba", "dba");
\ No newline at end of file diff --git a/challenge-233/packy-anderson/perl/ch-2.pl b/challenge-233/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..be71fe2c36 --- /dev/null +++ b/challenge-233/packy-anderson/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +use v5.38; + +use Lingua::EN::Inflexion qw( wordlist ); + +sub solution { + my @ints = @_; + say 'Input: @ints = (' . join(', ', @ints) . ')'; + + # count how often each integer occurs + my %counts; + foreach my $int ( @ints ) { + $counts{$int}++; + } + + # now create a hash of arrays listing which integers + # occur at what frequencies + my %frequency; + foreach my $int ( keys %counts ) { + push @{ $frequency{ $counts{$int} } }, $int; + } + + my @output; + my $text; + foreach my $freq ( sort keys %frequency ) { + my @list = @{ $frequency{$freq} }; + # get each integer for this frequency in descending order + foreach my $int ( reverse sort @list ) { + # we need to put the integer on the list $freq times + foreach ( 1 .. $freq ) { + push @output, $int; + } + } + # now let's do the English description of the output. + # have the integers in ascending order in the text, + # and wrap them in quotes + @list = map { "'$_'" } sort @list; + if (@list == 1) { + $text .= $list[0] . " has a frequency of $freq\n"; + } + else { + $text .= wordlist(@list); + if (@list == 2) { + $text .= ' both'; + } + $text .= " have a frequency of $freq, " + . "so they are sorted in decreasing order\n"; + } + } + + say "Output: (" . join(', ', @output) . ")"; + say "\n$text"; +} + +say "Example 1:"; +solution(1,1,2,2,2,3); + +say ""; +say "Example 2:"; +solution(2,3,1,3,2); + +say ""; +say "Example 3:"; +solution(-1,1,-6,4,5,-6,1,4,1);
\ No newline at end of file diff --git a/challenge-233/packy-anderson/python/ch-1.py b/challenge-233/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..62d7422964 --- /dev/null +++ b/challenge-233/packy-anderson/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +def charsInWord(word): + # split the word into characters, use set() to get + # the unique values and then rejoin as a string + return ''.join(set(list(word))) + +def findSimilarWordPairs(words): + similar = {} + for word in words: + charset = charsInWord(word) + if charset not in similar: + similar[charset] = [] + similar[charset].append(word) + + # filter out character sets that only have one word + multiples = filter( + lambda k: len(similar[k]) > 1, + similar.keys() + ) + + # make pairs by looping over the list + # of letter sets that had multiple entries + pairs = [] + for charset in multiples: + while ( len(similar[charset]) >= 2 ): + # remove the first word from the list of words + first = similar[charset].pop(0) + # pair it with each of the remaining words + for second in similar[charset]: + pairs.append([ first, second ]) + + return pairs + +def solution(words): + quoted = '"' + '", "'.join(words) + '"' + print(f'Input: @words = ({quoted})') + pairs = findSimilarWordPairs(words) + + print(f'Output: {len(pairs)}') + count = 0 + for pair in pairs: + if count == 0: + print('') + count += 1 + quoted = '"' + '", "'.join(pair) + '"' + print(f'Pair {count}: similar words ({quoted})') + +print("Example 1:") +solution(["aba", "aabb", "abcd", "bac", "aabc"]) + +print("\nExample 2:") +solution(["aabb", "ab", "ba"]) + +print("\nExample 3:") +solution(["nba", "cba", "dba"])
\ No newline at end of file diff --git a/challenge-233/packy-anderson/python/ch-2.py b/challenge-233/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..1bc2fe3cc7 --- /dev/null +++ b/challenge-233/packy-anderson/python/ch-2.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +def conjunction(words): + if len(words) < 2: + return(words) + elif len(words) == 2: + return(f'{words[0]} and {words[1]}') + else: + last = words.pop(-1) + l = ', '.join(words) + return(f'{l}, and {last}') + +def solution(ints): + as_list = ', '.join(map(lambda i: str(i), ints)) + print(f'Input: @ints = ({as_list})') + + # count how often each integer occurs + counts = {} + for i in ints: + if i not in counts: + counts[i] = 0 + counts[i] += 1 + + # now create a hash of arrays listing which integers + # occur at what frequencies + frequency = {} + for i in counts.keys(): + if counts[i] not in frequency: + frequency[ counts[i] ] = [] + frequency[ counts[i] ].append(i) + + output = [] + text = '' + for freq in sorted(frequency): + # get each integer for this frequency in descending order + for i in sorted(frequency[freq], reverse=True): + # we need to put the integer on the list $freq times + for x in range(freq): + output.append(i) + + # now let's do the English description of the output. + # have the integers in ascending order in the text, + # and wrap them in quotes + l = list(map(lambda i: f"'{str(i)}'", sorted(frequency[freq]))) + if len(l) == 1: + text += l[0] + f" has a frequency of {freq}\n" + else: + text += conjunction(l) + if len(l) == 2: + text += ' both' + text += ( + f" have a frequency of {freq}, " + + "so they are sorted in decreasing order\n" + ) + as_list = ', '.join(map(lambda i: str(i), output)) + print(f'Output: ({as_list})') + print(f'\n{text}') + + +print('Example 1:') +solution([1,1,2,2,2,3]); + +print('\nExample 1:') +solution([2,3,1,3,2]) + +print('\nExample 3:') +solution([-1,1,-6,4,5,-6,1,4,1])
\ No newline at end of file diff --git a/challenge-233/packy-anderson/raku/ch-1.raku b/challenge-233/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..5016039fa3 --- /dev/null +++ b/challenge-233/packy-anderson/raku/ch-1.raku @@ -0,0 +1,69 @@ +#!/usr/bin/env raku + +use v6; + +sub charsInWord(Str $word) { + # split the word into characters, then use the Raku + # array method unique to have each character appear once. + return $word.split('').unique.sort.join; +} + +sub findSimilarWordPairs(*@words where ($_.all ~~ Str)) { + my %similar; + for @words -> $word { + my $charset = charsInWord($word); + %similar{$charset}.push($word); + } + + # filter out character sets that only have one word + my @multiples = %similar.keys.grep: { + %similar{$_}.elems > 1 + }; + + # make pairs by looping over the list + # of letter sets that had multiple entries + my @pairs; + for @multiples -> $charset { + # if we assign @list = %similar{$charset}, we get + # an array with a single element, an array object. + # By using .splice, I can get all the elements in + # the array object assigned to @list + my @list = %similar{$charset}.splice(0, *); + + while ( @list.elems >= 2 ) { + # remove the first word from the list of words + my $first = @list.shift; + # pair it with each of the remaining words + for @list -> $second { + @pairs.push([ $first, $second ]); + } + } + } + return @pairs; +} + +sub solution { + my @words = @_; + say 'Input: @words = ("' ~ @words.join('", "') ~ '")'; + + my @pairs = findSimilarWordPairs(@words); + + say 'Output: ' ~ @pairs.elems; + my $count = 0; + for @pairs -> $pair { + say "" if $count == 0; + say 'Pair ' ~ ++$count ~ ': similar words ("' + ~ $pair.join('", "') ~ '")'; + } +} + +say "Example 1:"; +solution("aba", "aabb", "abcd", "bac", "aabc"); + +say ""; +say "Example 2:"; +solution("aabb", "ab", "ba"); + +say ""; +say "Example 3:"; +solution("nba", "cba", "dba");
\ No newline at end of file diff --git a/challenge-233/packy-anderson/raku/ch-2.raku b/challenge-233/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..1eb2453b75 --- /dev/null +++ b/challenge-233/packy-anderson/raku/ch-2.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku + +use v6; + +use Lingua::Conjunction; + +sub solution (*@ints where {$_.all ~~ Int}) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + + # count how often each integer occurs + my %counts; + for @ints -> $int { + %counts{$int}++; + } + + # now create a hash of arrays listing which integers + # occur at what frequencies + my %frequency; + for %counts.keys -> $int { + %frequency{ %counts{$int} }.push($int); + } + + my @output; + my $text; + for %frequency.keys.sort -> $freq { + my @list = %frequency{$freq}.splice(0, *); + # get each integer for this frequency in descending order + for @list.sort.reverse -> $int { + # we need to put the integer on the list $freq times + @output.append($int xx $freq); + } + # now let's do the English description of the output. + # have the integers in ascending order in the text, + # and wrap them in quotes + @list = @list.sort.map: { "'$_'" }; + if (@list.elems == 1) { + $text ~= @list[0] ~ " has a frequency of $freq\n"; + } + else { + $text ~= conjunction @list; + if (@list.elems == 2) { + $text ~= ' both'; + } + $text ~= " have a frequency of $freq, " + ~ "so they are sorted in decreasing order\n"; + } + } + + say "Output: (" ~ @output.join(', ') ~ ")"; + say "\n$text"; +} + +say "Example 1:"; +solution(1,1,2,2,2,3); + +say ""; +say "Example 2:"; +solution(2,3,1,3,2); + +say ""; +say "Example 3:"; +solution(-1,1,-6,4,5,-6,1,4,1);
\ No newline at end of file |
