aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-320/sgreen/python')
-rwxr-xr-xchallenge-320/sgreen/python/ch-1.py31
-rwxr-xr-xchallenge-320/sgreen/python/ch-2.py39
-rwxr-xr-xchallenge-320/sgreen/python/test.py21
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-320/sgreen/python/ch-1.py b/challenge-320/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..60156de33e
--- /dev/null
+++ b/challenge-320/sgreen/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def maximum_count(ints: list) -> int:
+ """Return the maximum between the number of positive and negative integers
+
+ Args:
+ ints (list): A list of integers
+
+ Returns:
+ int: The maximum
+ """
+
+ # Count the number of positive and negative integers
+ pos_count = sum(1 for i in ints if i > 0)
+ neg_count = sum(1 for i in ints if i < 0)
+
+ # Return the maximum of these two values
+ return max(pos_count, neg_count)
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = maximum_count(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-320/sgreen/python/ch-2.py b/challenge-320/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..cda60d3d8d
--- /dev/null
+++ b/challenge-320/sgreen/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def sum_difference(ints: list) -> int:
+ """Calculate the absolute difference between digit sum and element
+ sum of the given list.
+
+ Args:
+ ints (list): A list of positive integers
+
+ Returns:
+ int: The absolute difference.
+ """
+ # Check we are only given positive integers
+ if any(i < 1 for i in ints):
+ raise ValueError('Only positive integers allowed')
+
+ difference = 0
+ for i in ints:
+ # Single digit integers have no difference
+ if i > 9:
+ # Calculate the difference between the number and the sum of the
+ # individual digits
+ difference += i - sum(int(d) for d in str(i))
+
+ return abs(difference)
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = sum_difference(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-320/sgreen/python/test.py b/challenge-320/sgreen/python/test.py
new file mode 100755
index 0000000000..81900ad922
--- /dev/null
+++ b/challenge-320/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/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.maximum_count([-3, -2, -1, 1, 2, 3]), 3)
+ self.assertEqual(ch_1.maximum_count([-2, -1, 0, 0, 1]), 2)
+ self.assertEqual(ch_1.maximum_count([1, 2, 3, 4]), 4)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.sum_difference([1, 23, 4, 5]), 18)
+ self.assertEqual(ch_2.sum_difference([1, 2, 3, 4, 5]), 0)
+ self.assertEqual(ch_2.sum_difference([1, 2, 34]), 27)
+
+
+if __name__ == '__main__':
+ unittest.main()