PreLab 06

Recursive Madeness!
Due in class on Wednesday, March 11

In this prelab you will formulate some of the ideas necessary to complete Lab 06. Because we have an exam coming up, the last question has nothing to do with the lab; it is simply here to help you prepare for the exam.

Part 0 - Recursive Output

For the following two problems, work out by hand what the function will do. If you want to check your answers by typing them into Python, that is fine, but you need to be able to think about recursive functions without running them..

Function strange

   def strange(x) :
      if x <= 0 :
         return 1
      else :
         return 5 * strange(x-1) - 2

Function weird

   def weird(x) :
      if x > 0 :
         print(x,"",end='')
         if x%2 == 0 :
            weird(x-3)
            weird(x-2)
         else :
            weird(x-1)
1) What is the output of print(strange(3))

2) What is the output of weird(8)

Part 1 - Geometry

Consider the following figure.

Let (Ax, Ay) be the coordinates of point A, (Bx, By) those of point B, and (Cx, Cy) those of point C. The points P, Q and R are the midpoints of the three edges of the big triangle.

3) Specify the x and y coordinates of P, Q and R in terms of the coordinates of A, B and C. Don't assume that any coordinates are 0 or that there is any relationship between the points (the triangle ABC pictured happens to be fairly symmetric, but you shouldn't assume this will necessarily be the case).

The following question has nothing to do with the lab or with recursion. This will give you some practice writing code on paper, which is a skill that a lot of beginning programming students struggle with.

4) In Tom Stoppard's play Rosencrantz and Guildenstern are Dead Guildenstern (or is it Rosencrantz?) tosses a coin 92 times and gets Heads each time. Write a function Guildenstern(n) that tosses a coin n times and returns the length of the largest run of consecutive Heads. For example, if n is 15 and the sequence of tosses is HHTHTTHTHHHHTHT then the function would return 4 because at one point there were 4 Heads in a row. Note that you can toss a coin with random.randInt(0, 1), which will return 0 (Heads) or 1 (Tails).

 

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.