Alessandro - 2 months ago 22

Python Question

I have this strings : "case" and "ro". I need to get every combination between them, where I substitute the single letters from the second string into the first one, but only if the letter is greater than the other one. Ex: in "rose : r>c, o>a. Other Examples are: "cose", "roso", "coso"

I tried writing something, using iteration, but it goes on a infinite loop and doesn't generate nothing. If someone can help me figure out a simpler way to do this, it would be great.

Answer

I am answering on basis of what i have understood from the question. Please check whether the following answer is correct for you.If yes then I can explain the code later.

```
def solve( s1, s2): # prints all combinations of s1 from letters of s2 in a list
ans = [s1]
if s1=="":
return ans
tmp = solve( s1[1:],s2 )
ans += [ s1[0]+x for x in tmp ]
for c in s2:
if c>s1[0]:
ans += [ c+x for x in tmp ]
return list(set(ans))
print solve("case","ro")
```