fork download
  1. # Recursive Solution
  2. def letter_by_letter_recursive(word1, word2):
  3. print(word1)
  4. if word1 == word2:
  5. return
  6. for i,_ in enumerate(word1):
  7. if word1[i] != word2[i]:
  8. new_word = word2[:i+1] + word1[i+1:]
  9. return letter_by_letter_recursive(new_word, word2)
  10.  
  11.  
  12. # Naive Solution
  13. def letter_by_letter_naive(word1, word2):
  14. print(word1)
  15. w1, w2 = list(word1), word2
  16. i = 0
  17. while w1 != list(w2):
  18. if w1[i] != w2[i]:
  19. w1[i] = w2[i]
  20. print(''.join(w1))
  21. i += 1
  22.  
  23.  
  24. # Test Cases
  25. print('* * * Recursive Test * * *')
  26. tests = [('floor', 'brake'), ('wood', 'book'), ('a fall to the floor', 'braking the door in')]
  27. for test in tests:
  28. print('\n{} --> {}'.format(test[0], test[1]))
  29. letter_by_letter_recursive(test[0], test[1])
  30.  
  31.  
  32. print('\n\n* * * Niave Test * * *')
  33. for test in tests:
  34. print('\n{} --> {}'.format(test[0], test[1]))
  35. letter_by_letter_naive(test[0], test[1])
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
* * * Recursive Test * * *

floor --> brake
floor
bloor
broor
braor
brakr
brake

wood --> book
wood
bood
book

a fall to the floor --> braking the door in
a fall to the floor
b fall to the floor
brfall to the floor
braall to the floor
brakll to the floor
brakil to the floor
brakin to the floor
brakingto the floor
braking o the floor
braking t the floor
braking ththe floor
braking thehe floor
braking the e floor
braking the d floor
braking the dofloor
braking the dooloor
braking the dooroor
braking the door or
braking the door ir
braking the door in


* * * Niave Test * * *

floor --> brake
floor
bloor
broor
braor
brakr
brake

wood --> book
wood
bood
book

a fall to the floor --> braking the door in
a fall to the floor
b fall to the floor
brfall to the floor
braall to the floor
brakll to the floor
brakil to the floor
brakin to the floor
brakingto the floor
braking o the floor
braking t the floor
braking ththe floor
braking thehe floor
braking the e floor
braking the d floor
braking the dofloor
braking the dooloor
braking the dooroor
braking the door or
braking the door ir
braking the door in