diff options
Diffstat (limited to 'challenge-260/sgreen/python')
| -rwxr-xr-x | challenge-260/sgreen/python/ch-1.py | 41 | ||||
| -rwxr-xr-x | challenge-260/sgreen/python/ch-2.py | 41 | ||||
| -rwxr-xr-x | challenge-260/sgreen/python/test.py | 21 |
3 files changed, 103 insertions, 0 deletions
diff --git a/challenge-260/sgreen/python/ch-1.py b/challenge-260/sgreen/python/ch-1.py new file mode 100755 index 0000000000..90ba9f5b09 --- /dev/null +++ b/challenge-260/sgreen/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from collections import defaultdict +import sys + + +def uniq_occurrences(ints: list) -> bool: + """Check the frequency of integers are unique + + Args: + ints (list): The integers + + Returns: + bool: Whether the frequency of the list is unique + """ + + # Calculate the frequency of each integer + freq = defaultdict(int) + for i in ints: + freq[i] += 1 + + # Return if we have seen a frequency already + seen = {} + for i in freq.values(): + if i in seen: + return False + seen[i] = 1 + + # We have a unique orrurrence list + return True + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = uniq_occurrences(array) + print('1' if result else '0') + + +if __name__ == '__main__': + main() diff --git a/challenge-260/sgreen/python/ch-2.py b/challenge-260/sgreen/python/ch-2.py new file mode 100755 index 0000000000..fa649c6b28 --- /dev/null +++ b/challenge-260/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from more_itertools import distinct_permutations +import sys + + +def dictionary_rank(word: str) -> int: + """Calculate the position on the dictionary if all permutations of letters + in the given word were valid. + + Args: + word (str): The input word + + Returns: + int: The position + """ + # Convert to lower case, and sort the letters alphabetically + tuple_word = tuple(word.lower()) + letters = sorted(tuple_word) + count = 0 + + # Go through each sorted unique permutation until we find the word we want + for perm in distinct_permutations(letters): + count += 1 + if perm == tuple_word: + break + else: + raise ValueError('Cannot find position') + + # Return the rank + return count + + +def main(): + # Convert input into integers + result = dictionary_rank(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-260/sgreen/python/test.py b/challenge-260/sgreen/python/test.py new file mode 100755 index 0000000000..1a7ed7de1d --- /dev/null +++ b/challenge-260/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.assertTrue(ch_1.uniq_occurrences([1, 2, 2, 1, 1, 3])) + self.assertFalse(ch_1.uniq_occurrences([1, 2, 3])) + self.assertTrue(ch_1.uniq_occurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9])) + + def test_ch_2(self): + self.assertEqual(ch_2.dictionary_rank('CAT'), 3) + self.assertEqual(ch_2.dictionary_rank('GOOGLE'), 88) + self.assertEqual(ch_2.dictionary_rank('SECRET'), 255) + + +if __name__ == '__main__': + unittest.main() |
