aboutsummaryrefslogtreecommitdiff
path: root/challenge-262/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-262/sgreen/python')
-rwxr-xr-xchallenge-262/sgreen/python/ch-1.py34
-rwxr-xr-xchallenge-262/sgreen/python/ch-2.py36
-rwxr-xr-xchallenge-262/sgreen/python/test.py20
3 files changed, 90 insertions, 0 deletions
diff --git a/challenge-262/sgreen/python/ch-1.py b/challenge-262/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..c1f34a8db7
--- /dev/null
+++ b/challenge-262/sgreen/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def max_type(ints: list) -> int:
+ """Count the maximum number of positive or negative integers
+
+ Args:
+ ints (list): The list
+
+ Returns:
+ int: The maximum number
+ """
+
+ neg_count = pos_count = 0
+ for i in ints:
+ if i < 0:
+ neg_count += 1
+ elif i > 0:
+ pos_count += 1
+
+ return max(neg_count, pos_count)
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = max_type(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-262/sgreen/python/ch-2.py b/challenge-262/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..a375afe037
--- /dev/null
+++ b/challenge-262/sgreen/python/ch-2.py
@@ -0,0 +1,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()
diff --git a/challenge-262/sgreen/python/test.py b/challenge-262/sgreen/python/test.py
new file mode 100755
index 0000000000..5a380011b6
--- /dev/null
+++ b/challenge-262/sgreen/python/test.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.max_type([-3, 1, 2, -1, 3, -2, 4]), 4)
+ self.assertEqual(ch_1.max_type([-1, -2, -3, 1]), 3)
+ self.assertEqual(ch_1.max_type([1,2]), 2)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.equal_div([3, 1, 2, 2, 2, 1, 3], 2), 4)
+ self.assertEqual(ch_2.equal_div([1, 2, 3], 1), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()