Homework 5

Due Sunday 12-Feb, at 10:00pm


To start

  1. Create a folder named ‘hw5’
  2. Download hw5.py to that folder
  3. Inside that folder download the basic_graphics.py
  4. Edit hw5.py and modify the functions as required
  5. When you have completed and fully tested hw5, submit hw5.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. The starter hw5.py file includes test functions to help you test on your own before you submit to Gradescope. When you run your file, problems will be tested in order. If you wish to temporarily bypass specific tests (say, because you have not yet completed some functions), you can comment out individual test function calls at the bottom of your file in main(). However, be sure to uncomment and test everything together before you submit! Ask a CA if you need help with this.
  4. 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 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(canvas, x0, y0, x1, y1) [30 pts, manually graded]
    Write the function drawFlagOfTheEU(canvas, x0, y0, x1, y1) that takes a canvas to draw on, and four values -- x0, y0, x1, y1 -- that describe the rectangular region with left-top at (x0, y0) and right-bottom at (x1, y1). 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. Note: You can can use circles instead of stars.

  2. drawBarChart(canvas, w, h, L) [35 pts, manually graded]
    Write a program that draws a bar chart that shows the distribution of values in L, where L is a list of strings. For instance,
    L = ['bananas', 'oranges', 'oranges', 'bananas', 'apples', \ 'oranges', 'apples', 'pineapple', 'apples', 'apples' ]
    will produce the following bar chart:
    Example

    In this example, L has 10 elements: 4 apples, 2 bananas, 3 oranges, 1 pineapple. In other words, a 0.4 fraction of the items in L are apples, 0.2 bananas, 0.3 oranges, 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 L. 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. All bars should be next to each other, without gaps in between.
    6. 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.
    7. Do not worry about rounding when calculating the height.
    8. 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. drawChessBoard(canvas, width, height, board, color, margin) [35 pts, manually graded]
    Write a program that draws a chess board as follows:
    Example 1
    Here are some details on the parameters:
    1. board is a string of 64 characters. You may assume that it contains only spaces and characters (p,P,b,B,h,H,r,R,q,Q,k,K) representing the 6 pieces of Chess More information about Chess pieces.
    2. Lower-case letters in the string board represent white pieces, and upper-case letters black pieces.
    3. Each character of board indicates if a square is empty (space), or if it contains one piece. The squares are described row-wise, starting from the top row. The example chess board corresponds to the string:
      "RHBQKBHRPPPPPPPP pppppppprhbqkbhr"
    4. color is the an rgb tuple representing the color of the margin. "Lightening" color gives you the color of the non-white squares on the board (see below).
    5. margin is the distance between the grid and the edge of the screen.

    You must satisfy the following additional requirements to get full credit:
    1. You should be able to handle any width and height. The size of the board should adjust to fit the whole board on the screen.
    2. Your margin must be exactly what we specified. That is: your board must expand to fill up the full screen leaving only the margin given as colored space.
    3. Use Unicode characters to represent chess pieces More information about unicode characters
    4. Your font size must adjust with the size of the board. Pick a reasonable ratio between your square/cell size and your font size.
    5. Your board should have a checkerboard pattern, with the top left square always being white.
    6. Your non-white squares should be a "lightened" version of your color parameter. You can lighten an rgb color by adding a constant amount to the r, g, and b values (but make sure they're capped at 255!). See the rgbString function from the graphics notes to turn this tuple into a tkinter-compatible color. You may choose any reasonable constant to lighten by.