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

def upDown(word):
  if len(word)==1:
    print word    #Print #1
    return
  print word      #Print #2
  upDown(word[:-1])
  print word      #Print #3

?
If your ? is asking if it works, why don't you type it into JES, run it and see? Kelly Lyons


for the downUp code
print #2 prints the ever-shortening word
print #1 prints the single letter in the middle
print #3 prints the longer-growing words
and at most there are nine copies of downUp running with the input Hello.
right?
9 lines are printed. One copy of downUp can print more than one line.
-Blake O'Hare
Any hints on part C? Every program I've written for it (10+) have given me the Hello, ello, llo.... one instead of the Hello, Hell, Hel...
ok just kidding.. that program up there worked.. the first time i tried it i must've typed it wrong

b) I think 5
what does part B mean?

Is the answer to (b) just 1 copy?
A recursive function calls itself. This means that when you run downUp("hello"), it is going to create a new copy of downUp("ello") downUp("Hello") cannot finish running until downUp("ello") finish itself, so at that point there are 2 copies of the function running. But downUp("ello") will create a new copy which will create a new copy... until one of the copies contains a string of only one letter. Then, that copy will run the if statment, and not create a new copy. It will end, and then the copy that called it will end, and so on until the original call finishes. Part b is asking what is the largest number of copies running all at once. No, the answer is not 1. Kelly Lyons
is the answer to part b 5? no one answered if it was or not
Yes. Kelly Lyons
We won't necessarily comment on correct answers. But if you really want to make sure you're right, run downUp(hello) with the "Watcher" on. Everytime a new copy of def downUp(word) begins running, it will be listed, and none of them end until it returns. Summer McWilliams
I'm not exactly clear about part c of this problem. Why would changing line 6 to downUp(word[:-1]) make it take a letter off of the end? What does this mean?
the word[:-1] means take the word starting from the beginning of the word (This is becuase there is no number before the : so it assumes start at the beginning) and go until the length of the word -1. This is becuase the notation works very similar to the wrapping of 0-255 for colors. -1 means 255 when you are setting your red,green,blue. For a word, it does a similar thing, and it goes to the end of the word, and then back one letter. Kelly Lyons|



Link to this Page