CPTR 124 Fundamentals of Programming Final Examination Practice Problems


  1. Write a function named nodups that accepts a list and returns True if the list contains no duplicate elements; otherwise the function returns False. The function should not modify the contents of the list.
    def nodups(lst): # Add your code here . . .
  2. Write a function named count_evens that accepts a list containing integers. The function returns the number of even numbers in the list. The function should not modify the contents of the list.
    def count_evens(lst): # Add your code here . . .
  3. Write a function named is_prime that accepts a single integer value. The function returns True is the number is a prime number; otherwise, it returns False.
    def is_prime(n): # Add your code here . . .
  4. Given the following class definitions:
    class Point: def __init__(self, x, y): self.x = x self.y = y class Rectangle: def __init__(self, left_top, right_bottom): """ left_top corresponds to the left-top point of the rectangle; right-bottom point of the rectangle. """ self.left_top = left_top self.right_bottom = right_bottom

    The following shows how a client can make a Rectangle object:

    box = Rectangle(Point(10, 10), Point(120, 180)) print(box.left_top.x) # Prints 10 print(box.right_bottom.y) # Prints 180

    Write a function named inside that accepts a Rectangle object and a Point object. The function returns True if the point is inside the bounds of the rectangle; otherwise, it returns False.

    def inside(rect, pt): # Add your code here . . .
  5. Given the following class definition:
    class Point: def __init__(self, x, y): self.x = x self.y = y
    write a function named distance that accepts two point objects. The function returns the distance between the two points.
    def distance(pt1, pt2): # Add your code here . . .
  6. Write a function named myzip that accepts two list objects. The function should return a new list containing 2-tuples, with the first element of the first tuple equal to the first element in the first list, and the second element of the first tuple equal to the first element in the second list; first element of the second tuple equal to the second element in the second list, and the second element of the second tuple equal to the second element in the second list, etc. The number of tuples created is limited by the length of the shortest list. For example:
    zip([1, 2, 3, 4], ['a', 'b', 'c', 'd', 'e', 'f])
    produces
    [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
    You may not use the built-in zip function.
    def myzip(lst1, lst2): # Add your code here . . .
  7. Write a function named all_there that accepts a string argument. The function returns True if the string contains all the letters of the alphabet A, B, C, ..., Z; otherwise, the function returns False. The capitalization of the letters does not matter. The function ignores nonalphabetic characters. For example:
    all_there('yw@uSq2onljhfdb7aCegikMp#rtVxZ')
    returns True, but
    all_there('yw@uq2oljhfdb7aCgikMp#rtVxZ')
    returns False (missing E and S).
    def all_there(s): # Add your code here . . .
  8. Write a function named listrange that accepts a list containing integers. The function returns the difference between the largest value and smallest value in the list. Do not use the built-in max and min functions. The function should not modify the contents of the list. For example:
    listrange([45, 2, 88, 100, 6])
    returns 98, since 100 - 2 = 98.
    def listrange(lst): # Add your code here . . .
  9. Write a function named common_factors that accepts two positive integer values. The function returns a list of the positive factors they share. For example:
    common_factors(12, 16)
    returns the list [1, 2, 4]. The function's behavior is unspecified if one or both of the arguments are less than 1.
    def common_factors(m, n): # Add your code here . . .