diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-24 22:06:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-24 22:06:16 +0000 |
| commit | 70cd7d1c7ac8258aa41caecb622d0c1284d7cf30 (patch) | |
| tree | f6562c7dd52b3c11b758d1caa7120f8ba6aab979 | |
| parent | d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5 (diff) | |
| parent | 73b1c7ec14481d7163a6f882f6450fa526813d2d (diff) | |
| download | perlweeklychallenge-club-70cd7d1c7ac8258aa41caecb622d0c1284d7cf30.tar.gz perlweeklychallenge-club-70cd7d1c7ac8258aa41caecb622d0c1284d7cf30.tar.bz2 perlweeklychallenge-club-70cd7d1c7ac8258aa41caecb622d0c1284d7cf30.zip | |
Merge pull request #9121 from LubosKolouch/master
feat(challenge-244/lubos-kolouch/perl,python,raku,blog): Challenge 244 LK Perl Python Raku blog
| -rw-r--r-- | challenge-244/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/perl/ch-2.pl | 20 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/python/ch-2.py | 23 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-244/lubos-kolouch/raku/ch-2.raku | 10 |
7 files changed, 116 insertions, 0 deletions
diff --git a/challenge-244/lubos-kolouch/blog.txt b/challenge-244/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..822524aa7c --- /dev/null +++ b/challenge-244/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-11-20_Weekly_challenge_244 diff --git a/challenge-244/lubos-kolouch/perl/ch-1.pl b/challenge-244/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c204d75d31 --- /dev/null +++ b/challenge-244/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use Test::More tests => 3; + +sub count_smaller { + my @nums = @_; + my @result; + + for my $i ( 0 .. $#nums ) { + my $count = 0; + for my $j ( 0 .. $#nums ) { + $count++ if $nums[$j] < $nums[$i] && $j != $i; + } + push @result, $count; + } + + return @result; +} + +# Tests +is_deeply( [ count_smaller( 8, 1, 2, 2, 3 ) ], [ 4, 0, 1, 1, 3 ], 'Example 1' ); +is_deeply( [ count_smaller( 6, 5, 4, 8 ) ], [ 2, 1, 0, 3 ], 'Example 2' ); +is_deeply( [ count_smaller( 2, 2, 2 ) ], [ 0, 0, 0 ], 'Example 3' ); diff --git a/challenge-244/lubos-kolouch/perl/ch-2.pl b/challenge-244/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..688f814287 --- /dev/null +++ b/challenge-244/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,20 @@ +use List::Util qw(min max); +use Math::Combinatorics; + +sub group_hero_power { + my @nums = @_; + my $total_power = 0; + + for my $i ( 1 .. scalar @nums ) { + my $combinat = Math::Combinatorics->new( count => $i, data => [@nums] ); + while ( my @combo = $combinat->next_combination ) { + my $power = max(@combo)**2 * min(@combo); + $total_power += $power; + } + } + return $total_power; +} + +# Tests +use Test::More tests => 1; +is( group_hero_power( 2, 1, 4 ), 141, 'Test Case 1' ); diff --git a/challenge-244/lubos-kolouch/python/ch-1.py b/challenge-244/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..50799f5339 --- /dev/null +++ b/challenge-244/lubos-kolouch/python/ch-1.py @@ -0,0 +1,25 @@ +from typing import List + + +def count_smaller(nums: list[int]) -> list[int]: + """ + Count the number of elements smaller than the current element at each index. + + Args: + nums (List[int]): An array of integers. + + Returns: + List[int]: An array containing the counts. + """ + n = len(nums) + result = [] + for i in range(n): + count = sum(1 for j in range(n) if nums[j] < nums[i] and j != i) + result.append(count) + return result + + +# Tests +assert count_smaller([8, 1, 2, 2, 3]) == [4, 0, 1, 1, 3] +assert count_smaller([6, 5, 4, 8]) == [2, 1, 0, 3] +assert count_smaller([2, 2, 2]) == [0, 0, 0] diff --git a/challenge-244/lubos-kolouch/python/ch-2.py b/challenge-244/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2968b5c1d8 --- /dev/null +++ b/challenge-244/lubos-kolouch/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +import unittest +from itertools import combinations +from typing import List + + +def group_hero_power(nums: list[int]) -> int: + total_power = 0 + for i in range(1, len(nums) + 1): + for combo in combinations(nums, i): + total_power += max(combo) ** 2 * min(combo) + return total_power + + +# Tests +class TestGroupHeroPower(unittest.TestCase): + def test_example(self): + self.assertEqual(group_hero_power([2, 1, 4]), 141) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-244/lubos-kolouch/raku/ch-1.raku b/challenge-244/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..596ce41819 --- /dev/null +++ b/challenge-244/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,14 @@ +use Test; + +sub count-smaller(@nums) { + return gather for 0 ..^ @nums.elems -> $i { + take (0 ..^ @nums.elems).grep({ @nums[$_] < @nums[$i] && $_ != $i }).elems; + } +} + +# Tests +is-deeply count-smaller(8, 1, 2, 2, 3), [4, 0, 1, 1, 3], 'Example 1'; +is-deeply count-smaller(6, 5, 4, 8), [2, 1, 0, 3], 'Example 2'; +is-deeply count-smaller(2, 2, 2), [0, 0, 0], 'Example 3'; + +done-testing; diff --git a/challenge-244/lubos-kolouch/raku/ch-2.raku b/challenge-244/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..f7fa000949 --- /dev/null +++ b/challenge-244/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,10 @@ +sub group-hero-power(@nums) { + return [+] @nums.elems.map: -> $i { + @nums.combinations($i).map: { .max**2 * .min }.sum + } +} + +# Tests +use Test; +plan 1; +is group-hero-power(2, 1, 4), 141, 'Test Case 1'; |
