-3

Given an input n and strings, for example:

3
abc
de
fgh

How can I produce this output, using recursion?

adf
adg
adh
aef
aeg
aeh
bdf
bdg
bdh
bef
beg
beh
cdf
cdg
cdh
cef
ceg
ceh
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 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
  • 43,482
  • 10
  • 63
  • 98