Lecture Materials

Note: Today's Karel problem can be found here.

Class Announcements

  1. Section sign ups: The section signup form opened yesterday, and will close this Sunday at 5pm PDT.
  2. Early assignment start: Even though assignment 1 isn't due until Friday, get an early start - some of the problems are tricky!

Learning Goals

The goal of today's class is for you to feel comfortable with the programming strategy called "decomposition" whereby you take a large problem and break it down into smaller pieces (which become their own function). When approaching programming challenges experts often alternate between setting milestones and achieving those milestones via decomposition.

Questions & Answers

Q: I saw in handout #4 that you test new worlds by clicking the Load World button, but I cannot find it? Do you know where it is located in Pycharm?

A1:  It’s in the Karel Window - right below Chris’ cursor right now!

A2:  Load World button is in the Karel window (when it's running).

Q: do if if statements exist and can we use them in the pset

A1:  Yes. if statements are part of python/Karel

Q: is it possible to put the lecture slides on the website so that we can follow along please?

A1:  They are being uploaded right now. They just take some time to upload. Apologies for the delay.

Q: Is there a way to do While - Else ?

A1:  What would you expect it to do?

Q: Is the karel review sheet available on the website?

A1:  See Chapter 10 of the Karel Reader

Q: In the assignment sheet it mentions that we can make our own worlds to test programs on— how do we do that?

A1:  You would run the WorldEditor.py program that is included in the Assignment 1 project.

Q: are we allowed to use “and” for assignment 1?

A1:  But you can use it if you want

A2:  You shouldn’t need to

Q: are we allowed to use recursion on assignment 1?

A1:  If you are comfrotable with it and use it correctly, but it's not required. We won't talk about recursion in class.

Q: Can Karel go from East to West facing on the same block?

A1:  You could do 2 turn_lefts to have Karel turn around

Q: But he'll upside down

A1:  Then you just have Karel turn around again. Sound like you might want to write a function like move_backward() that might do all the steps needed to accomplish this :-).

Q: I'm wondering if he can turn west while having head up

A1:  Karel can only move the direction that Karel is facing

Q: what is API?

A1:  Think of it as the list of things you can ask Karel to do (e.g. moving and turning)

Q: for stylisitic purposes, when should you use “”” vs # when describing what a function does

A1:  A # is a single line comment and “”” is for multiline comments!

A2:  For this class, you can use either. But usually programmers use """ comments for multi-line comments at the start of each function and # comments for single line comments in the middle of code.

Q: When we write comments in our assignments, should they be tabbed inside of the defs? Or should they be placed in between defs? Thank you!

A1:  (Hopefully the indentation works here):

def function():
 this comment explains a function
 “”” move()

Q: Would the command “morning” be the body of the main function?

A1:  There are a ton of ways to decompose this! If the program covers the entire day, morning() is a reasonable function to go in main()

Q: can we use a variable that counts ?

A1:  We can’t use variables in Karel :)

Q: Why do you have to go through a neighbor pile and not go straight to the end step?

A1:  We’ll get there in a sec but think about how the program would end if we didn’t have a temporary pile!

A2:  You need to have a separate pile the represents the doubled pile. Karel can't directly determine the number of beepers in a pile.

Q: So Karal does not know which number of beepers right?

A1:  Correct. Karel can only tell if there is a beeper on a corner or not (not how many beepers there are).

Q: Do we assume that Karel already has enough beepers to have double?

A1:  Yes. We assume Karel has infinite beepers in the beeper bag to begin with.

Q: Does Karel have an infinite storage of beepers? (i.e. Where does Karel’s 2 beepers come from if she only picked up 1)

A1:  Karel has infinite beepers in this problem

Q: Should Karel be concerned about removing the existing beepers before putting in the new ones?

A1:  That's why the new beepers are put in a separate pile than the original pile we want to double.

Q: how can I follow along on the website? because the link doesnt work

A1:  Which link?

Q: The one chris sent in the chat to get to this website hes on right now

A1:  It’s on the lecture page for today

Q: Does Karel have to make double pile next door? Or can Karel pick up and put 2 back down on the same corner?

A1:  Think about how the program would end if you did it like that!

A2:  If Karel put down 2 on the same pile, Karel would never stop adding beepers to the pile since the pile would just keep getting bigger.

Q: do we assume karel starts with infinite beepers?

A1:  Yes

A2:  Yep!

Q: Under def main(), after double_pile, should we have a return_to_square_one?

A1:  live answered

Q: should you always have # notes and where

A1:  You should have a commnet for each function to describe what it does. Additionally, having comments in the middle of a function can be useful to describe complicated things you are trying to do.

A2:  live answered

Q: Does style when grading the homework also account for how concise the code is? Is it good to optimize it to as few lines as possible? Or does it matter?

A1:  For now, please optimize for code that’s readable rather than just minimizing the number of lines of code you’re using (often, those goals will align)

A2:  For CS106A we care more about code readability than optimization. You shouldn't try to optimize code if it sacrifices readbility.

Q: can we use a modified version of make_double_pile_nextdoor by moving backwards instead of moving forward and putting one beeper instead of two?

A1:  Can you elaborate on what you mean?

Q: Is there a reason why we can’t pick up all of the beepers at once?

A1:  You don’t know how many beepers you have if you picked them all up!

Q: should the pre be “pile in the back of Karel”?

A1:  For which function?

Q: why can’t you pick up all the beepers at once?

A1:  You don’t know how many beepers you have if you picked them all up!

Q: You don’t need to deliver each beeper individually, though, right? You can pick up a beeper for as long as beepers are present, and then move back and put_beeper for as long as beepers_in_bag, which is a basic karel condition.

A1:  Karel has infinite beepers, so you’d be putting beepers down for a long time :)

Q: Do we have to make the shortest programs possible, or is it okay as long as it works well ?

A1:  For CS106A we care more about code readability than optimization. You shouldn't try to optimize code if it sacrifices readbility. Longer code is just fine if it's more readable.

Q: ^ Ah, were we ever told that Karel has infinite beepers in this problem?

A1:  In general, Karel has infinite beepers unless we say otherwise!

Q: Why can’t you pick up all the beepers at once?

A1:  You don’t know how many beepers you have if you picked them all up!

A2:  live answered

Q: move nextdoor pile back

A1:  Nope, because Karel starts off behind the pile, so it’s in front of Karel

Q: can you explain the pre and post conditions again for this problem

A1:  Karel starts and stops at (1, 1) and facing East. (2, 1) has some number of beepers to start off, and has double that number of beepers at the end!

Q: what is the difference between adding an additional move_backwards() under the def main as opposed to adding two move backwards commands under the def make double pile next door?

A1:  Both are reasonable

Q: I don't know if it just my screen, but there is a black rectangular object blocking the slides.

A1:  That seems to be showing up on other screens too. Seems to be a Zoom issue.

Q: Chris, is it alright to write out def_main() first w/ high-level steps and comment out the stuff you're not ready for yet (before you confirm milestones)?

A1:  Yes! That’s what we do basically all the time when we’re programming :) It makes testing much easier

Q: Am i able to screenshare with you during OH after this….. something is wrong with my pycharm

A1:  Sure

Q: if I accidentally run an infinite loop, will my computer crash? my computer is really old haha

A1:  It might get a little slower if it runs for a while, but you can still quit the Karel program!

Q: is left of karel its west or our left?

A1:  90 degrees counterclockwise of the direction Karel is facing in

Q: Why move beepers from doubled pile back to original position one at a time? Could this work? #After the pile is doubled while beeper_present(): pick_beeper() move_backwards() while beepers_in_bag(): put_beeper()

A1:  Karel has infinite beepers, so you’d be putting them down for a while

Q: Is anyone else struggling to see the right hand side of the screen?

A1:  Yes, this seems to be a problem with how Zoom is refreshing (or not) the screen in the broadcast.

Q: does 3 only work for even columns?

A1:  It could be adjusted in some ways for odd worlds, but I still wouldn’t recommend it!

Q: If I am in ACE do I attend normal section too?

A1:  Yes. ACE is in addition to the regular section.

A2:  Yes

Q: how do you comment multiple lines at once

A1:  Highlight them and type command/ctrl + / !

Q: How do you hash multiple lines of code at once?

A1:  Highlight them and type command/ctrl + / !

Q: why did you choose to write the definitions of place_cardinal, place_chris, etc. after the definition of solve_puzzle and not under the original def c reate_puzzle?

A1:  Great question! I like to order my functions from biggest picture to more specific

Q: For some reason, the karel program won’t run my code… It’s having a hard time loading? Does anyone know a solution to this?

A1:  live answered

Q: Is it acceptable to do pattern 4 if you want to get practice with doing the least number of moves?

A1:  Sure, feel free to try that for practice.

Q: so if we want an if statement with 2 conditions, would we make it with “and” or an if statement within another if statement

A1:  live answered

Q: What do you exactly mean by matching the pre and post statements?

A1:  live answered