Github Classroom Link: https://classroom.github.com/a/MCr3kGDD
This is a problem set of three problems:
divisible_by_six_or_seven
We imagine Karel:
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).
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.
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...
x
to y
, including x
and y
,
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.
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.
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.
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.
A
that is greater than zero (0)A > 0
B
that is greater than zero (0)B > 0
d
greater or equal to zero (0) and less than or equal to A
0 ≤ d ≤ A
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