So, someone challenged me to find their username on a platform. They told me its a word with repeating characters, but otherwise spelled correctly. I’ve been trying to use IterTools to create a list of all the possible combinations using combinations_with_replacement but I’m unsure how to get the minimum number of characters to work. In my attempt I completely broke my script.
For example, “elephant” could be eelephant, elepphaant, eleephhhaanttttt, etc. All the characters are needed, and they have to be in the right order, but with any number of repeated characters. I haven’t yet worked out sorting, that will come afterwards, perhaps through a different means.
I tried using itertools chain to create a minimum number, but when I continued trying, it stopped listing combinations when run. Not sure what I did wrong. Will continue messing around to see if I can get it.
If anyone knows how to accomplish this, please let me know! Perhaps I’m using the wrong library altogether, so figured I’d ask the people who know better than I!
Thanks!
from itertools import combinations_with_replacement, chain
word ="elephant"
n = 10
cc = chain.from_iterable(combinations_with_replacement(word, i) for i in range(n, len(word)+1))
list(cc)
Tyson Hufnagel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.