diff options
| author | Steven <steven1170@zoho.eu> | 2024-03-25 15:52:29 +0000 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-03-25 15:52:29 +0000 |
| commit | 2c582022904f466301895a369bb6b5d9679adabc (patch) | |
| tree | 69b4e5996fd3f407727f4dc2f2d92d163d2e4913 /challenge-262/steven-wilson/python/ch-2.py | |
| parent | 041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff) | |
| download | perlweeklychallenge-club-2c582022904f466301895a369bb6b5d9679adabc.tar.gz perlweeklychallenge-club-2c582022904f466301895a369bb6b5d9679adabc.tar.bz2 perlweeklychallenge-club-2c582022904f466301895a369bb6b5d9679adabc.zip | |
add solutions week 262 in python
Diffstat (limited to 'challenge-262/steven-wilson/python/ch-2.py')
| -rwxr-xr-x | challenge-262/steven-wilson/python/ch-2.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-262/steven-wilson/python/ch-2.py b/challenge-262/steven-wilson/python/ch-2.py new file mode 100755 index 0000000000..91a31c941c --- /dev/null +++ b/challenge-262/steven-wilson/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + + +def count_equal_divisible(*elements, divisor): + ''' + Given an array of integers, @ints and an integer $k. Return the number of + pairs (i, j) where: + a) 0 <= i < j < size of @ints + b) ints[i] == ints[j] + c) i x j is divisible by k + >>> count_equal_divisible(3,1,2,2,2,1,3, divisor = 2) + 4 + >>> count_equal_divisible(1,2,3, divisor = 1) + 0 + ''' + if not isinstance(divisor, int): + raise TypeError("Divisor must be an integer") + + if not all(isinstance(elem, int) for elem in elements): + raise TypeError("All elements must be integers") + + count = 0 + length_elements = len(elements) + + for i, val_i in enumerate(elements): + for j in range(i + 1, length_elements): + val_j = elements[j] + if (val_i == val_j) and ((i * j) % divisor == 0): + count += 1 + + return count + + +if __name__ == "__main__": + import doctest + + doctest.testmod() |
