Material to Advance from CICS 110 to CICS 160
Sample exercises students should be able to solve before taking CICS 160
Students who have enough prior programming experience to skip CICS 110 should be able to write solutions in Python to the programming tasks of the types listed below.
- Suppose we represent points in a two-dimensional plane as a tuple of two floats p=(px,py) where the first value in the tuple represents the horizontal position of the point and the second value represents the vertical position. Suppose also that we represent a circle with a tuple of three floats c=(cx, cy, r), where cx is the horizontal position of the center of the circle, cy is the vertical position of the center of the circle, and r is the radius of the circle. Write a function contains(c, p) that returns True if circle c contains point p, and returns False otherwise. As a reminder, a circle with its center at (cx, cy) and radius r contains a point at (px, py) if and only if (cx -px)2 + (cy - py)2 is less than or equal to r2.
- Write a function first_one_outside(circle, listOfPoints). The argument circle represents a circle in the same format as above. That is, circle is a tuple of three floating point numbers, representing the x coordinate, y coordinate, and radius of a circle, respectively. Argument listOfPoints is a list of points, each point represented as points are represented above. That is, each element of listOfPoints is itself a tuple with two integers, and these two integers are the x and y coordinates of a point, respectively. Function first_one_outside returns the index of the first point in the list listOfPoints that is outside of the circle circle. If no point in listOfPoints is outside the circle circle, first_one_outside returns -1. Assume listOfPoints contains at least one point. Function first_one_outside should make use of function contains, defined above.
- Write a function called found_elsewhere which takes three parameters. The first parameter, called lint, is a list of integers. The second and third parameters, called target and index, respectively, are both integers. found_elsewhere should return True if the list lint contains target anywhere other than at position index (even if lint[index] != target), and returns False otherwise.
- Write a function called mult_occurs that takes a list called arr as a parameter and returns a list of the numbers in arr that appear more than once. You must use the function described in the previous task, found_elsewhere.
- Suppose a business stores information for each of its customers in a tuple where the first element is a string representing the customer's last name, the second element is a string representing the customer's first name, the third element is a string representing the customer's phone number, and the fourth element is a float representing the total value of the customer's past purchases. An example might look like ("Smith", "Mary", "413-555-1234",107.95). We will refer to one such tuple as a "customer record." Use this information to solve the following tasks.
- Write a function total_purchases(customers) that takes a list of customer records as input and returns the total value of all purchases from all customers. Assume the list of customers is not empty.
- Write a function top_customers(total_value,customers) that takes the list of customer records and a float total_value and returns a list of customer records containing all customer records with a total value of past purchases greater than or equal to total_value.