Lecture Materials

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: Will lecture slides be posted on the website?

A1:  Yes! I will post them after class today!


Q: Is there an undo in Pycharm?

A1:  cmd+z


Q: Can you please post the lecture notes so we can annotate it during class?

A1:  They will be posted after class. Thanks for asking!


Q: When do office hrs/laIR start and where’s the schedule for that?

A1:  Office hours with Chris, Mehran, and me started this week. LaIR starts on suday. There will be an anouncment about LaIR on the website.


Q: are the times to sign up for section in PST?

A1:  yes!


Q: Is there an “and” or an “or” type of function we can use in Assn 1? I keep finding myself wanting to check two conditions or see if either is true in my programs

A1:  yes you can use ‘and’ or ‘or’ to check multiple conditions.


Q: Have the lecture slides for today been posted on the website?

A1:  Not yet! they will be posted after class.


Q: how do I run the Karel program once I finish the code?

A1:  Check out the ‘using karel in pycharm’ handout! But you can type ‘py CollectNewspaperKarel.py’ (python3 instead of py on a mac).

A2:  See the handout on Using Karel in PyCharm (on the class website under the Handouts menu)


Q: how many prefrences for section should we put?

A1:  All of the times that you can make!


Q: How long are these section meetings?

A1:  50 minutes


Q: How do you put Karel into different worlds when running code in pycharm. I could only get karel to run in all of the default worlds for assignment 1

A1:  Click the "Load world" button and it will let you pick another world file to load.


Q: Is there a handout to learn how to create our own Karel world to test our code in? I tried to create a new .w file but it doesn’t recognize it.

A1:  You can run a program in the Assignment 1 project called "WorldBuilder.py" which lets you build world files through a graphical interface.


Q: What is python generally used for in application? Is it different than other coding languages (in terms of application)?

A1:  There are lots of things that Python gets used for. Python has similarities and difference with some other languages, but we won't really get into that in CS106A.


Q: Are we allowed to use "and" or "or" for Assn 1? I have some conditions I'd like to check simultaneously

A1:  Yes

A2:  Yes. They are in the Karel course reader, so you can use them in Assignment 1. But it is also possible to do the assignment without needing to use them.


Q: How do we submit our pset?

A1:  Check out the “submitting assignments” handout on the website!


Q: how do the pycharm files save?

A1:  use cmd+s or file -> save all


Q: where is the "load world" button in pycharm

A1:  It's not in PyCharm. You'll see if when you run any Karel program. It's in the Karel program window.


Q: For the Assignment, should I make comments throughout my code or simply when defining functions?

A1:  Every function should have a comment. But you can add additional comments throughout as well if you want.


Q: does there need to be a particular sequence in which the functions are written?

A1:  Not really. But "main" should generally either be the first or last function in your code.


Q: Are we allowed to use ‘and’ or ‘or’ for Assignment 1 or is that not allowed yet?

A1:  Yes !


Q: Should the functions all be written in a pyramid type form with the smallest functions at the bottom, or is the only important thing that the main function is first?

A1:  There's no specific ordering. Often functions get written in the order that they get used, but they don't have to.


Q: Does main() need to be called to run the program?

A1:  That's how we've set up Karel and other stuff we'll be doing in Python. But it's not a strict requirement of Python. We'll talk about it more later on in the class.


Q: I typed “python3 CollectNewspaperKarel.py” in the terminal (I have a Mac) and it says “couldn’t recognize data in image file ‘./karel/icon.png’”

A1:  It is likely that you have a homebrew version of python. In your terminal (not in pycharm) can you run “brew uninstall python3” email jwoodrow@stanford.edu if you have a question about this.


Q: if we do an if loop without the else, what does it do if the condition is not matched?

A1:  An "if" statements doesn't do a loop. It just checks the condition, and if it's not true, then it does not do the statements in the "body" of the loop


Q: How do you loop an if statement?

A1:  you can put them inside the loop. for i in range(5): if condition: #cool code


Q: It doesn’t seem like Paperless is allowing us to submit PSETs yet

A1:  Not yet, since sections aren't set up yet. You'll be able to submit next week after you gert assigned to section.


Q: can we have mutiple elses within the same command?

A1:  You can use elif to do this!


Q: Are the Karel commands in Karel reader all the command we need to program "checkboard" and "midway" question in problem set 1?

A1:  Yes. That should be all you need.


Q: Should we delete all the comments already in the code, so we can inlcude our comments?

A1:  Yes! You can replace these with your comments.


Q: for assignment one, do we need to define the preconditions and post conditions for each looped function?

A1:  Please define these conditions for all functions.


Q: should assignment 1 be able to be run in multiple worlds?

A1:  For CollectNewspaperKarel, it only needs to run in one world. The other parts of the assignment need to run in multiple worlds.


Q: Where can we find the “WorldBuilder.py” file in Assignment 1? It doesn’t seem to be included.

A1:  Sorry, it's called "WorldEditor.py"


Q: could I loop an if statement until a condition is met?

A1:  That sounds like a good time to use a while loop!


Q: Should you try to avoid using "for" or " while" loop in the main function for the purpose of style?

A1:  It's fine to use them in main if it makes sense. But pay attention to your decomposition.


Q: How to turn off chats? Its very distractingly popping up

A1:  I am not sure if you can turn off chats :(


Q: Is there a way to end a while loop in the middle of it (like if a condition is met)?

A1:  There is a way to break out of loop in the middle. We'll talk about that when we are talking about Python (after we finish Karel). You shouldn't need to use it now.


Q: Is there a rule of thumb for breeaking down functions? I heard it’s good for a function to be less than 10 lines in addition to each doing different actions. Is that true?

A1:  There isn't a hard rule, but ~10 lines is a good heuristic to use in many cases. Chris will talk more about decomposition -- the idea is to have functions correspond to subproblems you are solving.


Q: Is there a way to connect a while loop or if loop to two conditions? So for example, while front_is_clear AND right_is_blocked?

A1:  yes! you can use ‘and’ to connect the two conditions


Q: For our diagnostics, do we get to code in a nice IDE like PyCharm where it is color coded & autocompleted? Or would it just be a blank document?

A1:  You will have a nice IDE but you will not be able to run your code.


Q: Why does the code “if __name__ == __main__” exist in every program?

A1:  We will talk about this more later in the quarter! It tells the program to call the main function first.


Q: Do we need to specify our milestones on the pset?

A1:  You are not required to do this.


Q: would you leave the milestone comments in your final code or should it be removed once you have achieved them?

A1:  You can remove them once you achieve them.


Q: do you have to put the space in the parantheses after function names?

A1:  No, there no space in the parentheses


Q: in chris' code the turn right function's comment includes the statement "works in any precondition state!". when i was coding assignment 1 and commenting my code this statement didn't cross my mind. do i need this in my comments to receive full credit?

A1:  You could just say precondition: none. You do generally want to be consistent with your comments.


Q: I’m having a hard time telling which direction Karel is facing and which side is right or left. Any advice?

A1:  When the Karel window is open (program is running), you can select the "Select Icon" menu and pick the "Simple" option, which will show Karel as a triangle to make it clear which way Karel is facing.


Q: is there a style preference or convention for the order in which new functions are defined? (here chris defines them in the reverse order that they appear)

A1:  There's no specific ordering. Often functions get written in the order that they get used, but they don't have to.


Q: if a bunch of code is repeated twice, should it be defined? (For example, code move() twice instead of defining it)

A1:  If you find yourself repeating code, that sounds like it might be a good candidate to decompose into a separate function.


Q: Would it be bad styling to not have a step_down() & step_up() function and just put those inside of while loop of descend_mountain() since descend suggests going down?

A1:  Having step_up and step_down is clearer to the human reader, so it's a bit better stylistically.


Q: Is it important for def step_down to be directly under def descend_mountain?

A1:  There's no specific ordering for functions. Often functions get written in the order that they get used, but they don't have to.


Q: where and how often should we add pre and post condition comments?

A1:  At the top of each of your functions using the Triple quotes. Just like Chris’s code on the screen!

A2:  Each function should have a comment. In Karel, we use pre/post conditions for the comments, generally.


Q: Do you usually write your comments at the same time you write your code, or do you go back and add comments when you're checking it over?

A1:  You should write comments when you are writing the code. It's a good habit to get into now.


Q: For step_down(), what does it mean when the pre condition matches the post condition?

A1:  Basically, after Karel has take a step down, it's in a configuration to be able to step down again. So we can just repeat step down to go down multiple steps.


Q: should one line comments go before a function or next to the function on the same line? what makes more sense or is more generally accepted?

A1:  It depends on the comment. A comment that is explaining a line in the program is usually explaining that line. A comment before a function is explaining what the function is doing overall.


Q: should we write the multi-line comment above the def statement for the function or below it like Chris is doing?

A1:  Below like Chris is doing!


Q: Should the comment at the very beginning of the code be more detailed than the assignment parameters? What are some good guidelines for writing that first comment?

A1:  That comment does not need to be too detailed. Just include a general overview of what is happening in the file.


Q: What would you do if there is no wall at the end?

A1:  You would need to change the code. The code here is written to match a particular specification for what mountain worlds will look.


Q: What if the mountain ended few ledges before the wall? then what would be the while condition?

A1:  If that were the case, the specification for mountain worlds would be different. You would change the code to mathc the specification for what mountain worlds might look like.


Q: Would there ever be a situation where the mountain would be more than one corner from the edge of the world?

A1:  Not as specified here. If you had worlds like that, the specification for what Karel should be doing would be different.


Q: Does this assume there is no free space at the bottom of the mountain besides the last space? Otherwise front_is_clear wouldn’t work?

A1:  Yes!


Q: Why does Karel not attempt to step up after she climbs the mountain? Since the front is blocked

A1:  At that point, Karel is past the portion that climbs the mountain, so it wouldn't try to execute that code again.


Q: When is it better to use multi-line comments vs. a single-line comment?

A1:  If you have many lines then use multiline. If the comment is one or two lines (generally) then single-line comments.


Q: In our main function should we only have other functions? Is it considered improper to have a while loop or for loop inside the main function?

A1:  It's fine to use loops in main if it makes sense. But pay attention to your decomposition.


Q: Is there a sort of checklist for writing pre and postconditions? Any info that we should always include?

A1:  You just want to be clear about what needs to be true about Karel for the function to work properly (i.e., what your function is assuming to be true in order to work correctly.)


Q: The collect newspaper Karel program doesn’t need to be generalizable, right?

A1:  Correct


Q: could the "i" in a for loop be any letter or is it always "i"?

A1:  It could be any letter, but we just use i. We'll talk more about that (in the context of Python) in a few days.


Q: How can you get out of an infinite loop if you accidently end up in one?

A1:  Just close the Karel program window. Karel will be okay :-).


Q: if i applied the ACE section, do I have to to the section sign-up?

A1:  Yes. ACE section is in additon to the regular CS106A section.


Q: do pre and post conditions need to always match? Or only for the if loops

A1:  The usually match if you are repeating something in a loop. They don't have to match if you don't need the pre/post conditions to match to have the program run correctly.


Q: Why not just up and down, Prof?

A1:  You could go up and down instead of side to side. There are any approaches. Chris just showed a few examples.


Q: So if we write a program that works for all worlds but there a few parts where there are different pre/post conditions (ex using algo 2), would that be wrong?

A1:  live answered


Q: Should I create a comment for each function? When should i create a comment describing the function?

A1:  Yes. Each function should have a separate comment.


Q: what does random() do?

A1:  Generates random numbers. We'll talk about it in a couple weeks.


Q: how long did it take you to write word search karel?

A1:  live answered


Q: Where in the program do you call main()?

A1:  called by computer!


Q: Ambout section, how do we sign up?

A1:  Sign ups are on the website! cs106a.stanford.edu. Check the announcement.


Q: sections start next week?

A1:  Yes!


Q: The times in the section signup are in PDT, yes?

A1:  Yes!


Q: is there any way to save the questions on this q&a for ourselves?

A1:  I post them on the website under the lecture tab for the given lecture.


Q: How do I know if I should continue taking this class or take 106B instead?

A1:  Reach out to the course staff. Try the first 106b assignment!

A2:  Check out the CS106B syllabus and see if you feel comfortable with the material.


Q: How do you add the colors?

A1:  paint_corner(COLOR_NAME)


Q: That program is great!! You wrote it as undergrad?!

A1:  yes!


Q: When will lecture slides/code get posted up?

A1:  After class today!


Q: When will we be able to submit Assignment 1 on Paperless?

A1:  Next week, after section assignments are made.


Q: So for the midpoint question, would you suggest to keep anything specific in mind?

A1:  That's what we want you to figure out.


Q: Could you explain pre and post condition again? Would you like us to put the pre and post condition in comments?

A1:  Yes put them in the comments please! Pre condition. is the state of the world before the function and post condition is the state of the world after.


Q: Do we need to generalize the program for Assignment 1?

A1:  CollectNewspaper only needs to work in the default world. The other 3 problems should generalize to the worlds described in the problem descriptions.


Q: does the front_is_blocked function only check for walls or if there is a beeper in front of karel does it also check for that

A1:  Only walls


Q: What if your code works on all worlds but it crashes at the end instead of finishing?

A1:  That's bad. Your program should end cleanly without crashing.


Q: for the CheckerboardKarel program in the 1*1 world, does he has to put a beeper? or not crashing is the solution?

A1:  live answered

A2:  If the first corner always has a beeper, 1*1 should have a beeper. In no cases should it crash.


Q: do we always start our code with def_main():

A1:  In this class, yes.


Q: How should we write a comment? Is there a general rule? Or should we just write a regular sentence in the way we want Karel to do

A1:  Check out Chris and Mehran’s code from lecture to get a feel for good comments!


Q: should we write the pre and post conditions everytime in the comments for every function?

A1:  Yes please!


Q: Do we put them in the comments for the main function or for the def functions or both?

A1:  both!


Q: for the newspaper assignment, the handout states that there should be a function for each of three parts, but one of the parts is “pick up newspaper,” which is just pick_beeper(). Is it necessary to create a function with only one command?

A1:  live answered

A2:  Yes go for it !!


Q: does karel always have infinite beepers?

A1:  No. It depends on the problem specification.


Q: When do we know that we have a beeper on the bag ?

A1:  live answered

A2:  You can check using if beepers_in_bag()


Q: When will the LaIR open this week?

A1:  Sunday at 5pm PT.


Q: Does the optional problem has credit for the + grade?

A1:  It is going above and beyond !!


Q: If I shop this class, how long will I have access to it?

A1:  live answered


Q: How can I build a world, for the extension? What should I do?

A1:  live answered

A2:  WorldEditor.py in the assignment 1 folder.


Q: Will we be able to go over our assignment with anyone before if we’re having trouble?

A1:  Come to LaIR! We will post about it on the website!


Q: Does taking 106A instead of B make me "behind" when comparing myself to my classmates?

A1:  NO !!


Q: Do we always have to ckec if Karel has a beeper before putting it?

A1:  Not always, but that is living dangerously.