i see how this code prints s which is 'cad' but, then it somehow backtracks to print s again which is 'cat' according to the results after i execute it. can someone walk me through how this works? it would be a great help.

def eds(k,L,s):
   """
   starting with the k-th list in L,
   adds letters to the current string s
   """
   if k >= len(L):
      print s
   else:
      for i in range(0,len(L[k])):
         eds(k+1,L,s+L[k][i])

def main():
   """
   enumerates letter combinations
   """
   S = ['c','s','v']
   V = ['a','e','i','o','u']
   E = ['d','t','w']
   L = [S,V,E]
   eds(0,L,"")

main()

Let the code explain how it works:

tab = " " * 2

def eds(k,L,s):
   """
   starting with the k-th list in L,
   adds letters to the current string s
   """
   print("%sentering eds(%d, L, %s)" % (tab*k, k, repr(s)))
   if k >= len(L):
      print("%sfound -> %s" % (tab*k, repr(s)))
   else:
      for i in range(0,len(L[k])):
         print("%sloop: i = %d" % (tab*k, i))
         eds(k+1,L,s+L[k][i])

def main():
   """
   enumerates letter combinations
   """
   S = ['c','s','v']
   V = ['a','e','i','o','u']
   E = ['d','t','w']
   L = [S,V,E]
   eds(0,L,"")

main()

""" my output --->
entering eds(0, L, '')
loop: i = 0
  entering eds(1, L, 'c')
  loop: i = 0
    entering eds(2, L, 'ca')
    loop: i = 0
      entering eds(3, L, 'cad')
      found -> 'cad'
    loop: i = 1
      entering eds(3, L, 'cat')
      found -> 'cat'
    loop: i = 2
      entering eds(3, L, 'caw')
      found -> 'caw'
  loop: i = 1
    entering eds(2, L, 'ce')
    loop: i = 0
      entering eds(3, L, 'ced')
      found -> 'ced'
    loop: i = 1
      entering eds(3, L, 'cet')
      found -> 'cet'
    loop: i = 2
      entering eds(3, L, 'cew')
      found -> 'cew'
  loop: i = 2
    entering eds(2, L, 'ci')
    loop: i = 0
      entering eds(3, L, 'cid')
      found -> 'cid'
    loop: i = 1
      entering eds(3, L, 'cit')
      found -> 'cit'
    loop: i = 2
      entering eds(3, L, 'ciw')
      found -> 'ciw'
  loop: i = 3
    entering eds(2, L, 'co')
    loop: i = 0
      entering eds(3, L, 'cod')
      found -> 'cod'
    loop: i = 1
      entering eds(3, L, 'cot')
      found -> 'cot'
    loop: i = 2
      entering eds(3, L, 'cow')
      found -> 'cow'
  loop: i = 4
    entering eds(2, L, 'cu')
    loop: i = 0
      entering eds(3, L, 'cud')
      found -> 'cud'
    loop: i = 1
      entering eds(3, L, 'cut')
      found -> 'cut'
    loop: i = 2
      entering eds(3, L, 'cuw')
      found -> 'cuw'
loop: i = 1
  entering eds(1, L, 's')
  loop: i = 0
    entering eds(2, L, 'sa')
    loop: i = 0
      entering eds(3, L, 'sad')
      found -> 'sad'
    loop: i = 1
      entering eds(3, L, 'sat')
      found -> 'sat'
    loop: i = 2
      entering eds(3, L, 'saw')
      found -> 'saw'
  loop: i = 1
    entering eds(2, L, 'se')
    loop: i = 0
      entering eds(3, L, 'sed')
      found -> 'sed'
    loop: i = 1
      entering eds(3, L, 'set')
      found -> 'set'
    loop: i = 2
      entering eds(3, L, 'sew')
      found -> 'sew'
  loop: i = 2
    entering eds(2, L, 'si')
    loop: i = 0
      entering eds(3, L, 'sid')
      found -> 'sid'
    loop: i = 1
      entering eds(3, L, 'sit')
      found -> 'sit'
    loop: i = 2
      entering eds(3, L, 'siw')
      found -> 'siw'
  loop: i = 3
    entering eds(2, L, 'so')
    loop: i = 0
      entering eds(3, L, 'sod')
      found -> 'sod'
    loop: i = 1
      entering eds(3, L, 'sot')
      found -> 'sot'
    loop: i = 2
      entering eds(3, L, 'sow')
      found -> 'sow'
  loop: i = 4
    entering eds(2, L, 'su')
    loop: i = 0
      entering eds(3, L, 'sud')
      found -> 'sud'
    loop: i = 1
      entering eds(3, L, 'sut')
      found -> 'sut'
    loop: i = 2
      entering eds(3, L, 'suw')
      found -> 'suw'
loop: i = 2
  entering eds(1, L, 'v')
  loop: i = 0
    entering eds(2, L, 'va')
    loop: i = 0
      entering eds(3, L, 'vad')
      found -> 'vad'
    loop: i = 1
      entering eds(3, L, 'vat')
      found -> 'vat'
    loop: i = 2
      entering eds(3, L, 'vaw')
      found -> 'vaw'
  loop: i = 1
    entering eds(2, L, 've')
    loop: i = 0
      entering eds(3, L, 'ved')
      found -> 'ved'
    loop: i = 1
      entering eds(3, L, 'vet')
      found -> 'vet'
    loop: i = 2
      entering eds(3, L, 'vew')
      found -> 'vew'
  loop: i = 2
    entering eds(2, L, 'vi')
    loop: i = 0
      entering eds(3, L, 'vid')
      found -> 'vid'
    loop: i = 1
      entering eds(3, L, 'vit')
      found -> 'vit'
    loop: i = 2
      entering eds(3, L, 'viw')
      found -> 'viw'
  loop: i = 3
    entering eds(2, L, 'vo')
    loop: i = 0
      entering eds(3, L, 'vod')
      found -> 'vod'
    loop: i = 1
      entering eds(3, L, 'vot')
      found -> 'vot'
    loop: i = 2
      entering eds(3, L, 'vow')
      found -> 'vow'
  loop: i = 4
    entering eds(2, L, 'vu')
    loop: i = 0
      entering eds(3, L, 'vud')
      found -> 'vud'
    loop: i = 1
      entering eds(3, L, 'vut')
      found -> 'vut'
    loop: i = 2
      entering eds(3, L, 'vuw')
      found -> 'vuw'
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.