diff options
| author | Simon Green <mail@simon.green> | 2024-03-31 22:45:04 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-03-31 22:45:04 +1100 |
| commit | 4c42510de23a775a8cd514185d2d4cf319f9f10a (patch) | |
| tree | cd172339a0bffcb798a4e44b24ab46f427b35104 /challenge-262/sgreen/python/ch-2.py | |
| parent | 041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff) | |
| download | perlweeklychallenge-club-4c42510de23a775a8cd514185d2d4cf319f9f10a.tar.gz perlweeklychallenge-club-4c42510de23a775a8cd514185d2d4cf319f9f10a.tar.bz2 perlweeklychallenge-club-4c42510de23a775a8cd514185d2d4cf319f9f10a.zip | |
sgreen solutions to challenge 262
Diffstat (limited to 'challenge-262/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-262/sgreen/python/ch-2.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-262/sgreen/python/ch-2.py b/challenge-262/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a375afe037 --- /dev/null +++ b/challenge-262/sgreen/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys + + +def equal_div(ints: list, k: int) -> int: + """Count the number of occurrences where two values are equal and the + product of their position is divisible by k + + Args: + ints (list): The input list + k (int): An integer + + Returns: + int: The number of occurrences + """ + + count = sum(1 + for i in range(len(ints)-1) + for j in range(i+1, len(ints)) + if ints[i] == ints[j] and i * j % k == 0 + ) + + return count + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = equal_div(array, k) + print(result) + + +if __name__ == '__main__': + main() |
