Homework 4

Due Sunday 19-Sep, at 10: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. drawUnitedStatesFlag(width=950, height=500) [30 pts, manually graded]

    Write the function drawUnitedStatesFlag which draws the US flag in the provided dimensions. You can assume that the height:width ratio will be 10:19, as is the case with the actual US flag.

    You can find much useful information about the flag’s dimensions on Wikipedia:
    https://en.wikipedia.org/wiki/Flag_of_the_United_States, but we do not expect you to match the actual US flag design perfectly; you should instead seek to create a reasonable approximation of the flag. However, your flag must meet the following requirements:

    1. The flag should start from the upper left-hand corner of the window.

    2. The flag should have the correct number of stripes, alternating red and white in the correct order.

    3. The blue field in the upper left corner should cover exactly seven stripes and have a reasonably correct width.

    4. The flag should have the correct number of stars in the correct configuration, with the star size and spacing reasonably close to the actual flag.

    5. The colors should be the correct shades of red and blue. (Check Wikipedia)

    For an example of what reasonable flags might look like, here is an example:

    image


  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. drawNiceRobot(width, height) [35 pts, manually graded]

    Write a function drawNiceRobot(canvas, width, height) that (you guessed it!) draws a nice robot! This is not meant to be very difficult. We just want to see some really cool robots while grading your homework. Your function must make a drawing using the 112 graphics library that meets the following criteria:
    1. Easily identifiable as a robot
    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 4! No extra files, no importing anything other than cmu_graphics, string, math. Have fun!