diff options
Diffstat (limited to 'challenge-261/packy-anderson/python/ch-2.py')
| -rwxr-xr-x | challenge-261/packy-anderson/python/ch-2.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-261/packy-anderson/python/ch-2.py b/challenge-261/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..fbbf4f535e --- /dev/null +++ b/challenge-261/packy-anderson/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +def multiplyByTwo(ints, start): + intSet = frozenset(ints) + explain = [] + step = 0 + + while start in intSet: + step += 1 + old = start + start *= 2 + explain.append( + f"Step {step}: {old} is in the array " + + f"so {old} x 2 = {start}" + ) + explain.append( + f"{start} is not in the array so return {start}." + ) + return (start, "\n".join(explain)) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints, start): + print(f'Input: @ints = ({comma_join(ints)})', end=" ") + print(f'and $start = {start}') + (output, explain) = multiplyByTwo(ints, start) + print(f'Output: {output}\n\n{explain}') + +print('Example 1:') +solution([5,3,6,1,12], 3) + +print('\nExample 2:') +solution([1,2,4,3], 1) + +print('\nExample 3:') +solution([5,6,7], 2)
\ No newline at end of file |
