CS 151: Intro to Programming in Python

Problem Set 2: Checkers

Due: Monday, 16 September, 11:59 PM

Github Classroom Link: https://classroom.github.com/a/MCr3kGDD


This is a problem set of three problems:

  • One last Karel problem
  • The first Python problem
  • One thinking problem
Each has template files within which you will complete your work.

Files:

1. Checkerboard

  • Update (only) this file:
    • Prob1.py
  • You will need to have these files in the same folder/directory:
    • karel.py
    • Prob1.w
    • 1x8.w
    • 5x3.w
    • 5x5.w
    • 8x1.w

2. divisible_by_six_or_seven

  • Update (only) this file:
    • Prob2.py
  • You will may also use:
    • test_ps2.py

3. Both Squares

  • Update (only) this file:
    • Prob3.txt

Problems:

Problem 1: Karel Checkerboard

We imagine Karel:

  1. Begins in the bottom left corner, facing toward the right.
  2. Wishes to place a beeper on half of squares.
  3. Wishes no beepers to be on squares with a beeper immediately above, below, left, or right.
    • We term this the "checkboard" pattern.

Make sure you your program works on the following grid sizes:

8x8 1x8 5x3 5x5 8x1

Karel may end anywhere on the grid but should not end with an error. There are always two acceptbale checkerboard patterns (bottom left has, or does not have, a beeper).

How I solved Problem 1

If/else, a special case of if, was covered minimally in class, but of great use here. Read more. My solution contained around 32 lines of Python (.py) excluding comments. I utilized four functions in addition to main() and three optional functions. I used one 'for', one 'while', and two 'if/else' statements.

I did not successfully complete the task on my first attempt, but solution worked when I added appropriate 'else' branches to my 'if/else' statements.

Problem 2: divisible_by_six_or_seven

Prob2.py contains a few lines under a comment named "boilerplate" that allow you test your code, but is otherwise mostly empty. Write your solution as specified above this "boilerplate" which you are encouraged to leave unaltered.

Write a function called divisible_by_six_or_seven, that

  • divisible_by_six_or_seven accepts two arguments: a smaller integer value that I will call x and a larger integer value, that I will call y.
  • divisible_by_six_or_seven prints every number...
    • from x to y, including x and y,
    • that is divisible by six (6) or seven (7), but
    • not divisble by both six (6) and seven (7).
  • divisible_by_six_or_seven may print these numbers in any order
  • divisible_by_six_or_seven, after printing all the numbers, will return (not print) how many numbers were printed.

I have written a small web application to help you. It allows you to input two values, and see the expected "print" and "return" results. Link.

How I solved Problem 2

I used one 'for' loop over a range created by the 'range()' function, similar to Karel 'for' loops. Within this loop, I used a series of 'if' statements, modulo '%' operations, and comparisons to zero using double equals '=='. Within the 'if' statements, I printed values and also increased the value of a counting variable, which I would latter return. There are many ways to structure these statements, and you should do what makes the most sense for you.

I was able to complete this task on my first attempt, but it took many tests before I convinced myself that it was correct. Also, I wrote the helper demonstration before attempting this task, and I took several attempts (at least 3) to create the code for demonstration.

Extra Credit

I have discovered a solution that requires a single line of code - that is, my Prob2.py takes only one line. I used a list comprehension and a lambda expression to achieve this.

Any student able to write, and explain, a single-line solution to Problem 2 will receive up one assignment worth of extra credit. The explanation is required because I believe generative AI programs may be able to solve this question quite easily, but I am doubtful you will be able to explain the code that they write. And if you can explain the code, then it doesn't matter that an AI wrote it.

If you would like to attempt this extra credit assignment, submit your single-line solution and schedule a time to meet with me to convince me you understand it.

Problem 3: Squares

You do not need to write code for Problem 3

Prob3.txt is a text (.txt) file and not a Python (.py) file. That is because Problem 3 is a writing problem: write out in prose, as if you are speaking to me, how you would solve the following problem.

Consider two squares.

  • One square of side length A that is greater than zero (0)
    A > 0
  • One square of side length B that is greater than zero (0)
    B > 0
  • They overlap by a distance d greater or equal to zero (0) and less than or equal to A
    0 ≤ d ≤ A
  • We imagine the square are lined up horizontally (left-to-right) on the bottom and overlap horizontally (left-to-right).

We ask you to find the total area covered by both squares, without counting overlapping area twice.

If d is equal to 0, then the total area is equal to A * A + B * B

If d is equal to A, then the total area is equal to the larger of A * A or B * B

I have written a small web application to help you. It allows you to input three values, and see the expected area. Link.

You do not need to write code for Problem 3

Hints

  • You just need the shaded area, so be careful you don’t “double count” the overlapping region.
  • You don’t know which square is larger, so you may need your algorithm to adjust depending on the inputs.
  • It might be useful to map out the major possible configurations to ensure your algorithm works for all of them.