Homework 1 - Bit

HW 1 is made a range of Bit problems.

Part-a is the first set of problems on this page. Part-b is the second set of problems depending on lecture-3. The whole thing is due Wed Oct 2nd at 11:55pm. How to submit is explained in the part-b text.

Lair helper hours (linked off course page) begin for the quarter on Sun night. The experimental server saves your code each time you click the Run button.

Solve these problems using loops and logic like the lecture examples (not using Python features like for, range, int counting, break, and, or .. which we have not covered yet.)

Work on your code using one case at a time. As a last step, select the "Run All" option in the menu and Run to check your code against all the cases. If successful, the output is "All Correct" at the top with the big checkmark, which means your code produces the correct output. You should also check your code over for clean style (also covered Fri).

Part-a

The part-a problems are 1 function each - no extra decomposition.

1. top-rgb

2. top-bot

3. top-logic1

4. top-logic2

5. change-green

6. hidey-hole
Suggestion: One loop to move right, with its test stopping at the right spot. Then a second loop to go down. Strategy: a series of loops using their tests to stop in the right spot. This applies to problem 7 also.

7. cave-blue

8. up-green
Suggestion: write one while loop that gets to the top first. Then add if-logic inside the loop to detect and handle the the niches on the way. See the lecture "dip" example.

Before
alt: up green

After
alt: up green

9. tunnel

Before
alt: bit tunnel

After
alt: bit tunnel

The code for tunnel is a little repetitious. For part-b, we'll see a technique to tighten that up.

10. great escape

Great escape: Bit is stuck in a terrible maze. Use 5 loops to get bit to the exit at the upper left square like this: 1. Move bit forward until a solid black square appears to bit's left. 2. Turn right and then move forward until the first green square. 3. Turn right and move one square forward, into the 1-wide vertical hole. Move down the hole until an empty square appears to bit's right. 4. Turn right and then move until blocked. 5. Turn right and then move until blocked. Bit praises your coding genius from the upper left square!

Suggestion - this problem has, if we're being honest, every loop problem we could think of. The conditions for each of the 5 loops are given in the problem statement. Make a drawing to work out the geometry and logic for each of the 5 loops, getting bit to stop on the right square using each while-test. Here is a drawing based on on the Case-1 maze, showing how you might focus on the 5 stopping squares to work out the while-tests.

alt: for case-1, the 5 squares to stop on are circled to help work out the while tests

Stop Here - you are done with part-a of the homework. We will release part-b on Friday, and also instructions for turning in your code (which the experimental server saves on each click of the Run button). Part-b will have 4 problems, using techniques from Friday's lecture.

 

Part-b

These problems use the ideas from Friday lecture.

11. Top Blue Green

top-bluegreen

This problem deals with the double-move issue of how to do 2 moves within a loop. This is a single-function problem.

12. Forest and Trees

Forest

Starting with this problem, there will be one or two helper functions per problem. Recall the key theme of lecture-3 — call the helper function. There are radio-buttons at the top of the page on the experimental server that scroll to each function and adjust which case is tested, or you can do it manually. All the function defs should be at the leftmost indent in the text; not one function indented inside another. The line "bit = Bit(filename)" should only appear once in the whole program (the Fri lecture examples demonstrate this). The Python "pass" construct is a placeholder showing where you code goes, and it should be deleted.

For these problems, we are specifying the function pre/post conditions here in the handout instead of in the triple-quote """Pydoc""" in the code.

a. The helper function do_tree() draws a small tree made of 4 painted squares. The bottom of the tree is made of a green square, a red square, and a green square in a row. Then above the red square is one more green square. Before the function runs, bit is one square away and facing what will be the red square. The function draws the tree with a series of moves, turns, and paints, but no loops. It's ok if bit moves a little inefficiently, so long as the tree is drawn. At the end, leave bit on the original square, but facing the opposite direction from the beginning. Assume there is enough clear space to draw the tree.

Before do_tree()
alt:before tree

After do_tree()
alt:after tree

b. For the do_forrest() function, bit starts facing the right side of the world. Move bit straight forward until blocked. For each moved-to square, if the square is red, draw a tree just above the red square.

Before do_forest()
alt:before forest

After do_forest()
alt:after forest

13. Green Rooms

Green Rooms

a. For this problem, the do_room() helper function paints one room green. Each room is 1 square high and 1 or more squares wide. There is one empty square - a door - that is the opening into the room. Bit begins in the door square, facing into the room. Go into the room and paint all its squares green. Then return to occupy the door square, facing out of the room. It's ok if bit goes some extra distance, so long as all the painting is done and bit gets back to the door. Our solution uses 3 while loops.

alt:painting one room

b. Bit starts at the beginning of a 1-wide hall. Move bit forward to the end of the hall. For each moved to square, a door to a room may appear at the left or right, or both. Fill all the rooms with green. End with bit at end end of the hall, facing the original direction, like this:

alt:all rooms painted

14. Checkerboard

Checkerboard

This problem is an algorithmic classic. It's helpful to be familiar with the Fill example from lecture-3 which has some of the same issues as Checkerboard. For the Checkerboard problem, bit starts in the upper left corner, facing down. Move bit to the lower left corner, filling in all the rows with a checkerboard pattern, like this:

Before:
alt: blank checkerboard

After:
alt: checkerboard filled in

It's handy to have terms for the two row types — we'll say the topmost row is "type 0", since the blue squares begin right away, and the second row is "type 1".

Your code should be able to solve any size world that is 2x2 or larger, and the world may not be square. The double-move issue appears a couple times in the checkerboard, so keep your solution to the blue-green problem above in mind.

alt: 10x6 checkerboard filled in

Row Helper Functions

We will decompose out two helpers, solving each type of row — do_type0() and do_type1().

There is a cute coding trick that helps with the code for the type-0 and type-1 rows. It's quite non-obvious, so we're going to nudge you in the right direction: inside your do_type1() function, you can call the do_type0() function to do almost all the work. As a result, the do_type1() function will be short and will not itself contain any loops.

Get the helpers working first, before solving the whole checkerboard. Cases 1-6 in the menu test the helpers, testing them with both even and odd width rows.

The pre/post for the helpers are provided in the """Pydoc""" on each function.

def do_row0(bit):
    """
    Begin facing down in the row to fill.
    Move towards the right side of the world,
    making a type-0 row.
    End at the left side of the row, facing down.
    """
    pass


def do_row1(bit):
    """
    Begin facing down in the row to fill.
    Move towards the right side of the world,
    making a type-1 row.
    End at the left side of the row, facing down.
    """
    pass

Type-0 before/after:
alt: row0 before
alt: row0 after

Type-1 before/after:
alt: row1 before
alt: row1 after

Final do_checkerboard()

Now the main challenge — write code in do_checkerboard() to solve the whole thing. Bit begins in the upper left corner facing down, and ends in the lower left corner facing down.

You have earned a moment of satisfaction when it finally works.

All Done

Below is the link to turn in your code to "Paperless", the system where the section leaders review and grade your code. The Experimental server is storing your code each time you click the Run button, as you can tell if you close a tab and then re-open it. Use the page linked below to review and send your from the experimental server to paperless. Your code needs to have passed the Run All case, getting the big checkmark, and the code should be cleaned to follow proper PEP8 Python coding style as explained here: Python Style 1

> Turn In HW1

And you're done - welcome to Python coding!