-2

Given an input n and strings, for example:

3
abc
de
fgh

how would it be permuted to the output:

adf
adg
adh
aef
aeg
aeh
bdf
bdg
bdh
bef
beg
beh
cdf
cdg
cdh
cef
ceg
ceh

Its my first time seeing this type of problem, and we're only allowed to use recurssive functions so no imports i only know regular permutation of strings. I dont know how to tackle this problem can someone help me with a solution? thanks in advance!

  • You want to identify the logic and patterns first. Notice how the n-th letter is always one of the n-th string. – Guimoute Nov 04 '20 at 09:56
  • Another one with list of strings [cartesian product from list of strings](https://stackoverflow.com/questions/46900628/cartesian-product-from-list-of-strings) – Tomerikoo Nov 04 '20 at 10:04
  • Did you solve it without recursion? Do you need help with understanding recursion or permutations? – darw Nov 04 '20 at 11:19
  • i didnt solve it i dont know how to in the first place, what is required of us is to sovle that reccursively which i dont know how to do so – Caden Acevedo Nov 04 '20 at 16:08

1 Answers1

0

This can be solved with a regular itertools.product;

In [20]: list(itertools.product('abc', 'de', 'fgh'))                            
Out[20]: 
[('a', 'd', 'f'),
 ('a', 'd', 'g'),
 ('a', 'd', 'h'),
 ('a', 'e', 'f'),
 ('a', 'e', 'g'),
 ('a', 'e', 'h'),
 ('b', 'd', 'f'),
 ('b', 'd', 'g'),
 ('b', 'd', 'h'),
 ('b', 'e', 'f'),
 ('b', 'e', 'g'),
 ('b', 'e', 'h'),
 ('c', 'd', 'f'),
 ('c', 'd', 'g'),
 ('c', 'd', 'h'),
 ('c', 'e', 'f'),
 ('c', 'e', 'g'),
 ('c', 'e', 'h')]
ForceBru
  • 41,233
  • 10
  • 61
  • 89