diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2024-03-29 12:29:58 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2024-03-29 12:29:58 +0100 |
| commit | 92a112547b3a615f87e42435788978eade7ff656 (patch) | |
| tree | 9a65fb659a614f1b39a02b4659a4d16e7777fc64 | |
| parent | 5fab68e28d1e9534146b0a350be001a452fe381a (diff) | |
| download | perlweeklychallenge-club-92a112547b3a615f87e42435788978eade7ff656.tar.gz perlweeklychallenge-club-92a112547b3a615f87e42435788978eade7ff656.tar.bz2 perlweeklychallenge-club-92a112547b3a615f87e42435788978eade7ff656.zip | |
Challenge 262 LK Perl Python
| -rw-r--r-- | challenge-262/lubos-kolouch/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-262/lubos-kolouch/perl/ch-2.pl | 28 | ||||
| -rw-r--r-- | challenge-262/lubos-kolouch/python/ch-1.py | 13 | ||||
| -rw-r--r-- | challenge-262/lubos-kolouch/python/ch-2.py | 16 |
4 files changed, 77 insertions, 0 deletions
diff --git a/challenge-262/lubos-kolouch/perl/ch-1.pl b/challenge-262/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ab2df48556 --- /dev/null +++ b/challenge-262/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; + + +package Ch1; +use Carp; + +sub max_positive_negative { + my @ints = @_; + my $positive_count = scalar(grep { $_ > 0 } @ints); + my $negative_count = scalar(grep { $_ < 0 } @ints); + return $positive_count > $negative_count ? $positive_count : $negative_count; +} + +# Assert tests +croak "Test failed!" unless max_positive_negative(-3, 1, 2, -1, 3, -2, 4) == 4; +croak "Test failed!" unless max_positive_negative(-1, -2, -3, 1) == 3; +croak "Test failed!" unless max_positive_negative(1, 2) == 2; + +1;
\ No newline at end of file diff --git a/challenge-262/lubos-kolouch/perl/ch-2.pl b/challenge-262/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c2eb3354ee --- /dev/null +++ b/challenge-262/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + + +package Ch2; +use Carp; + +sub count_equal_divisible { + my ($ints_ref, $k) = @_; + my @ints = @$ints_ref; + my $count = 0; + my $n = scalar @ints; + + for my $i (0 .. $n - 1) { + for my $j ($i + 1 .. $n - 1) { + if ($ints[$i] == $ints[$j] && ($i * $j) % $k == 0) { + $count++; + } + } + } + return $count; +} + +# Assert tests +croak "Test failed!" unless count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4; +croak "Test failed!" unless count_equal_divisible([1, 2, 3], 1) == 0; + +1;
\ No newline at end of file diff --git a/challenge-262/lubos-kolouch/python/ch-1.py b/challenge-262/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f87605552f --- /dev/null +++ b/challenge-262/lubos-kolouch/python/ch-1.py @@ -0,0 +1,13 @@ +from typing import List + + +def max_positive_negative(ints: List[int]) -> int: + positive_count = sum(1 for x in ints if x > 0) + negative_count = sum(1 for x in ints if x < 0) + return max(positive_count, negative_count) + + +# Assert tests +assert max_positive_negative([-3, 1, 2, -1, 3, -2, 4]) == 4 +assert max_positive_negative([-1, -2, -3, 1]) == 3 +assert max_positive_negative([1, 2]) == 2 diff --git a/challenge-262/lubos-kolouch/python/ch-2.py b/challenge-262/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5d42adf2a0 --- /dev/null +++ b/challenge-262/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +from typing import List + + +def count_equal_divisible(ints: List[int], k: int) -> int: + count = 0 + n = len(ints) + for i in range(n): + for j in range(i + 1, n): + if ints[i] == ints[j] and i * j % k == 0: + count += 1 + return count + + +# Assert tests +assert count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4 +assert count_equal_divisible([1, 2, 3], 1) == 0 |
