From 3486e1463d03a356101506406c8fc9e2e99bcbe9 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 2 Dec 2023 12:25:27 +0100 Subject: feat(challenge-245/lubos-kolouch/perl,python,raku,blog): Challenge 245 LK Perl Python Raku blog --- challenge-245/lubos-kolouch/python/ch-1.py | 27 ++++++++++++++++++++++ challenge-245/lubos-kolouch/python/ch-2.py | 37 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-245/lubos-kolouch/python/ch-1.py create mode 100644 challenge-245/lubos-kolouch/python/ch-2.py (limited to 'challenge-245/lubos-kolouch/python') 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 -- cgit