2

I have a string for which I have to generate all the permutations. The constraint being each replaceable position has a fixed set of characters.

For example, I have string

"pl"+i1+"r"+i2+"t"+u1+a1+"q"+i3+"t"+e1+"-br"+a2+"h"+c1+"n"+e2+e3

All i* belong to set i.

All e* belong to set e.

All u* belong to set u.

All a* belong to set a.

This is what I tried:

e = ["e", "é", "ë", "ê", "è"]
a = ["a", "â", "à", "æ"]
i = ["i", "ç", "î", "ï", "ÿ"]
u = ["ù", "û", "u"]
c = ["c", "ç"]
for e1 in e:
  for e2 in e:
    for e3 in e:
      for i1 in i:
        for i2 in i:
          for i3 in i:
            for a1 in a:
              for a2 in a:
                for u1 in u:
                  for c1 in c:
                    mystr = "pl"+i1+"r"+i2+"t"+u1+a1+"q"+i3+"t"+e1+"-br"+a2+"h"+c1+"n"+e2+e3
                    print(mystr)

It works but it is not scalable and I will have to write more for loops for a different string.

Can someone help me with a better version of this so that if I pass a list to a method and it should return all the possible permutations as a list.

Input: ["pl", i, "r", i, "t", u, a, "q", i, "t", e, "-br", a, "h", c, "n", e, e] where a, e, i, u, c are list of accented characters.

Output: All possible strings

Saif
  • 2,530
  • 3
  • 27
  • 45
  • 1
    What you are asking for are **not** permutations - permutations are multiple values all drawn from the *same* list, *without replacement* (i.e. not allowing the same element to be used more than once). Once you know the proper terminology, it is easy to research the solution - for example, as in the linked duplicate. – Karl Knechtel May 30 '22 at 10:52
  • Yeah, right. Thanks. – Saif May 30 '22 at 13:17

0 Answers0