diff options
Diffstat (limited to 'challenge-258/sgreen/python')
| -rwxr-xr-x | challenge-258/sgreen/python/ch-1.py | 18 | ||||
| -rwxr-xr-x | challenge-258/sgreen/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-258/sgreen/python/test.py | 21 |
3 files changed, 66 insertions, 0 deletions
diff --git a/challenge-258/sgreen/python/ch-1.py b/challenge-258/sgreen/python/ch-1.py new file mode 100755 index 0000000000..caab0a1a85 --- /dev/null +++ b/challenge-258/sgreen/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import sys + + +def count_even_digits(ints: list) -> int: + return sum(1 for i in ints if len(str(i)) % 2 == 0) + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = count_even_digits(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-258/sgreen/python/ch-2.py b/challenge-258/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f670d54629 --- /dev/null +++ b/challenge-258/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def sum_of_values(ints: list, k: int) -> int: + total = 0 + + for pos, value in enumerate(ints): + # Count the number of ones in the position value + if k == bin(pos).count('1'): + # If it matches the 'k', add the value to the total + total += value + + return total + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = sum_of_values(array, k) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-258/sgreen/python/test.py b/challenge-258/sgreen/python/test.py new file mode 100755 index 0000000000..25e8507722 --- /dev/null +++ b/challenge-258/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.count_even_digits([10, 1, 111, 24, 1000]), 3) + self.assertEqual(ch_1.count_even_digits([111, 1, 11111]), 0) + self.assertEqual(ch_1.count_even_digits([2, 8, 1024, 256]), 1) + + def test_ch_2(self): + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 1), 17) + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 2), 11) + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 0), 2) + + +if __name__ == '__main__': + unittest.main() |
