diff options
| author | Simon Green <mail@simon.green> | 2024-03-24 17:33:45 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-03-24 17:33:45 +1100 |
| commit | 91f2331d26cbc320ce8712336d691010fa1d345e (patch) | |
| tree | 8332bd5869ad7f97cfc671e674bf7d373cfef32b /challenge-261/sgreen/python | |
| parent | cad939ed2335566e31b26c5b74b9fc99c23db7a5 (diff) | |
| download | perlweeklychallenge-club-91f2331d26cbc320ce8712336d691010fa1d345e.tar.gz perlweeklychallenge-club-91f2331d26cbc320ce8712336d691010fa1d345e.tar.bz2 perlweeklychallenge-club-91f2331d26cbc320ce8712336d691010fa1d345e.zip | |
Simon's solution to challenge 261
Diffstat (limited to 'challenge-261/sgreen/python')
| -rwxr-xr-x | challenge-261/sgreen/python/ch-1.py | 30 | ||||
| -rwxr-xr-x | challenge-261/sgreen/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-261/sgreen/python/test.py | 22 |
3 files changed, 85 insertions, 0 deletions
diff --git a/challenge-261/sgreen/python/ch-1.py b/challenge-261/sgreen/python/ch-1.py new file mode 100755 index 0000000000..b95c0c3c16 --- /dev/null +++ b/challenge-261/sgreen/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys + + +def element_digit_sum(ints: list) -> int: + """Calculate the absolute difference between the element sum and the sum + of each digit + + Args: + ints (list): List of integers + + Returns: + int: The absolute difference + """ + + element_sum = sum(ints) + digit_sum = sum(int(i) for s in map(str, ints) for i in s) + return abs(element_sum - digit_sum) + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = element_digit_sum(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-261/sgreen/python/ch-2.py b/challenge-261/sgreen/python/ch-2.py new file mode 100755 index 0000000000..90a73a42a9 --- /dev/null +++ b/challenge-261/sgreen/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import sys + + +def multiple_by_two(ints: list, start: int) -> int: + """Multiple the start number by 2 if it is in the list + + Args: + ints (list): List of integers + start (int): The starting number + + Returns: + int: The first value that isn't in the string + """ + + solution = start + while solution in ints: + solution *= 2 + + return solution + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + start = array.pop() + result = multiple_by_two(array, start) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-261/sgreen/python/test.py b/challenge-261/sgreen/python/test.py new file mode 100755 index 0000000000..984f8848e1 --- /dev/null +++ b/challenge-261/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.element_digit_sum([1, 2, 3, 45]), 36) + self.assertEqual(ch_1.element_digit_sum([1, 12, 3]), 9) + self.assertEqual(ch_1.element_digit_sum([1, 2, 3, 4]), 0) + self.assertEqual(ch_1.element_digit_sum([236, 416, 336, 350]), 1296) + + def test_ch_2(self): + self.assertEqual(ch_2.multiple_by_two([5, 3, 6, 1, 12], 3), 24) + self.assertEqual(ch_2.multiple_by_two([1, 2, 4, 3], 1), 8) + self.assertEqual(ch_2.multiple_by_two([5, 6, 7], 2), 2) + + +if __name__ == '__main__': + unittest.main() |
