Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Fall 2003: Recursion

Questions, comments, answers?

Back to Fall2003 Final Exam Review


a)	print #2, print #1, print #3
b)	5
c) def upDown(word):
     if len(word)==1:
       print word    #Print #1
       return
     print word      #Print #2
  upDown(word[0:len(word)-1])
  print word      #Print #3



Can you fix the indentation on the code? It's not right as-is. Mark Guzdial


How about the following indentation?
c)def upDown(word):
    if len(word)==1:
      print word    #Print #1
      return
  print word      #Print #2
  upDown(word[0:len(word)-1])
  print word      #Print #3


Should the print statements be indented the same amount as the def?

Nope. Mark Guzdial



def upDown(word):
if len(word) == 1:
print word
return
print word
upDown(word[0:len(word)-1])
print word


Could you please explain b? Why is it 5?

Hello, ello, llo, lo, o. The program is first ran using the input "Hello". The program is called again- now using "ello". The program is called once again using "llo". And so on. Until it gets to just the letter "o". At this point, five programs are running at once. (Now the programs begin to finish one by one- so, five is maximum). I hope this helps. #12

You can also think about it from the perspective of functional programming. Functions in functional programming tend to process one small bit of data at a time. These recursive functions work similarly, where the one small bit is each letter. Five letters, five calls to the function. Mark Guzdial


Wouldn't you need more memory when using recursion? Would that be its downside?

It depends on how the recursion is implemented and the language. Java is really bad with recursion, so it does take too much memory. Scheme is excellent with recursion, so well-written recursive functions take no more memory than a FOR loop. Mark Guzdial



I wish my wish not be granted :-)

Here's a hint that may work with less typing!: word[:-1]
Chris Reinig

couldn't you just use the spacing that was in the example in the final review??





Link to this Page