Reputation: 7669
I have a sequence of letters and want to find all combinations of letters possible by cutting off the letters around a certain letter.
C is my special letter here and X can be anything, so with a sequence:
XCXX
So all the possible combinations would be:
XCXX
XCX
XC
CXX
CX
C
Is there a python function for this, or should I code it from scratch?
Thank you
Upvotes: 3
Views: 1666
Reputation: 40833
No, but you could do it as a one-liner.
data = "XCXX"
cIndex = data.index("C")
permutations = [data[i:j+1] for i in range(cIndex+1)
for j in range(cIndex, len(data))]
Upvotes: 0
Reputation: 4938
I would have coded it from scratch like this:
def cuts(s,i):
return [ s[a:b] for a in range(i+1) for b in range(i+1,len(s)+1)]
where s
is the string and i
is the index of your "special letter" in s
.
Example:
>>> cuts('XCXX', 1)
['XC', 'XCX', 'XCXX', 'C', 'CX', 'CXX']
Upvotes: 4
Reputation: 28846
Try counting the number of elements before and after your special element C
. Then your combinations are uniquely defined by those two numbers, and you can just use string slicing to get a specific one.
Upvotes: 3