diff options
Diffstat (limited to 'challenge-262/packy-anderson/python/ch-2.py')
| -rwxr-xr-x | challenge-262/packy-anderson/python/ch-2.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-262/packy-anderson/python/ch-2.py b/challenge-262/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e485257ef9 --- /dev/null +++ b/challenge-262/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def countEquDiv(k, ints): + explain = [] + cnt = 0 + for i in range(len(ints) - 1): + for j in range(i+1, len(ints)): + # does ints[i] == ints[j]? + if not ints[i] == ints[j]: break + # is i x j is divisible by k? + if not ( (i * j) % k ) == 0: break + # count the pair and explain why + cnt += 1 + explain.append( + f"({i}, {j}) => ints[{i}] == ints[{j}] " + + f"and {i} x {j} is divisible by {k}" + ) + return(cnt, "\n".join(explain)) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(k, ints): + print(f'Input: @arr = ({comma_join(ints)}) '+ + f'and $k = {k}') + (cnt, explain) = countEquDiv(k, ints) + print(f'Output: {cnt}\n\n{explain}') + +print('Example 1:') +solution(2, [3,1,2,2,2,1,3]) + +print('\nExample 2:') +solution(1, [1,2,3])
\ No newline at end of file |
