diff options
| author | Simon Green <mail@simon.green> | 2024-03-17 18:20:49 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-03-17 18:20:49 +1100 |
| commit | 07699eac47d83850fec02e87c69cffff471236ac (patch) | |
| tree | cd9acbdbfd5183c3d037fc5e306c4880b02edc2e /challenge-260/sgreen/python/ch-2.py | |
| parent | aad9041674e6575f6c24925854d6353f2098a117 (diff) | |
| download | perlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.tar.gz perlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.tar.bz2 perlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.zip | |
Simon's solution to challenge 260
Diffstat (limited to 'challenge-260/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-260/sgreen/python/ch-2.py | 41 |
1 files changed, 41 insertions, 0 deletions
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() |
