Homework 7 - Recursive Functions
Homework 7 - Recursive Functions
computing n factorial
computing powers of 2
All of these functions will be written in an application program called recfun.py. The
main program of the application will display a menu of the 3 functions, and prompt
the user to choose one. Depending on the choice, the program will then prompt the
user for the input to be passed to the function. Finally, the program will call the
function and print the result.
Find n!
Write a function that takes as a parameter an integer n and returns n!.
recursive definition of factorial is as follows:
factorial(n) =
examples:
factorial(4) --> 24
factorial(7) --> 5040
if n == 0
n * factorial(n-1)
otherwise
The
Now, we want to write a program that asks the user how theyre doing
After saving the file, run the program and you should see a window
with a smiley face. Let me know if this does not work on your
computer.
from Tkinter import *
window = Tk()
ps2_canvas = Canvas(window, width=500, height=500)
ps2_canvas.grid(row=0, column=0)
# These next four statements are the ones you should play with for your
# program. LEAVE EVERYTHING ELSE (apart from the comments) ALONE
# draw the face
ps2_canvas.create_oval(100, 100, 400, 400, fill='yellow')
# Draw the mouth
ps2_canvas.create_arc(175, 175, 325, 325, start=225, extent=90, style=ARC)
# Draw the left eye
ps2_canvas.create_oval(200, 205, 202, 207, fill='black')
# Draw the right eye
ps2_canvas.create_oval(298, 205, 300, 207, fill='black')
# Keep this line at the end of your program
window.mainloop()
and then draws a face corresponding to the users feeling. For this part,
our program must react to at least three different emotions (first, the
program lets the user know which emotions it can react to, and then it
prompts the user for their emotion. If the user enters an emotion that
the program doesnt react to, the program must print a message to
that effect). Below, Im showing faces for happy, sad, and pensive;
however, youre most welcome to choose any three different emotions
that you like.
Feel free to change any features of the face if you like to, e.g., change
the eyes, add eyebrows, change the color of the face for specific
emotions, add ears, hair, etc. (be as creative as possible, all the
students in the two sections are going to vote to choose the
three coolest programs). Also, note that Python understands quite a
few color names. Please see the attached instructions on how the
create_oval, create_arc, and create_line functions work.
Feel free to change the greeting message and/or the message shown
when the user inputs an emotion that the program doesnt recognize.
The program greets the user and lets them know which emotions it can
react to. It then asks the user a question to see how theyre doing. The
user then enters the answer happy.
Then the program draws the following face as a response to the users
happy emotion.
Lets run the program again, this time, the user says theyre sad.
Lets run it again. This time, lets enter pensive as our answer:
Hello, there. Im a robot that understands these emotions:
happy, sad, and pensive.
How are you doing today? pensive
Lets run the program once again. This time, lets enter a response to
which the program doesnt know how to react:
Hello, there. Im a robot that understands these emotions:
happy, sad, and pensive.
How are you doing today? mad
Sorry, I dont recognize your input.
Drawing a carpet
Next, write a program called carpet.py which draws an image known as Sierpinski's
carpet, as shown below:
To create this drawing, divide the canvas area into a 3x3 grid. Fill in the center
section and then draw a Sierpinski carpet (with one fewer recursion levels) in each
of the remaining 8 surrounding squares.
You can use the method
canvas.create_rectangle(x1, y1, x2, y2, int y, width=0, fill=<your choice of
color>);
to draw a rectangle with no border filled with the specified color
The picture above shows a Sierpinski carpet of order 6.
As with bubbles, carpet.java should prompt the user for the number of recursion
levels, and then draw the picture.
Drawing a gasket
The next image is known as either Sierpinski's triangle or Sierpinski's gasket.
Shown below is a Sierpinski gasket of order 5:
Call your program gasket.py. Again, the main program should prompt the user for
the number of levels, then draw the picture.