Homework 4

Due Tuesday 6-Feb, at 9:00pm


To start

  1. Create a folder named ‘hw4’
  2. Download hw4.py to that folder
  3. Inside that folder download the CMU Graphics.
  4. Edit hw4.py and modify the functions as required
  5. When you have completed and fully tested hw4, submit hw4.py to Gradescope. For this hw, you may submit up to 999 times, but only your last submission counts.

While you may submit to Gradescope as often as you like for this assignment, some questions are not autograded, so you will be responsible for testing your code and making sure it meets the problem requirements. As stated in the style guide, you do not have to write test cases for interactive, random, graphics, data initialization or event functions. Instead, you should test by visually inspecting your code’s behavior as you complete steps of each problem, where reasonably possible. This will make debugging your code much easier.

Some important notes

  1. This homework is solo. You may not collaborate or discuss it with anyone outside of the course, and your options for discussing with other students currently taking the course are limited. See the academic honesty policy for more details.
  2. Do not hardcode the test cases in your solutions.
  3. Remember the course’s academic integrity policy. Solving the homework yourself is your best preparation for exams and quizzes; cheating or short-cutting your learning process in order to improve your homework score will actually hurt your course grade long-term.

Limitations

Do not use lists, sets, dictionaries, try/except, classes, or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.

A Note About Style Grading

Like in the previous assignment, we will be grading your code based on whether it follows the 15-112 style guide. We may deduct up to 10 points from your overall grade for style errors. We highly recommend that you try to write clean code with good style all along, rather than fixing your style issues at the end. Good style helps you code faster and with fewer bugs. It is totally worth it. In any case, style grading already started, so please use good style from now on!

Problems

  1. drawFlagOfTheEU(x0, y0, width, height) [30 pts, manually graded]

    Write the function drawFlagOfTheEU(x0, y0, width, height) that takes four values -- x0, y0, width, height -- that describe the rectangular region with left-top at (x0, y0) and the given width and height. This function should draw the flag of the European Union so that it fills the given rectangular region. Here is that flag:

    image

    The flag should follow the specifications given here. You should also draw the label "European Union" a bit above the flag.

  2. drawBarChart(width, height, data) [35 pts, manually graded]
    Write a program that draws a bar chart that shows the distribution of values in data, where data represents a list of items encoded as a multi-line string where every line contains label and an integer, separated with a colon. For instance,
    data = """bananas:2 oranges:3 apples:4 pineapple:1"""
    will produce the following bar chart:
    Example

    In this example, data represents a list of 10 items: 2 bananas, 3 oranges, 4 apples, and 1 pineapple. In other words, a 0.2 fraction of the items in data are bananas, 0.3 oranges, 0.4 apples and 0.1 pineapple.

    Here are some details on the parameters of the chart.
    1. The chart must have 11, equally-spaced y-ticks on the y-axis with labels 0.0, 0.1, ..., 1.0
    2. Draw dashed lines from left to right as shown in the example, aligned with each y-tick.
    3. The chart must have n equally spaced x-ticks on the x-axis, where n is the number of unique values in data. Each tick should be labeled with the corresponding word in a vertical orientation.
    4. There should a reasonable separation between the tick labels and the axes.
    5. The chart should span as much window space as possible within the given dimensions.
    6. All bars should be next to each other, without gaps in between.
    7. The height of each bar should be proportional to the frequency of the item in the list: it should represent the fraction value in the plot.
    8. Do not worry about rounding when calculating the height.
    9. You can choose the colors however you would like, but you should make sure you can support at least 6 different colors and no adjacent bars should have the same color.

  3. drawYourFavouriteAnimal(width, height) [35 pts, manually graded]

    Write a function drawYourFavouriteAnimal(width, height) that (you guessed it!) draws an animal! This is not meant to be very difficult. We just want to see some really cool animal drawings while grading your homework. Your function must make a drawing using the cmu_graphics library that meets the following criteria:
    1. Easily identifiable as an animal
    2. Includes at least 10 shapes total, including at least one oval, one rectangle, one non-rectangular polygon, and one line
    3. Uses at least 4 colors
    4. Resizes with the canvas. (You may assume that the canvas will always resize proportionally, and you may change the starting proportions in the test case if you want to)
    Do not use anything we haven't learned in class or in the notes through Week 5! No extra files, no importing anything other than cmu_graphics. Have fun!