aboutsummaryrefslogtreecommitdiff
path: root/challenge-188/mohammad-anwar/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-188/mohammad-anwar/python/ch-1.py')
-rw-r--r--challenge-188/mohammad-anwar/python/ch-1.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-188/mohammad-anwar/python/ch-1.py b/challenge-188/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..5f05a65a6e
--- /dev/null
+++ b/challenge-188/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/python3
+
+'''
+
+Week 188:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-188
+
+Task #1: Divisible Pairs
+
+ You are given list of integers @list of size $n and divisor $k.
+
+ Write a script to find out count of pairs in the given list that
+ satisfies the following rules.
+
+ The pair (i, j) is eligible if and only if
+ a) 0 <= i < j < len(list)
+ b) list[i] + list[j] is divisible by k
+
+'''
+
+import unittest
+
+def countDivisiblePairs(array, k) -> int:
+ count = 0
+ size = len(array)
+ for i in range(size):
+ for j in range(i + 1, size):
+ if (array[i] + array[j]) % k == 0:
+ count = count + 1
+
+ return count
+
+#
+#
+# Unit test class
+
+class TestDivisiblePairs(unittest.TestCase):
+ def test_divisiblePairs(self):
+ self.assertEqual(countDivisiblePairs([4, 5, 1, 6], 2), 2, 'Example 1')
+ self.assertEqual(countDivisiblePairs([1, 2, 3, 4], 2), 2, 'Example 2')
+ self.assertEqual(countDivisiblePairs([1, 3, 4, 5], 3), 2, 'Example 3')
+ self.assertEqual(countDivisiblePairs([5, 1, 2, 3], 4), 2, 'Example 4')
+ self.assertEqual(countDivisiblePairs([7, 2, 4, 5], 4), 1, 'Example 5')
+
+unittest.main()