![]() Not a single string permutation was actually produced. This produced correct output and passed the puzzle at high speed (performance metrics not recorded, but noticably different). ![]() So you'll want to grab some pen and paper and find a formulation of that problem.įollowing this reasoning, among similar suggestions from others, I tried an approach based more in enumerative combinatorics: from math import factorial as F If I understand the puzzle correctly, it only asks to return the position of the input, not all of the permutations. I would think you can come up with a formula to calculate the position of the input word, based on the alphabetic ordering (since the list is sorted alphabetically) of the letters. Since I strongly emphasized identifying ways to speed up the code I posted, I'm upvoting everyone else for having made improvements in that posted the following comment: Providing my own answer under the assumption that a good way to optimize code is to not use it in the first place. Return sorted(list(set(permutations(word)))).index(word) + 1 """Return the anagram list position of the word""" Perms = perms + permutations(word + word, word) """Return list of all possible permutatons of given characters""" Even so, I'd rather submit a handcoded solution with competitive performance knowing why mine was worse than to opt for a module. But to be fair, this includes time printing so this might not be a fair comparison. ![]() I'm profiling in the meantime to see if I can answer my own question, but I invite those who see the problem outright to help me understand how I slowed this down.ĮDIT: My solution appears to outperform itertools.permutations. ![]() I took a (possibly naive) dynamic programming approach using memoization using a dict keyed by one particular permutation of some character set, but that's not enough. One of the test cases passed "BOOKKEEPER" as an input, which takes ~4.2s for me to complete. My problem is that the puzzle limits my execution time to 500ms. The puzzle says that I am to take a string S, find all permutations of its characters, sort my results, and then return the one-based index of where S appears in that list.įor example, the string 'bac' appears in the 3rd position in the sorted list of its own permutations. I solved a puzzle but need to optimize my solution. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |