Introduction to Computer Programming

Program 1: Fruitful Functions

Due date: Friday, September 15 at 5pm

Instructions

Unless otherwise indicated, use only the Python operations and commands we have described thus far in class. If you are not sure if you should use a particular construct, check with the instructor.

Start by working with the supplied starter file, but save the file as hw1_*yourusername*.py where yourusername is your @gm.slc.edu username.

Replace <YOUR NAME GOES HERE> with your name.

Fill in all the areas of the supplied file marked by "...".

As mentioned above, name your file hw1_*yourusername*.py where yourusername is your @gm.slc.edu username. (If your email was msiff@gm.slc.edu, you would name your file hw1_msiff.py.)

When ready to submit your work, reply to email from the instructor the announcing the assignment (it should have the subject: ICP17: Programming Assignment 1) and attach the one file. (Do not paste the contents of the file into the body of the message. Do not expect the instructor to read anything in the body of the message. The instructor will only examine the attached file.)

Complete the function tri_num(m) that returns half of the product of m and one more than m. For example, if the input is 4, it should returns one half of 4 times 5. Examples:

Complete the function easy(y) so that it applies the function complicated twice over to its input: in other words it calls the function complicated on y and then calls it again on what that returns. Examples:

>>> easy(1)
0
>>> easy(2)
52
>>> easy(3)
-17

Complete the function add_character(c, x) that returns the character that corresponds to adding x to the ASCII value of c. The body of your function should be just one line - a return statement. The expression returned must involve composition and the built-in functions chr and ord. Examples:

Complete the function pow3(n) so that it returns 3 to the power of nwithout using the built-in exponentiation operator (**). You can (and should use a for loop). Remember that 3 raised to the 0 power is 1. Examples:

A palindrome is a string that is its own mirror image: it reads the same reading left to right as it would be from right to left. (For example, 'radar' and 'amanaplanacanalpanama' are palindromes; 'radon' is not.) Complete the function make_palindrome(start_character, n) that returns a string of length 4n+1 that is a palindrome centered around start character. The sequence of characters that are mirrored to the left and right of the start character should consists of pairs of symbols that ascend and descend (in terms of ASCII) from start_character in lockstep. Examples:

(You should not assume that yertle's pen is already down. Nor should you assume that yertle is facing any particular direction. Consider using pendown and setheading. )

Write a (nonfruitful) function nested_squares(n, start_length, gap) that draws n nested squares from the inside out using turtles and the draw_square function from the preceding problem. The innermost square has sides of length start_length. Each subsequent square should be centered around the preceding smaller square with gap units of space between the squares on all sides. As an example:

>>> nested_squares(5, 20, 10)

should draw:

Write a fruitful function from scratch that takes two arguments. The arguments can be integers, strings or one of each. The type of the result (string or integer) is up to you. Your function should compute something that we have not discussed in class or lab. Include documentation (a docstring!) with examples following the models used elsehwere on the assignment.