aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-26 11:48:02 +0000
committerGitHub <noreply@github.com>2024-03-26 11:48:02 +0000
commitdd2eb04706b382a42d4496c32bba16a99cdba5ef (patch)
tree94cdbda550bfdf99f9e7d9d6304fcda5ec04a798
parent670c5ed14cb7d2cdcea4bf2778da6d07e508c473 (diff)
parent2c582022904f466301895a369bb6b5d9679adabc (diff)
downloadperlweeklychallenge-club-dd2eb04706b382a42d4496c32bba16a99cdba5ef.tar.gz
perlweeklychallenge-club-dd2eb04706b382a42d4496c32bba16a99cdba5ef.tar.bz2
perlweeklychallenge-club-dd2eb04706b382a42d4496c32bba16a99cdba5ef.zip
Merge pull request #9817 from oWnOIzRi/week262
add solutions week 262 in python
-rwxr-xr-xchallenge-262/steven-wilson/python/ch-1.py27
-rwxr-xr-xchallenge-262/steven-wilson/python/ch-2.py37
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()