diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-27 11:22:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-27 11:22:15 +0000 |
| commit | 178b8e792f6879f96b87e234b9f272fed120a87f (patch) | |
| tree | 5eb7be7840a57818ccb310a98053b913d1a4e435 | |
| parent | 039b81056a5a049d7e4cb012f970cfc0fc467faa (diff) | |
| parent | 0aa78a55c295d4a4eeaeda3b2f780961ba98e30b (diff) | |
| download | perlweeklychallenge-club-178b8e792f6879f96b87e234b9f272fed120a87f.tar.gz perlweeklychallenge-club-178b8e792f6879f96b87e234b9f272fed120a87f.tar.bz2 perlweeklychallenge-club-178b8e792f6879f96b87e234b9f272fed120a87f.zip | |
Merge pull request #9825 from packy/master
Challenge 262 solutions by Packy Anderson
| -rw-r--r-- | challenge-262/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-262/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/perl/ch-2.pl | 34 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/raku/ch-1.raku | 32 | ||||
| -rwxr-xr-x | challenge-262/packy-anderson/raku/ch-2.raku | 35 |
8 files changed, 202 insertions, 1 deletions
diff --git a/challenge-262/packy-anderson/README.md b/challenge-262/packy-anderson/README.md index 72d867b319..b5c4eea6fb 100644 --- a/challenge-262/packy-anderson/README.md +++ b/challenge-262/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Two Elements, Multiplied by Digit Sum](https://packy.dardan.com/b/Jj) +[Counting to the Max!](https://packy.dardan.com/b/Jx) diff --git a/challenge-262/packy-anderson/blog.txt b/challenge-262/packy-anderson/blog.txt new file mode 100644 index 0000000000..d18478ea7c --- /dev/null +++ b/challenge-262/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Jx
\ No newline at end of file diff --git a/challenge-262/packy-anderson/perl/ch-1.pl b/challenge-262/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..276c334d0e --- /dev/null +++ b/challenge-262/packy-anderson/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( max sum ); + +sub maxPosNeg(@ints) { + my $pos = sum map { $_ > 0 ? 1 : 0 } @ints; + my $neg = sum map { $_ < 0 ? 1 : 0 } @ints; + my $max = max $pos, $neg; + return ( + $max, + join("\n", + "Count of positive integers: $pos", + "Count of negative integers: $neg", + "Maximum of count of positive and " . + "negative integers: $max" + ) + ); +} + +sub solution(@ints) { + say 'Input: @arr = (' . join(', ', @ints) . ')'; + my ($max, $explain) = maxPosNeg(@ints); + say "Output: $max\n\n$explain"; +} + +say "Example 1:"; +solution(-3, 1, 2, -1, 3, -2, 4); + +say "\nExample 2:"; +solution(-1, -2, -3, 1); + +say "\nExample 3:"; +solution(1,2);
\ No newline at end of file diff --git a/challenge-262/packy-anderson/perl/ch-2.pl b/challenge-262/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..b861131782 --- /dev/null +++ b/challenge-262/packy-anderson/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.38; + +sub countEquDiv($k, @ints) { + my @explain; + my $cnt = 0; + foreach my $i ( 0 .. $#ints - 1 ) { + foreach my $j ( $i + 1 .. $#ints ) { + # does ints[i] == ints[j]? + next unless $ints[$i] == $ints[$j]; + # is i x j divisible by k? + next unless ( ($i * $j) % $k ) == 0; + # count the pair and explain why + $cnt++; + push @explain, + "($i, $j) => ints[$i] == ints[$j] " . + "and $i x $j is divisible by $k"; + } + } + return($cnt, join("\n", @explain)); +} + +sub solution($k, @ints) { + say 'Input: @arr = (' . join(', ', @ints) + . ') and $k = ' . $k; + my ($cnt, $explain) = countEquDiv($k, @ints); + say "Output: $cnt\n\n$explain"; +} + +say "Example 1:"; +solution(2, 3,1,2,2,2,1,3); + +say "\nExample 2:"; +solution(1, 1,2,3);
\ No newline at end of file diff --git a/challenge-262/packy-anderson/python/ch-1.py b/challenge-262/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..f695636a37 --- /dev/null +++ b/challenge-262/packy-anderson/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +def maxPosNeg(ints): + pos = sum([1 for i in ints if i > 0]) + neg = sum([1 for i in ints if i < 0]) + maxCount = max(pos, neg) + return ( + maxCount, + "\n".join([ + f"Count of positive integers: {pos}", + f"Count of negative integers: {neg}", + f"Maximum of count of positive and " + + f"negative integers: {maxCount}" + ]) + ) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @arr = ({comma_join(ints)})') + (maxCount, explain) = maxPosNeg(ints) + print(f'Output: {maxCount}\n\n{explain}') + +print('Example 1:') +solution([-3, 1, 2, -1, 3, -2, 4]) + +print('\nExample 2:') +solution([-1, -2, -3, 1]) + +print('\nExample 3:') +solution([1,2])
\ No newline at end of file diff --git a/challenge-262/packy-anderson/python/ch-2.py b/challenge-262/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e485257ef9 --- /dev/null +++ b/challenge-262/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def countEquDiv(k, ints): + explain = [] + cnt = 0 + for i in range(len(ints) - 1): + for j in range(i+1, len(ints)): + # does ints[i] == ints[j]? + if not ints[i] == ints[j]: break + # is i x j is divisible by k? + if not ( (i * j) % k ) == 0: break + # count the pair and explain why + cnt += 1 + explain.append( + f"({i}, {j}) => ints[{i}] == ints[{j}] " + + f"and {i} x {j} is divisible by {k}" + ) + return(cnt, "\n".join(explain)) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(k, ints): + print(f'Input: @arr = ({comma_join(ints)}) '+ + f'and $k = {k}') + (cnt, explain) = countEquDiv(k, ints) + print(f'Output: {cnt}\n\n{explain}') + +print('Example 1:') +solution(2, [3,1,2,2,2,1,3]) + +print('\nExample 2:') +solution(1, [1,2,3])
\ No newline at end of file diff --git a/challenge-262/packy-anderson/raku/ch-1.raku b/challenge-262/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..ba3c0076cd --- /dev/null +++ b/challenge-262/packy-anderson/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use v6; + +sub maxPosNeg(@ints) { + my $pos = [+] @ints.map({ $_ > 0 ?? 1 !! 0 }); + my $neg = [+] @ints.map({ $_ < 0 ?? 1 !! 0 }); + my $max = max $pos, $neg; + return ( + $max, + ( + "Count of positive integers: $pos", + "Count of negative integers: $neg", + "Maximum of count of positive and " ~ + "negative integers: $max" + ).join("\n") + ); +} + +sub solution(@ints) { + say 'Input: @arr = (' ~ @ints.join(', ') ~ ')'; + my ($max, $explain) = maxPosNeg(@ints); + say "Output: $max\n\n$explain"; +} + +say "Example 1:"; +solution([-3, 1, 2, -1, 3, -2, 4]); + +say "\nExample 2:"; +solution([-1, -2, -3, 1]); + +say "\nExample 3:"; +solution([1,2]);
\ No newline at end of file diff --git a/challenge-262/packy-anderson/raku/ch-2.raku b/challenge-262/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..d9632102ae --- /dev/null +++ b/challenge-262/packy-anderson/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use v6; + +sub countEquDiv($k, @ints) { + my @explain; + my $cnt = 0; + for 0 .. @ints.end - 1 -> $i { + for $i + 1 .. @ints.end -> $j { + # does ints[i] == ints[j]? + next unless @ints[$i] == @ints[$j]; + # is i x j divisible by k? + next unless ( ($i * $j) mod $k ) == 0; + # count the pair and explain why + $cnt++; + @explain.push( + "($i, $j) => ints[$i] == ints[$j] " ~ + "and $i x $j is divisible by $k" + ); + } + } + return($cnt, @explain.join("\n")); +} + +sub solution($k, @ints) { + say 'Input: @arr = (' ~ @ints.join(', ') + ~ ') and $k = ' ~ $k; + my ($cnt, $explain) = countEquDiv($k, @ints); + say "Output: $cnt\n\n$explain"; +} + +say "Example 1:"; +solution(2, [3,1,2,2,2,1,3]); + +say "\nExample 2:"; +solution(1, [1,2,3]); |
