diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-20 19:18:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-20 19:18:20 +0000 |
| commit | 18824bd635a6e006123c1b8ba3b2ed1eaaf6fc96 (patch) | |
| tree | 540e9a0f654a555a6aa268d4390556084d3cbb7c | |
| parent | 78a1fd4752bcec4d417fa7ae8c205703c9767b2a (diff) | |
| parent | 497b0b770f58c3087ebb83f08150588e7c785f31 (diff) | |
| download | perlweeklychallenge-club-18824bd635a6e006123c1b8ba3b2ed1eaaf6fc96.tar.gz perlweeklychallenge-club-18824bd635a6e006123c1b8ba3b2ed1eaaf6fc96.tar.bz2 perlweeklychallenge-club-18824bd635a6e006123c1b8ba3b2ed1eaaf6fc96.zip | |
Merge pull request #9779 from oWnOIzRi/week261
add solutions week 261 in python
| -rw-r--r-- | challenge-261/steven-wilson/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-261/steven-wilson/python/ch-2.py | 32 |
2 files changed, 58 insertions, 0 deletions
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() |
