Hello how are you? I have a challenge https://www.codewars.com/kata/53e57dada0cb0400ba000688/solutions/python and I’ve stuck on it for a 2 days, I created the first solution but it was slow(using itertools.permutations), and then I’ve make this block of code.
import math def listPosition(word): n = 0 wlsort = list(word) wlsort.sort() wcopy = word while wcopy: for i in range(wlsort.index(wcopy)): n += math.factorial(len(wlsort) - 1) wlsort.pop(wlsort.index(wcopy)) wcopy = wcopy[1:] return n + 1
but it doesn’t work with words like
bookkeeper, Do you have any hints, or idea on how to solve it?
I think my problem is that when I have a word like
car it will brute-force it like ->
and if i have
caar it will brute-force it like ->
aarc and it doesn’t do
acar because it is brute-forcing alphabetically.
Here is my attempt. It’s quite straightforward, if you read the req. carefully. Please let me know if you have any questions.
from math import factorial as fact def listPosition(word): count = 0 while len(word): first = word uniqs = set(word) possibles = fact(len(word)) for ch in uniqs: possibles /= fact(word.count(ch)) for ch in uniqs: if ch < first: count += possibles/len(word) * word.count(ch) word = word[1:] return count + 1