CS 21: Algorithmic Problem Solving

 

Homework #4: Functions

Due 11:59pm Tuesday, February 20

Remember to run update21 to get the files needed for this assignment. The program handin21 will only submit files in the cs21/homework/4 directory.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page

Some of the problems will have optional components that allow you to further practice your skills in python. Optional portions will not be graded, but may be interesting for those wanting some extra challenges.

Q1: Unix user names

Write a function by editing the file username.py that computes a computer user name by concatenating the first letter of the user's first name, (upto) the first 6 letters of the user's lastname, and the digit '1'.

Usernames should obey the following rules:

To help enforce these rules, you may want to look at the string functions by running help(str) or looking at the list of functions in chapter 4.4 of the book.

The input parameters of this function are the first and last name of the user. The function should return the username. Test your function by writing a small main program that calls your function. Use at least three names, including at least one last name with less than six characters, and one name with punctuation.

A sample run is shown below:

   username("Andrew", "Danner") = adanner1
   username("Vanilla", "Ice") = vice1
   username("Eddie", "Van Halen") = evanhal1
   username("Brian", "St. Pierre") = bstpier1
   username("Kareem", "Abdul-Jabbar") = kabdulj1
   
Q2: Functions on lists
In the file listfunc.py, write a function calc that takes a number x as input and returns the value x*x/2.0. Next write a function calc_mod that takes a list of numbers as input and modifies each element z in the list to be z*z/2.0. This function should call the calc function one or more times. Write a small test program that shows that your function modifies the list. This function should not return any value. Finally, write a function calc_copy that takes a list as input and returns a new list containing the values z*z/2.0 for each element z in the original list. Write a small test program that shows the original list is unmodified. Save all your functions and testing code in the file listfunc.py

Q3: Functions on polygons
In this question, you will implement a set of functions that move a polygon. One obvious way to move a polygon is to use the polygon class method move, but we will explore how this function might be implemented "behind the scenes" by building our own version. Implement the functions described below in the file movepoly.py.

Write a function move_point that takes as input a Point object p, and two distances x and y and returns a new Point object p' that is identical to p but is moved a distance x away from p in the x-direction and a distance y away from p in the y-direction. Your function may use the getX and getY methods for points, but cannot use the move method.

Write a function move_polygon that takes a Polygon object P, and two distances x and y as input and returns a new Polygon object P' that has the same vertices as P but is moved a distance x and y in the x and y directions, respectively. Again, you cannot use the move method in the graphics class, but you can use the move_point function you just defined.

Write a small sample main() program that tests your functions. An example of a test program is shown below

hw4 example

Optional extensions

Optional Components
As noted above, these questions are NOT required to receive full credit.

Rotate Polygons

Write a function called rotate_poly that can rotate a polygon by a specified angle around a specified center of rotation. You may need to know the equations for rotating a point (x1,y1) by an angle t around a center point (x2, y2). The formula for the rotated point (x1',y1') is as follows:
dx1'=dx cos(t) - dy sin(t) 
dy1'=dx sin(t) + dy cos(t)
where 
dx= x1-x2, dx1'= x1'-x2
dy= y1-y2, dy1'= y1'-y2

The python math functions sin and cos expect the angle in radians. To convert from degrees to radians, use rad=deg*pi/180.

Some example output is shown below. The test file rottest.py in your homework/4 has a copy of the code shown below.

hw4 example 2