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 Spring 2004: Recursion

Link back to Sp2004 Final Exam Review

Questions and Answers?



a) print #2 prints the shortening on the way down words
print #1 prints the single letter word
print #3 prints the longer growing words

b) there are five copies of downUp running with the input of "hello" until it reaches the single letter in the middle.. when it reaches this point it prints and returns, then it runs another five copies to print the longer growing words Sana Yousufi

Sana, (b) is wrong. Mark Guzdial


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

Sana Yousufi

b)There are 4 to get to the single letter and then 4 after....?

Trace it out, or use the little elves approach. How many people (function invocations) will be standing at the front of the room? Mark Guzdial


Could you please explain how we go from getting down to "H" in the middle, back up, letter by letter til it's "Hello" again? I'm not sure how we ever get to that third print statement.

Kelli Webb

Never mind. I had an "elf" epiphany!

Kelli Webb

will there be nine elves once your done with the function? one elf for each word? or are there only five elves total? Sana Yousufi

"One elf for each word"? Did you mean "one elf for each letter"? Mark Guzdial

for part b I think they are 9...am I right? I tried the elf approach, but not sure if I did it correct...
Kyla LeCroy

shouldn't the answer to part b be five. If you use the elf approach. The first elf has hello, the second ello, the third llo, the fourth lo, and the fifth o. as soon as the fifth one (the one with only one letter) prints his word, he "sits down" then the other ones print their word again and sit down in the reverse order that they came up. So the most "elves" ever standing at one time will be five. Does that make sense?
Heather Symon

Thank you, Heather! Mark Guzdial


CAn you please explain the answer to a?

For part c, is the upDown(word[:-1]) above correct? I thought that you could only use 0 and positive numbers to refer to elements in a string. If it's not right, though, I don't know what would be.

Take a look at the recursion slides. Yup, -1 is fine. Mark Guzdial


An attempt to explain a:
It will use print #1 when it gets down to one letter, b/c that's what the if statement says to do. It will use print #2 as long as downUp(word[1:]) can run (as long as there is more than one letter left).
I'd have trouble explaining #3, nor do I have time b/c today is my birthday and I'm going to dinner. But it's just been fantastic studying all day on my birthday.
Jonathan Laing


HAPPY BIRTHDAY JONATHAN!!!
Rae Smith

Thanks! -JL



Link to this Page