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 Jan 17th 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 Mon night due to the holiday. 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

These first 9 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

7. cave-blue

8. up-green

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.

Stop - problems below use decomposition techniques from lecture-3

Part-b

10. Top Blue Green

top-bluegreen

This problem deals with the double-move issue of how to do 2 moves within a loop. This problem does not require decomposition, but all the problems below do.

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

12. Green Rooms

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 the door square, and end 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

13. Checkerboard

Checkerboard

This problem is an algorithmic classic. 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() snd 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 should understand the Fill example from lecture-3 which solves some of the same issues. Hopefully you enjoy 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 up with good Python coding style as explained in: Python Style 1

> Turn In HW1

And you're done - welcome to Python coding!