aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-245/lubos-kolouch/blog.txt1
-rw-r--r--challenge-245/lubos-kolouch/perl/ch-1.pl20
-rw-r--r--challenge-245/lubos-kolouch/perl/ch-2.pl40
-rw-r--r--challenge-245/lubos-kolouch/python/ch-1.py27
-rw-r--r--challenge-245/lubos-kolouch/python/ch-2.py37
-rw-r--r--challenge-245/lubos-kolouch/raku/ch-1.raku13
-rw-r--r--challenge-245/lubos-kolouch/raku/ch-2.raku39
7 files changed, 177 insertions, 0 deletions
diff --git a/challenge-245/lubos-kolouch/blog.txt b/challenge-245/lubos-kolouch/blog.txt
new file mode 100644
index 0000000000..f568daf36f
--- /dev/null
+++ b/challenge-245/lubos-kolouch/blog.txt
@@ -0,0 +1 @@
+https://egroup.kolouch.org/nextcloud/sites/lubos/2023-11-27_Weekly_challenge_245
diff --git a/challenge-245/lubos-kolouch/perl/ch-1.pl b/challenge-245/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..ed491f5651
--- /dev/null
+++ b/challenge-245/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+sub sort_language {
+ my ( $langs, $popularity ) = @_;
+ return [
+ map { $_->[0] }
+ sort { $a->[1] <=> $b->[1] }
+ map { [ $langs->[$_], $popularity->[$_] ] } 0 .. $#$langs
+ ];
+}
+
+# Test Cases
+is_deeply( sort_language( [ 'perl', 'c', 'python' ], [ 2, 1, 3 ] ), [ 'c', 'perl', 'python' ], 'Example 1' );
+is_deeply(
+ sort_language( [ 'c++', 'haskell', 'java' ], [ 1, 3, 2 ] ),
+ [ 'c++', 'java', 'haskell' ],
+ 'Example 2'
+);
diff --git a/challenge-245/lubos-kolouch/perl/ch-2.pl b/challenge-245/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..899d617bc7
--- /dev/null
+++ b/challenge-245/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,40 @@
+use strict;
+use warnings;
+use List::Util qw(sum);
+use Test::More tests => 3;
+
+sub largest_multiple_of_three {
+ my @ints = @_;
+
+ # Sort in descending order
+ @ints = sort { $b cmp $a } @ints;
+
+ # Function to remove the smallest element with a specific remainder
+ sub remove_smallest {
+ my ( $ints_ref, $remainder ) = @_;
+ for ( my $i = $#$ints_ref ; $i >= 0 ; $i-- ) {
+ if ( $ints_ref->[$i] % 3 == $remainder ) {
+ splice( @$ints_ref, $i, 1 );
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+ # Adjust the list to make the sum of its digits divisible by 3
+ my $sum = sum(@ints);
+ if ( $sum % 3 == 1 ) {
+ remove_smallest( \@ints, 1 ) || ( remove_smallest( \@ints, 2 ) && remove_smallest( \@ints, 2 ) );
+ }
+ elsif ( $sum % 3 == 2 ) {
+ remove_smallest( \@ints, 2 ) || ( remove_smallest( \@ints, 1 ) && remove_smallest( \@ints, 1 ) );
+ }
+
+ $sum = sum(@ints);
+ return @ints && $sum % 3 == 0 ? join( '', @ints ) : -1;
+}
+
+# Test Cases
+is( largest_multiple_of_three( 8, 1, 9 ), 981, 'Example 1' );
+is( largest_multiple_of_three( 8, 6, 7, 1, 0 ), 8760, 'Example 2' );
+is( largest_multiple_of_three(1), -1, 'Example 3' );
diff --git a/challenge-245/lubos-kolouch/python/ch-1.py b/challenge-245/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d960e4b918
--- /dev/null
+++ b/challenge-245/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import unittest
+from typing import List, Tuple
+
+
+def sort_language(langs: list[str], popularity: list[int]) -> list[str]:
+ combined = sorted(zip(langs, popularity), key=lambda x: x[1])
+ return [lang for lang, _ in combined]
+
+
+# Test Cases
+class TestSortLanguage(unittest.TestCase):
+ def test_example_1(self):
+ self.assertEqual(
+ sort_language(["perl", "c", "python"], [2, 1, 3]), ["c", "perl", "python"]
+ )
+
+ def test_example_2(self):
+ self.assertEqual(
+ sort_language(["c++", "haskell", "java"], [1, 3, 2]),
+ ["c++", "java", "haskell"],
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-245/lubos-kolouch/python/ch-2.py b/challenge-245/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..f31c9a44e8
--- /dev/null
+++ b/challenge-245/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+
+def largest_multiple_of_three(ints):
+ # Sort numbers in descending order
+ ints.sort(reverse=True)
+
+ # Function to calculate the sum of digits
+ sum_digits = sum(ints)
+
+ # Function to remove the smallest element with a specified remainder
+ def remove_smallest(remainder):
+ for i in range(len(ints) - 1, -1, -1):
+ if ints[i] % 3 == remainder:
+ del ints[i]
+ return True
+ return False
+
+ # Adjust the list to make the sum of its digits divisible by 3
+ if sum_digits % 3 == 1:
+ # Try to remove one number with a remainder of 1, else remove two with a remainder of 2
+ if not remove_smallest(1):
+ remove_smallest(2) and remove_smallest(2)
+ elif sum_digits % 3 == 2:
+ # Try to remove one number with a remainder of 2, else remove two with a remainder of 1
+ if not remove_smallest(2):
+ remove_smallest(1) and remove_smallest(1)
+
+ return int("".join(map(str, ints))) if ints and sum(ints) % 3 == 0 else -1
+
+
+# Test Cases
+test1 = largest_multiple_of_three([8, 1, 9]) == 981
+test2 = largest_multiple_of_three([8, 6, 7, 1, 0]) == 8760
+test3 = largest_multiple_of_three([1]) == -1
+
+test1, test2, test3
diff --git a/challenge-245/lubos-kolouch/raku/ch-1.raku b/challenge-245/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..6f3fefb279
--- /dev/null
+++ b/challenge-245/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,13 @@
+use Test;
+
+sub sort-language(@langs, @popularity) {
+ my @paired = @langs Z @popularity;
+ my @sorted = @paired.sort({$^a[1] <=> $^b[1]});
+ return @sorted.map(*[0]);
+}
+
+# Test Cases
+is-deeply sort-language(<perl c python>, <2 1 3>), <c perl python>, 'Example 1';
+is-deeply sort-language(<c++ haskell java>, <1 3 2>), <c++ java haskell>, 'Example 2';
+
+done-testing;
diff --git a/challenge-245/lubos-kolouch/raku/ch-2.raku b/challenge-245/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..277c462feb
--- /dev/null
+++ b/challenge-245/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,39 @@
+use Test;
+
+sub largest-multiple-of-three(@ints) {
+ # Sort in descending order
+ @ints = @ints.sort({$^b cmp $^a});
+ my $sum = [+] @ints;
+
+ # Function to remove the smallest element with a specific remainder
+ sub remove-smallest(Int $remainder) {
+ for @ints.keys.reverse -> $i {
+ if @ints[$i] % 3 == $remainder {
+ @ints.splice($i, 1);
+ return True;
+ }
+ }
+ return False;
+ }
+
+ # Adjust the list to make the sum of its digits divisible by 3
+ if $sum % 3 == 1 {
+ unless remove-smallest(1) {
+ remove-smallest(2) && remove-smallest(2);
+ }
+ } elsif $sum % 3 == 2 {
+ unless remove-smallest(2) {
+ remove-smallest(1) && remove-smallest(1);
+ }
+ }
+
+ $sum = [+] @ints;
+ return @ints && $sum % 3 == 0 ?? @ints.join('').Int !! -1;
+}
+
+# Test Cases
+is largest-multiple-of-three([8, 1, 9]), 981, 'Example 1';
+is largest-multiple-of-three([8, 6, 7, 1, 0]), 8760, 'Example 2';
+is largest-multiple-of-three([1]), -1, 'Example 3';
+
+done-testing;