From 497b0b770f58c3087ebb83f08150588e7c785f31 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 19 Mar 2024 15:58:42 +0000 Subject: add solutions week 261 in python --- challenge-261/steven-wilson/python/ch-1.py | 26 ++++++++++++++++++++++++ challenge-261/steven-wilson/python/ch-2.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 challenge-261/steven-wilson/python/ch-1.py create mode 100644 challenge-261/steven-wilson/python/ch-2.py diff --git a/challenge-261/steven-wilson/python/ch-1.py b/challenge-261/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..9682017e67 --- /dev/null +++ b/challenge-261/steven-wilson/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + + +def element_digit_sum(*elements): + ''' Given an array of integers, evaluate the absolute difference between + element and digit sum of the given array. + >>> element_digit_sum(1,2,3,45) + 36 + >>> element_digit_sum(1,12,3) + 9 + >>> element_digit_sum(1,2,3,4) + 0 + >>> element_digit_sum(236, 416, 336, 350) + 1296 + ''' + return abs(sum_digits(elements) - sum(elements)) + + +def sum_digits(elements): + return sum(int(char) for elem in elements for char in str(elem)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-261/steven-wilson/python/ch-2.py b/challenge-261/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..3f63e0b83d --- /dev/null +++ b/challenge-261/steven-wilson/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + + +def multiply_by_two(*elements, start): + ''' Given an array of integers and a start integer + + a) Look for start in the array, if found multiply the number by 2 + b) If not found stop the process otherwise repeat + + In the end return the final value. + + >>> multiply_by_two(5,3,6,1,12, start=3) + 24 + >>> multiply_by_two(1,2,4,3, start=1) + 8 + >>> multiply_by_two(5,6,7, start=2) + 2 + ''' + if not all(isinstance(elem, int) for elem in elements) or not isinstance(start, int): + raise ValueError('Input must consist of integers') + + elements_set = set(elements) + while start in elements_set: + start *= 2 + + return start + + +if __name__ == "__main__": + import doctest + + doctest.testmod() -- cgit