aboutsummaryrefslogtreecommitdiff
path: root/challenge-262/sgreen/python/ch-2.py
blob: a375afe037db8a7a04de6d52ce43b074da292351 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()