diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-12-29 13:05:13 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-12-29 13:05:13 +0100 |
| commit | e629c979504d589477d7bb70feea7b42049f15f3 (patch) | |
| tree | be1b764a80e2f70472a455577fa0c1be2d18e65d | |
| parent | 80dd2ab239aecd016700c06fffaf45a8d1250ec4 (diff) | |
| download | perlweeklychallenge-club-e629c979504d589477d7bb70feea7b42049f15f3.tar.gz perlweeklychallenge-club-e629c979504d589477d7bb70feea7b42049f15f3.tar.bz2 perlweeklychallenge-club-e629c979504d589477d7bb70feea7b42049f15f3.zip | |
feat(challenge-249/lubos-kolouch/perl,python,raku,blog): Challenge 249 LK Perl Python Raku blog
| -rw-r--r-- | challenge-249/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/python/ch-1.py | 38 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/python/ch-2.py | 34 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/raku/ch-1.raku | 23 | ||||
| -rw-r--r-- | challenge-249/lubos-kolouch/raku/ch-2.raku | 21 |
7 files changed, 186 insertions, 0 deletions
diff --git a/challenge-249/lubos-kolouch/blog.txt b/challenge-249/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..8dcbaf2e9f --- /dev/null +++ b/challenge-249/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-12-25_Weekly_challenge_249 diff --git a/challenge-249/lubos-kolouch/perl/ch-1.pl b/challenge-249/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..26010b0066 --- /dev/null +++ b/challenge-249/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,42 @@ +use strict; +use warnings; +use Test::More; + +sub equal_pairs { + my @ints = @_; + my %freq; + my @order; + + # Count the frequency of each element and track the order + foreach my $int (@ints) { + if ( !exists $freq{$int} ) { + push @order, $int; + } + $freq{$int}++; + } + + # Check if all elements have even frequency + foreach my $key ( keys %freq ) { + return [] if $freq{$key} % 2; # Returning an array reference + } + + # Form pairs + my @pairs; + foreach my $key (@order) { + push @pairs, ( [ $key, $key ] ) x ( $freq{$key} / 2 ); + } + + return \@pairs; +} + +# Test cases +subtest 'Test Cases' => sub { + is_deeply( + equal_pairs( 3, 2, 3, 2, 2, 2 ), + [ [ 3, 3 ], [ 2, 2 ], [ 2, 2 ] ], + 'Example 1' + ); + is_deeply( equal_pairs( 1, 2, 3, 4 ), [], 'Example 2' ); # Revised test case +}; + +done_testing(); diff --git a/challenge-249/lubos-kolouch/perl/ch-2.pl b/challenge-249/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9be18e7f07 --- /dev/null +++ b/challenge-249/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use Test::More tests => 3; + +sub di_string_match { + my ($str) = @_; + my $n = length($str); + my ( $low, $high ) = ( 0, $n ); + my @perm = (); + + foreach my $char ( split //, $str ) { + if ( $char eq 'I' ) { + push @perm, $low++; + } + else { + push @perm, $high--; + } + } + + push @perm, $low; # $low == $high at this point + return @perm; +} + +# Tests +is_deeply( [ di_string_match("IDID") ], [ 0, 4, 1, 3, 2 ], 'Test IDID' ); +is_deeply( [ di_string_match("III") ], [ 0, 1, 2, 3 ], 'Test III' ); +is_deeply( [ di_string_match("DDI") ], [ 3, 2, 0, 1 ], 'Test DDI' ); diff --git a/challenge-249/lubos-kolouch/python/ch-1.py b/challenge-249/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..286bd201e4 --- /dev/null +++ b/challenge-249/lubos-kolouch/python/ch-1.py @@ -0,0 +1,38 @@ +import unittest +from collections import Counter + + +def equal_pairs(ints): + freq = Counter(ints) + seen = set() + ordered_elements = [] + + # Keep the order of first occurrence + for num in ints: + if num not in seen: + seen.add(num) + ordered_elements.append(num) + + # Check if all elements have even frequency + if any(count % 2 for count in freq.values()): + return [] + + # Form pairs in the order of first occurrence + pairs = [] + for num in ordered_elements: + pairs.extend([[num, num] for _ in range(freq[num] // 2)]) + + return pairs + + +# Unit tests +class TestEqualPairs(unittest.TestCase): + def test_example_1(self): + self.assertEqual(equal_pairs([3, 2, 3, 2, 2, 2]), [[3, 3], [2, 2], [2, 2]]) + + def test_example_2(self): + self.assertEqual(equal_pairs([1, 2, 3, 4]), []) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-249/lubos-kolouch/python/ch-2.py b/challenge-249/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..723490c7bb --- /dev/null +++ b/challenge-249/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import unittest +from typing import List + + +def di_string_match(s: str) -> list[int]: + low, high = 0, len(s) + perm = [] + + for char in s: + if char == "I": + perm.append(low) + low += 1 + else: + perm.append(high) + high -= 1 + + perm.append(low) # low == high at this point + return perm + + +# Tests + + +class TestDIStringMatch(unittest.TestCase): + def test_cases(self): + self.assertEqual(di_string_match("IDID"), [0, 4, 1, 3, 2]) + self.assertEqual(di_string_match("III"), [0, 1, 2, 3]) + self.assertEqual(di_string_match("DDI"), [3, 2, 0, 1]) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-249/lubos-kolouch/raku/ch-1.raku b/challenge-249/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..d59a8937e2 --- /dev/null +++ b/challenge-249/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,23 @@ +use Test; + +sub equal-pairs(@ints) { + my %freq = @ints.Bag; + + # Check if all elements have even frequency + return [] if %freq.values.any(* % 2); + + # Form pairs + my @pairs; + for %freq.kv -> $num, $count { + @pairs.append([$num, $num]) for 1..($count div 2); + } + + return @pairs; +} + +# Tests +plan 2; +is-deeply equal-pairs(3, 2, 3, 2, 2, 2), [[2, 2], [2, 2], [3, 3]], 'Example 1'; +is-deeply equal-pairs(1, 2, 3, 4), [], 'Example 2'; + +done-testing; diff --git a/challenge-249/lubos-kolouch/raku/ch-2.raku b/challenge-249/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..8c06d0eb48 --- /dev/null +++ b/challenge-249/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,21 @@ +use Test; + +sub di-string-match(Str $str) { + my $n = $str.chars; + my ($low, $high) = 0, $n; + my @perm; + + for $str.comb -> $char { + @perm.push($char eq 'I' ?? $low++ !! $high--); + } + + @perm.push($low); # $low == $high at this point + return @perm; +} + +# Tests +is-deeply di-string-match("IDID"), [0, 4, 1, 3, 2], 'Test IDID'; +is-deeply di-string-match("III"), [0, 1, 2, 3], 'Test III'; +is-deeply di-string-match("DDI"), [3, 2, 0, 1], 'Test DDI'; + +done-testing; |
