diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-12-16 12:28:29 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-12-16 12:28:29 +0100 |
| commit | cab15bdfa58d33a4e128a89a0f8499790887016e (patch) | |
| tree | 51f04defb978e5a12f1dd6107c189dbbc8a358a9 | |
| parent | 04ecddac57686ccd063c71f7efb0520e09f98cbd (diff) | |
| download | perlweeklychallenge-club-cab15bdfa58d33a4e128a89a0f8499790887016e.tar.gz perlweeklychallenge-club-cab15bdfa58d33a4e128a89a0f8499790887016e.tar.bz2 perlweeklychallenge-club-cab15bdfa58d33a4e128a89a0f8499790887016e.zip | |
feat(challenge-247/lubos-kolouch/perl,python,raku,blog/): Challenge 247 LK Perl Python Raku blog
| -rw-r--r-- | challenge-247/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/perl/ch-2.pl | 19 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/python/ch-2.py | 20 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/raku/ch-1.raku | 19 | ||||
| -rw-r--r-- | challenge-247/lubos-kolouch/raku/ch-2.raku | 16 |
7 files changed, 136 insertions, 0 deletions
diff --git a/challenge-247/lubos-kolouch/blog.txt b/challenge-247/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..d897873225 --- /dev/null +++ b/challenge-247/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-12-11_Weekly_challenge_247 diff --git a/challenge-247/lubos-kolouch/perl/ch-1.pl b/challenge-247/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..635066adfc --- /dev/null +++ b/challenge-247/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +use List::Util qw(shuffle); + +sub secret_santa { + my (@names) = @_; + my %family_map; + my %assignments; + + # Extract family names and map them + foreach my $name (@names) { + my ($family_name) = $name =~ /(\w+)$/; + push @{ $family_map{$family_name} }, $name; + } + + while (1) { + my @shuffled = shuffle(@names); + %assignments = (); + for my $i ( 0 .. $#names ) { + my $giver = $names[$i]; + my $receiver = $shuffled[$i]; + $assignments{$giver} = $receiver; + last if ( $giver =~ /(\w+)$/ )[0] eq ( $receiver =~ /(\w+)$/ )[0]; + } + return \%assignments if keys(%assignments) == @names; + } +} + +# Test and Output +my @names = ( 'Mr. Wall', 'Mrs. Wall', 'Mr. Anwar', 'Mrs. Anwar', 'Mr. Conway', 'Mr. Cross' ); +my $assigned = secret_santa(@names); + +while ( my ( $giver, $receiver ) = each %$assigned ) { + print "$giver -> $receiver\n"; +} diff --git a/challenge-247/lubos-kolouch/perl/ch-2.pl b/challenge-247/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..02290af097 --- /dev/null +++ b/challenge-247/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use Test::More tests => 2; + +sub most_frequent_letter_pair { + my ($s) = @_; + my %pairs; + + # Counting frequencies of each pair + $pairs{ substr( $s, $_, 2 ) }++ for 0 .. length($s) - 2; + + # Finding the most frequent pair + my @sorted_pairs = sort { $pairs{$b} <=> $pairs{$a} || $a cmp $b } keys %pairs; + return $sorted_pairs[0]; +} + +# Tests +is( most_frequent_letter_pair('abcdbca'), 'bc', 'Example 1' ); +is( most_frequent_letter_pair('cdeabeabfcdfabgcd'), 'ab', 'Example 2' ); diff --git a/challenge-247/lubos-kolouch/python/ch-1.py b/challenge-247/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f547b2beac --- /dev/null +++ b/challenge-247/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import random +from typing import Dict, List + + +def secret_santa(names: list[str]) -> dict[str, str]: + def extract_family_name(name: str) -> str: + return name.split()[-1] + + while True: + shuffled = random.sample(names, len(names)) + assignments = {giver: receiver for giver, receiver in zip(names, shuffled)} + if all( + extract_family_name(giver) != extract_family_name(receiver) + for giver, receiver in assignments.items() + ): + return assignments + + +# Output +names = ["Mr. Wall", "Mrs. Wall", "Mr. Anwar", "Mrs. Anwar", "Mr. Conway", "Mr. Cross"] +assigned = secret_santa(names) + +for giver, receiver in assigned.items(): + print(f"{giver} -> {receiver}") diff --git a/challenge-247/lubos-kolouch/python/ch-2.py b/challenge-247/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..967445743b --- /dev/null +++ b/challenge-247/lubos-kolouch/python/ch-2.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + + +def most_frequent_letter_pair(s: str) -> str: + from collections import Counter + + # Counting frequencies of each pair + pairs = Counter(s[i : i + 2] for i in range(len(s) - 1)) + + # Finding the most frequent pair + most_frequent = max( + pairs.items(), key=lambda x: (x[1], -ord(x[0][0]), -ord(x[0][1])) + ) + + return most_frequent[0] + + +# Tests +assert most_frequent_letter_pair("abcdbca") == "bc" +assert most_frequent_letter_pair("cdeabeabfcdfabgcd") == "ab" diff --git a/challenge-247/lubos-kolouch/raku/ch-1.raku b/challenge-247/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..6167b1a186 --- /dev/null +++ b/challenge-247/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,19 @@ +sub secret-santa(@names) { + my %assignments; + + loop { + my @shuffled = @names.pick(*); + %assignments = @names Z=> @shuffled; + last if all(%assignments.map({ .key.split(' ').tail ne .value.split(' ').tail })); + } + + return %assignments; +} + +# Output +my @names = ('Mr. Wall', 'Mrs. Wall', 'Mr. Anwar', 'Mrs. Anwar', 'Mr. Conway', 'Mr. Cross'); +my %assigned = secret-santa(@names); + +for %assigned.kv -> $giver, $receiver { + say "$giver -> $receiver"; +} diff --git a/challenge-247/lubos-kolouch/raku/ch-2.raku b/challenge-247/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..fd8af58f66 --- /dev/null +++ b/challenge-247/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,16 @@ +use Test; + +sub most-frequent-letter-pair(Str $s) returns Str { + my %pairs; + + # Counting frequencies of each pair + %pairs{.substr($_, 2)}++ for 0..$s.chars - 2; + + # Finding the most frequent pair + my @sorted-pairs = %pairs.keys.sort({%pairs{$^b} <=> %pairs{$^a} || $^a cmp $^b}); + return @sorted-pairs[0]; +} + +# Tests +is most-frequent-letter-pair('abcdbca'), 'bc', 'Example 1'; +is most-frequent-letter-pair('cdeabeabfcdfabgcd'), 'ab', 'Example 2'; |
