Homework 5

Due Sunday 13-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. After you submit to Gradescope, make sure you check your score. If you aren’t sure how to do this, then ask a CA or Professor.
  3. There is no partial credit on Gradescope testcases for autograded problems. Your Gradescope score is your Gradescope score.
  4. Read the last bullet point again. Seriously, we won’t go back later and increase your Gradescope score for any reason. Even if you worked really hard and it was only a minor error…
  5. Do not hardcode the test cases in your solutions.
  6. 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.
  7. 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!

A Note About Testing

You will notice that the skeleton file only includes testcases for some of the functions you are writing. You should write testcases for the others. (You can find some nice ways to test in the write-up below, but you will need to translate those to actual testcases.)

Problems

  1. isRotation(s, t) [10 pts]
    Write the function isRotation(s, t) that takes two possibly-empty strings and returns True if one is a rotation of the other. Note that a string is not considered a rotation of itself.
    Hint: rotateStringLeft may be helpful here.

  2. bestStudentAndAvg(gradebook) [15 pts]
    Background: for this problem, a gradebook is a multiline string where each row contains a student's name (one word, all lowercase) followed by one or more comma-separated integer grades. A gradebook always contains at least one student, and each row always contains at least one grade. Gradebooks can also contain blank lines and lines starting with the "#" character, which should be ignored.

    With this in mind, write the function bestStudentAndAvg(gradebook), that takes a gradebook and finds the student with the best average (ignoring the case where there is a tie) and returns a string of that student's name followed by a colon (":") followed by his/her average (rounded to the nearest integer). For example, here is a test case:
    gradebook = """ # ignore blank lines and lines starting with #'s wilma,91,93 fred,80,85,90,95,100 betty,88 """ assert(bestStudentAndAvg(gradebook) == "wilma:92")
    Note: you most likely will want to use both s.split(",") and s.splitlines() in your solution.

  3. extractAssignStatements(text) [20 pts]
    Write the function extractAssignStatements(text) that extracts all valid assignment statements from string text. A valid assignment statement is of the form lhs=rhs. The left-hand side (lhs) of the statement must be a variable name. For simplicity, only alphabetical characters are allowed in the variable name (that is, no numbers or symbols). The right-hand side (rhs) must be a non-negative integer. Between both sides, only the character '=' or spaces can occur. The function should return a list of tuples, (lhs, rhs) where lhs is a string and rhs is an integer. Here are some examples:
    assert(extractAssignStatements("hello,class=15112 and anotherVar=42. The end") == [('class',15112), ('anotherVar',42)]) assert(extractAssignStatements(" .-, anotherVar=42.") == [('anotherVar',42)]) assert(extractAssignStatements("a=98,b=23232;c=543- --=- d=-42.23") == [('a',98), ('b', 23232),('c',543)])

  4. drawNiceRobot(canvas, width, height) [25 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 5! No extra files, no importing anything other than basic_graphics. Have fun!

  5. drawUnitedStatesFlag(canvas, 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. You can use circles instead of stars. If you use stars you get 5pts bonus.

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

    6. 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