diff options
Diffstat (limited to 'challenge-283/sgreen/python')
| -rwxr-xr-x | challenge-283/sgreen/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-283/sgreen/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-283/sgreen/python/test.py | 21 |
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-283/sgreen/python/ch-1.py b/challenge-283/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7f5ed66cd7 --- /dev/null +++ b/challenge-283/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def unique_number(ints: list) -> int: + # Calculate the frequency of each integer + freq = Counter(ints) + + # Get the integers that appear only once + only_once = [i for i in freq if freq[i] == 1] + + if len(only_once) == 1: + return only_once[0] + + if len(only_once) == 0: + raise ValueError('No values only appear once') + + raise ValueError('More than one value appears once') + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = unique_number(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-283/sgreen/python/ch-2.py b/challenge-283/sgreen/python/ch-2.py new file mode 100755 index 0000000000..978ad41f6d --- /dev/null +++ b/challenge-283/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def digit_count_value(ints: list) -> bool: + # Calculate the frequency of each integer + freq = Counter(ints) + + for idx, value in enumerate(ints): + # Check the index only appear value number of times + if freq[idx] != value: + return False + + # Checks pass + return True + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = digit_count_value(array) + print('true' if result else 'false') + + +if __name__ == '__main__': + main() diff --git a/challenge-283/sgreen/python/test.py b/challenge-283/sgreen/python/test.py new file mode 100755 index 0000000000..9073c9d954 --- /dev/null +++ b/challenge-283/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.unique_number([3, 3, 1]), 1) + self.assertEqual(ch_1.unique_number([3, 2, 4, 2, 4]), 3) + self.assertEqual(ch_1.unique_number([1]), 1) + self.assertEqual(ch_1.unique_number([4, 3, 1, 1, 1, 4]), 3) + + def test_ch_2(self): + self.assertTrue(ch_2.digit_count_value([1, 2, 1, 0])) + self.assertFalse(ch_2.digit_count_value([0, 3, 0])) + + +if __name__ == '__main__': + unittest.main() |
