aboutsummaryrefslogtreecommitdiff
path: root/challenge-325/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-325/sgreen/python')
-rwxr-xr-xchallenge-325/sgreen/python/ch-1.py34
-rwxr-xr-xchallenge-325/sgreen/python/ch-2.py32
-rwxr-xr-xchallenge-325/sgreen/python/test.py21
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-325/sgreen/python/ch-1.py b/challenge-325/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..899fdf8dbf
--- /dev/null
+++ b/challenge-325/sgreen/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def consecutive_ones(ints: list) -> int:
+ """
+ Function to find the maximum number of consecutive 1s in a binary array.
+ :param ints: List of integers (0s and 1s)
+ :return: Maximum count of consecutive 1s
+ """
+ max_count = 0
+ current_count = 0
+
+ for num in ints:
+ if num == 1:
+ current_count += 1
+ if current_count > max_count:
+ max_count = current_count
+ else:
+ current_count = 0
+
+ return max_count
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = consecutive_ones(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-325/sgreen/python/ch-2.py b/challenge-325/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..5110b21fa8
--- /dev/null
+++ b/challenge-325/sgreen/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def final_price(prices: list) -> list:
+ """
+ Function to calculate the final price of items after applying discounts.
+ :param prices: List of integers representing item prices
+ :return: List of final prices after discounts
+ """
+ solution = []
+ for i in range(len(prices)):
+ discount = 0
+ for j in range(i + 1, len(prices)):
+ if prices[j] <= prices[i]:
+ discount = prices[j]
+ break
+ solution.append(prices[i] - discount)
+
+ return solution
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = final_price(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-325/sgreen/python/test.py b/challenge-325/sgreen/python/test.py
new file mode 100755
index 0000000000..e08bbd66e3
--- /dev/null
+++ b/challenge-325/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.consecutive_ones([0, 1, 1, 0, 1, 1, 1]), 3)
+ self.assertEqual(ch_1.consecutive_ones([0, 0, 0, 0]), 0)
+ self.assertEqual(ch_1.consecutive_ones([1, 0, 1, 0, 1, 1]), 2)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.final_price([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3])
+ self.assertEqual(ch_2.final_price([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
+ self.assertEqual(ch_2.final_price([7, 1, 1, 5]), [6, 0, 1, 5])
+
+
+if __name__ == '__main__':
+ unittest.main()