diff options
| -rwxr-xr-x | challenge-262/steven-wilson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-262/steven-wilson/python/ch-2.py | 37 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-262/steven-wilson/python/ch-1.py b/challenge-262/steven-wilson/python/ch-1.py new file mode 100755 index 0000000000..0f3576c196 --- /dev/null +++ b/challenge-262/steven-wilson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + + +def max_positive_negative(*elements): + ''' + Given an array of integers, return the maximum number of either positive or + negative integers in the given array. + >>> max_positive_negative(-3, 1, 2, -1, 3, -2, 4) + 4 + >>> max_positive_negative(-1, -2, -3, 1) + 3 + >>> max_positive_negative(1,2) + 2 + ''' + if not all(isinstance(elem, int) for elem in elements): + raise TypeError("Arguments must be integers") + + positive = sum(1 for elem in elements if elem >= 0) + negative = len(elements) - positive + + return max(positive, negative) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() 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() |
