CSSE 120 -- Intro. to Software Development

Homework 4

  1. Complete the assigned reading for the next session, Zelle sections 4.4-4.7.
  2. Complete the Angel quiz over this reading. You'll find this on the course Angel page, under Lessons → Homework → Homework 4 → String Manipulation and File Processing
  3. Using range. Write a Python range expression for each of the following lists. You should write your expressions in a new file, rangeAndList.py, in IDLE, using print statements to print the answers. Answers to the first two are given as examples.
    1. [0, 1, 2, 3, 4, 5]
      Answer: print "3a.", range(6)
    2. [2, 3, 4, 5, 6, 7]
      Answer: print "3b.", range(2, 8)
    3. [-2, -1, 0, 1, 2]
    4. [1, 3, 5, 7, 9]
    5. [1, 4]
    6. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    7. [2, 1, 0, -1, -2]
  4. Manipulating Lists. Continuing with the file your created for the previous problem, add the following line of code:

    myList = [3, 6, 8, 11, 7, 4, 5, 9, 10, 0, 2]

    Now add statements to your program to print each of the following numbers or lists using expressions involving myList. Again, a couple of examples are given.

    1. 3
      Answer: print "4a.", myList[0]
    2. [3, 6, 8]
      Answer: print "4b.", myList[:3]
    3. [8, 11, 7, 4]
    4. 2
    5. [0, 2]
    6. The position of 4 in myList.
    7. The length of myList.

    Upload the file containing your answers to problems 3 and 4 to the RangeAndList dropbox in the Homework 4 folder on ANGEL.

  5. Some Geometry with Lists. For this problem you'll write three small, separate, but similar graphics programs: Pizza, Polygon, and Star.

    Each program prompts the user to enter an odd integer, number. Then the programs draw pictures as follows:

    The pictures below show what the display of your programs should look like for a couple of cases.

    Examples when number is 5:

    Examples when number is 7:

    Copy and paste liberally during this assignment, but get Pizza right before moving on to Polygon and Star.

    You may wish to use the following code, which generates an appropriate list of Points for any value of number:

        # Some constants
        radius = 150
        centerX = 200
        centerY = 200
        
        # Builds vertices list
        vertices = []
        for i in range(number):
            x = radius * cos(2 * pi * i / number) + centerX
            y = radius * sin(2 * pi * i / number) + centerY
            vertices.append(Point(x, y))
    

    Do you see how you can iterate over the Points stored in the list vertices in order to find endpoints for the lines?

    When you have finished and tested your programs, submit each Python source file to the corresponding drop box in the Homework 4 folder on ANGEL.

  6. Bonus. Can you figure out how to replace the vertices-building code that we gave you with a list comprehension instead? [If you submit solutions for #5 that use list comprehensions, you will receive bonus points]
  7. The Python Package Index, a repository of extensions to Python, is often called the Cheese Shop. Watch this bizarre video to see where the reference comes from.

If you need help, recall that in addition to your instructor, the student assistant lab hours are a resource for you. The times are listed in the Course Syllabus.