From a8e8a75796d9be05a2fc4ce38db216141b8aea5b Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 25 Dec 2023 18:58:47 -0500 Subject: Challenge 249 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-249/packy-anderson/README.md | 82 ++++++++++++----------------- challenge-249/packy-anderson/blog.txt | 1 + challenge-249/packy-anderson/perl/ch-1.pl | 47 +++++++++++++++++ challenge-249/packy-anderson/perl/ch-2.pl | 39 ++++++++++++++ challenge-249/packy-anderson/python/ch-1.py | 47 +++++++++++++++++ challenge-249/packy-anderson/python/ch-2.py | 36 +++++++++++++ challenge-249/packy-anderson/raku/ch-1.raku | 46 ++++++++++++++++ challenge-249/packy-anderson/raku/ch-2.raku | 39 ++++++++++++++ 8 files changed, 288 insertions(+), 49 deletions(-) create mode 100644 challenge-249/packy-anderson/blog.txt create mode 100755 challenge-249/packy-anderson/perl/ch-1.pl create mode 100755 challenge-249/packy-anderson/perl/ch-2.pl create mode 100755 challenge-249/packy-anderson/python/ch-1.py create mode 100755 challenge-249/packy-anderson/python/ch-2.py create mode 100755 challenge-249/packy-anderson/raku/ch-1.raku create mode 100755 challenge-249/packy-anderson/raku/ch-2.raku diff --git a/challenge-249/packy-anderson/README.md b/challenge-249/packy-anderson/README.md index f08eb862a3..6682f66637 100644 --- a/challenge-249/packy-anderson/README.md +++ b/challenge-249/packy-anderson/README.md @@ -8,12 +8,16 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: $str = "loveleetcode", $char = "e" -Output: (3,2,1,0,1,0,0,1,2,2,1,0) +Input: @ints = (3, 2, 3, 2, 2, 2) +Output: (3, 3), (2, 2), (2, 2) Example 2: -Input: $str = "aaab", $char = "b" -Output: (3,2,1,0) +Input: @ints = (1, 2, 3, 4) +Output: () + +Example 3: +Input: @ints = (1, 2, 3, 4, 4, 3, 2, 1) +Output: (1, 1), (4, 4), (3, 3), (2, 2) ``` * [Task 2](raku/ch-2.raku) @@ -22,28 +26,16 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: $a = [ - [1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12] - ] -Output: $b = [ - [14, 18, 22], - [30, 34, 38] - ] +Input: $str = 'IDID' +Output: (0, 4, 1, 3, 2) Example 2: -Input: $a = [ - [1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1] - ] -Output: $b = [ - [2, 1, 0], - [1, 2, 1], - [0, 1, 2] - ] +Input: $str = 'III' +Output: (0, 1, 2, 3) + +Example 3: +Input: $str = 'DDI' +Output: (3, 2, 0, 1) ``` ## Perl @@ -54,12 +46,16 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: $str = "loveleetcode", $char = "e" -Output: (3,2,1,0,1,0,0,1,2,2,1,0) +Input: @ints = (3, 2, 3, 2, 2, 2) +Output: (2, 2), (2, 2), (3, 3) Example 2: -Input: $str = "aaab", $char = "b" -Output: (3,2,1,0) +Input: @ints = (1, 2, 3, 4) +Output: () + +Example 3: +Input: @ints = (1, 2, 3, 4, 4, 3, 2, 1) +Output: (4, 4), (3, 3), (2, 2), (1, 1) ``` * [Task 2](perl/ch-2.pl) @@ -68,28 +64,16 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: $a = [ - [1, 2, 3, 4], - [5, 6, 7, 8], - [9, 10, 11, 12] - ] -Output: $b = [ - [14, 18, 22], - [30, 34, 38] - ] +Input: $str = 'IDID' +Output: (0, 4, 1, 3, 2) Example 2: -Input: $a = [ - [1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1] - ] -Output: $b = [ - [2, 1, 0], - [1, 2, 1], - [0, 1, 2] - ] +Input: $str = 'III' +Output: (0, 1, 2, 3) + +Example 3: +Input: $str = 'DDI' +Output: (3, 2, 0, 1) ``` ## Guest Language: Python @@ -98,4 +82,4 @@ Output: $b = [ ## Blog Post -[Perl Weekly Challenge: The Shortest Distance between Submatrix Sums](https://packy.dardan.com/b/Ff) +[Perl Weekly Challenge: Stringy DI and Paired Equals](https://packy.dardan.com/b/Fv) diff --git a/challenge-249/packy-anderson/blog.txt b/challenge-249/packy-anderson/blog.txt new file mode 100644 index 0000000000..1adcf62852 --- /dev/null +++ b/challenge-249/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Fv \ No newline at end of file diff --git a/challenge-249/packy-anderson/perl/ch-1.pl b/challenge-249/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..ec399a13e6 --- /dev/null +++ b/challenge-249/packy-anderson/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +use v5.38; + +sub equalPairs(@ints) { + my @pairs; + my %num_count; + # count how many of each int we have + foreach my $num ( @ints ) { + $num_count{$num}++; + } + # first, make sure we have even numbers of each integer + foreach my $k ( keys %num_count ) { + my $v = $num_count{$k}; + next if $v % 2 == 0; # it's even, we can make pairs + return @pairs; # we have an odd number, can't make pairs + } + # now make pairs from those integers + foreach my $k ( keys %num_count ) { + my $count = $num_count{$k}; + while ($count > 0) { + push @pairs, [$k, $k]; + $count -= 2; + } + } + return @pairs; +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my @pairs = equalPairs(@ints); + if (@pairs == 0) { + say 'Output: ()'; + } + else { + @pairs = map { qq{($_->[0], $_->[1])} } @pairs; + say 'Output: ' . join(', ', @pairs); + } +} + +say "Example 1:"; +solution(3, 2, 3, 2, 2, 2); + +say "\nExample 2:"; +solution(1, 2, 3, 4); + +say "\nExample 3:"; +solution(1, 2, 3, 4, 4, 3, 2, 1); \ No newline at end of file diff --git a/challenge-249/packy-anderson/perl/ch-2.pl b/challenge-249/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..e24b164ac9 --- /dev/null +++ b/challenge-249/packy-anderson/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +use v5.38; + +sub diStringMatch($str) { + my @permutation; + # first, generate the list of integers + # we're making permutations of + my @nums = 0 .. length($str); + # now let's generate our permutation + foreach my $c ( split(//, $str) ) { + if ($c eq 'D') { + # take the largest number available + push @permutation, pop(@nums); + } + else { + # take the smallest number available + push @permutation, shift(@nums); + } + } + # add last remaining number + push @permutation, $nums[0]; + + return @permutation; +} + +sub solution($str) { + say qq{Input: \$str = '$str'}; + my @permutation = diStringMatch($str); + say 'Output: (' . join(', ', @permutation) . ')'; +} + +say "Example 1:"; +solution("IDID"); + +say "\nExample 2:"; +solution("III"); + +say "\nExample 3:"; +solution("DDI"); \ No newline at end of file diff --git a/challenge-249/packy-anderson/python/ch-1.py b/challenge-249/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..228bb47da3 --- /dev/null +++ b/challenge-249/packy-anderson/python/ch-1.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +from collections import Counter + +def equalPairs(nums): + pairs = [] + num_count = Counter() + # count how many of each int we have + for num in nums: + num_count[num] += 1 + + # first, make sure we have even numbers of each integer + for k, v in dict(num_count).items(): + if v % 2 == 0: # it's even, we can make pairs + continue + else: + return pairs # we have an odd number, no pairs + + # now make pairs from those integers + for k, v in dict(num_count).items(): + count = v # the values k, v are read-only + while count > 0: + pairs.append( [k, k] ) + count -= 2 + + return pairs + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(nums): + print(f'Input: @ints = ({comma_join(nums)})') + pairs = equalPairs(nums) + if len(pairs) == 0: + print('Output: ()') + else: + pairs = [ f'({x[0]}, {x[1]})' for x in pairs ] + print(f"Output: { ', '.join(pairs) }") + +print('Example 1:') +solution([3, 2, 3, 2, 2, 2]) + +print('\nExample 2:') +solution([1, 2, 3, 4]) + +print('\nExample 3:') +solution([1, 2, 3, 4, 4, 3, 2, 1]) \ No newline at end of file diff --git a/challenge-249/packy-anderson/python/ch-2.py b/challenge-249/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..8c3750f195 --- /dev/null +++ b/challenge-249/packy-anderson/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +def diStringMatch(str): + permutation = [] + # first, generate the list of integers + # we're making permutations of + nums = list(range(len(str)+1)) + # now let's generate our permutation + for c in str: + if c == 'D': + # take the largest number available + permutation.append( nums.pop(-1) ) + else: + # take the smallest number available + permutation.append( nums.pop(0) ) + # add last remaining number + permutation.append( nums[0] ) + + return permutation + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(str): + print(f'Input: $str = ({comma_join(str)})') + permutations = diStringMatch(str) + print(f'Output: ({comma_join(permutations)})') + +print('Example 1:') +solution("IDID") + +print('\nExample 2:') +solution("III") + +print('\nExample 3:') +solution("DDI") \ No newline at end of file diff --git a/challenge-249/packy-anderson/raku/ch-1.raku b/challenge-249/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..6daec9c767 --- /dev/null +++ b/challenge-249/packy-anderson/raku/ch-1.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env raku +use v6; + +sub equalPairs(@ints) { + my @pairs; + my %num_count; + # count how many of each int we have + for @ints -> $num { + %num_count{$num}++; + } + # first, make sure we have even numbers of each integer + for %num_count.kv -> $k, $v { + next if $v % 2 == 0; # it's even, we can make pairs + return @pairs; # we have an odd number, can't make pairs + } + # now make pairs from those integers + for %num_count.kv -> $k, $v { + my $count = $v; # the values $k, $v are read-only + while ($count > 0) { + @pairs.push( [$k, $k] ); + $count -= 2; + } + } + return @pairs; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @pairs = equalPairs(@ints); + if (@pairs == 0) { + say 'Output: ()'; + } + else { + @pairs = @pairs.map({ qq{($_[0], $_[1])} }); + say 'Output: ' ~ @pairs.join(', '); + } +} + +say "Example 1:"; +solution([3, 2, 3, 2, 2, 2]); + +say "\nExample 2:"; +solution([1, 2, 3, 4]); + +say "\nExample 3:"; +solution([1, 2, 3, 4, 4, 3, 2, 1]); diff --git a/challenge-249/packy-anderson/raku/ch-2.raku b/challenge-249/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..44b853efd2 --- /dev/null +++ b/challenge-249/packy-anderson/raku/ch-2.raku @@ -0,0 +1,39 @@ +#!/usr/bin/env raku +use v6; + +sub diStringMatch($str) { + my @permutation; + # first, generate the list of integers + # we're making permutations of + my @nums = 0 .. $str.chars; + # now let's generate our permutation + for $str.split('', :skip-empty) -> $c { + if ($c eq 'D') { + # take the largest number available + @permutation.push( @nums.pop() ); + } + else { + # take the smallest number available + @permutation.push( @nums.shift() ); + } + } + # add last remaining number + @permutation.push( @nums[0] ); + + return @permutation; +} + +sub solution($str) { + say qq{Input: \$str = '$str'}; + my @permutation = diStringMatch($str); + say 'Output: (' ~ @permutation.join(', ') ~ ')'; +} + +say "Example 1:"; +solution("IDID"); + +say "\nExample 2:"; +solution("III"); + +say "\nExample 3:"; +solution("DDI"); \ No newline at end of file -- cgit