Lecture Materials

Class Announcements

We've posted information about the diagnostic and some practice material on this handout. Please make sure to have downloaded and installed BlueBook, our exam platform.

Questions & Answers


Q: Yay! Chris is back!! Surgery all good?!

A1:  Had as much of a good time as I could — and the doctors say it did what it was supposed to :-)


Q: what file do we put in bluebook?

A1:  For the practice diagnostic, you can download the practice diagnostic bluebook file from the diagnostic handout. Then you can open this file in bluebook.


Q: Can your run your code in the BlueBook?

A1:  No


Q: For the exam, we just log into buebook at 1 pm and the test will show up?

A1:  You will download the exam file from the website before you take the exam and then at 1pm we will post the exam password.


Q: Is there anything Chris hasn’t built?

A1:  Haha there are infinite things :-)


Q: How do we run our code in bluebook? Or is that not necessary?

A1:  Not necessary — we grade it as if you werent running your code and dont take off points for typos and such!


Q: Can you say “not in” for list if statements?

A1:  Yes! Here is an example: >>> x = [2, 3, 4] >>> 3 in x True >>> 3 not in x False


Q: if we want to modify a list in a sub-function do we have to say return list after that?

A1:  Yes if you *reassign* meaning you change the list using an equal sign. If you just change one value, the change will persist regardless of whether or not you return it…


Q: Where is the practice diagnostic located?

A1:  http://web.stanford.edu/class/cs106a/handouts/diagnostic-1.html


Q: elem and list[i] both refer to the value of elements, but list[i] allows us to loop through elements based on index while using elem just loops through each elem value, correct?

A1:  correct! But if you are modifying elem.. you need the range version


Q: What happens to the elem if it’s immutable and you change them? Would you have to create another list to put those elements?

A1:  another list is a good option!


Q: So mutable means the list changes permenantly throughout the program and immutable does not change the list?

A1:  When you pass a mutable object it can be changed in the parent function. When you pass an immutable object, you cannot change the value in the parent function without returning and storing a new value.


Q: I still don’t understand when we can use for-each loops – could we use it in the removeduplicates function or not?

A1:  If you are “removing” elements — your best option is to make a brand new list. Removing while looping (regardless of which version) is always dangerous


Q: is there an answer key for the diagnostic?

A1:  Yes! See http://web.stanford.edu/class/cs106a/handouts/diagnostic-1.html


Q: For the diagnostic, so essentially, we should trace our code to see if it would work hypothetically?

A1:  Yes!


Q: how can you compare an element in your list to all the other elements?

A1:  You have to do so one by one. You can loop over a list, and then for each element, loop over the list again and compare it to all other elements. Try it out !


Q: could you do a list.append(elem) and a for each loop and have that affect a list?

A1:  list.append will affect the list regardless of which loop type you use. Good thinking.


Q: What is “True”? And when should we use it in a while loop? How does it work?

A1:  Its a value of type “boolean”. while True: will repeat forever


Q: if we use a for loop in range(len(list)) but remove items from the list in the for loop, that will always cause an error because the list is smaller?

A1:  It may not always cause an error, but it is likely not doing what you think it is doing. It is good practice to not remove items from the list you are looping over.


Q: Will today’s lesson be in the diagnostic?

A1:  No. The diagnostic covers everything up to and including the Friday (10/2) lecture.


Q: Thank you, is there a difference between value and elem?

A1:  just different names!


Q: does EXTRA_CREDIT need to be a parameter? Since its a constant isnt it universal?

A1:  We pass it as a parameter in case we want to change the value later. Perhaps there is one value for EXTRA_CREDIT but a different value for CURVE or something like that.


Q: so does that mean that we dont need to return the list?

A1:  In the example, the list scores was changed without returning it.


Q: Can you also use a for-each loop for give_extra_credit()

A1:  no! Because you are modifying score_list[i]. For each loops are “read only” so changes to individual elements do not persist


Q: What happens if you try to pass in an argument that doesn’t match the parameter object type? I.e. what happens if you try to pass an int into score_list?

A1:  You can pass an argument that is the wront type without an error, but if you try to do an operation on that parameter and you are not able to do the operation on that type, you will get an error. Try it out!


Q: So you don’t have to return the output from the extra credit function?

A1:  No because scores was changed inside the function.


Q: when will we get our scores for the diagnostic? will we have to do an IG?

A1:  You will not have an IG for the diagnostic. Scores will come out around a week later.


Q: Thank you! So, we don't need to use return if we are making mutable changes within a list. However, if we create a new list from another list that is passed to a helper function, we need to return that new list to the main function if we want to do something with it further (e.g. print it). Is that correct?

A1:  Yes!!!! That is an important distinction to keep in mind and Im glad you confirmed.


Q: why dont we have to return the new avg list?

A1:  for lists, you only need to return if you “reassign” the list ie if you had written score_list = []. If you change individual elements the impact will persist without you changing it


Q: To confirm, the file for the Wednesday diagnostic will be posted sometime today or tomorrow. We need to download the file before class Wednesday. Then we receive the password to open the exam by attending lecture Wednesday at 1pm where it will be released live?

A1:  Yes! Except we will likely post the password as an announcment rather than having you attend a zoom lecture.


Q: Okay, awesome. Thank you for all the help, Chris!

A1:  with pleasure!


Q: Are the RGB values of a pixel stored in a list? Like how do you access/manipulate the RGB values?

A1:  They are stored similarly to a list. Mehran will show you how to access/manipulate the RGB values.


Q: In the remove duplicates program, is it better to take the num list and eliminate the duplicates or build up the list by only appending numbers that do not duplicate from num lists? Basically which strategy is best for approaching this problem: breaking down the old list or building up a new list w/out duplicates? Thanks!

A1:  It is generally better practice to build up a new list.


Q: On the RGB scale, is 255 white or black?

A1:  255,255,255 is white!

A2:  White.


Q: I just downloaded Pillow on mac, and it says im using version 19.2.3 while 20.2.3 is available - should I update?

A1:  update is always fine — either is ok for cs106a


Q: does importing functions or libraries make our program slower? Is there a downside to importing the whole library instead of just a function?

A1:  You do not need to worry about this in 106A, but yes importing an entire library creates more overhead for your program.


Q: Do the practice diagnostic problems also reflect the difficulty of problems we can expect for Wednesday?

A1:  Yes.


Q: Can we do that pillow install command in any directory? (im currently working in assigment 3)

A1:  yes!


Q: what’s the difference between import and install?

A1:  Install downloads the library to your computer. Import allows you to tell the program that you are going to be using the library in the code in this file.


Q: will images be on the diagnostic?

A1:  No.


Q: For the diagnostic, do we need to put comments?

A1:  No. But you can put them if that helps you code.


Q: Do we go to lecture on Wednesday at 1 or do we just start directly with the diagnostic in Bluebook?

A1:  You download the exam file from the website. Once the exam time starts, we will post an announcment with the password to start the exam. You do not need to come to lecture on Wednesday.


Q: Isn’t there more complicated information (like header bits etc) in images like .jpg? Why do we need not to worry about them?

A1:  The SimpleImage library extracts that away from you.


Q: I tried to run the practice diagnostic in Bluebook yesterday, but it kept saying invalid file type?

A1:  Did you download the bluebook version of the practice diagnostic?


Q: Will using break statements be allowed in the diagnostic?

A1:  Yes.


Q: So a for-each loop can change elements in an image? Cause I thought they weren't usually used for changing elements in a collection such as in a list

A1:  good question. The key distinction is that Mehran isn’t reassigning the value. Instead he is mutating the value. Anytime you see the dot (“.”) in a statement you are mutating!


Q: if we use for example a for i in range(width_of_image), should we add 1 to width_of_image to get the last line of pixel or will width_of_image get the last line also?

A1:  the image is also “zero indexed” meaning the range does exactly what you want without the +1 (ie there is a pixel at row = 0, but no pixel at row = width_of_image)


Q: Where can I find the bluebook version of the practice diagnostic to download it?

A1:  Here: http://web.stanford.edu/class/cs106a/handouts/diagnostic-1.html


Q: what does the // 2 mean?

A1:  That is integer division. So it takes the floor of a/b.


Q: What would happen if the transformation to the RGB value resulted in a value outside the range?

A1:  SimpleImage is your friend and clips it to range


Q: why don’t you need to return the flower object in darker?

A1:  Because image is a. mutable object passed into the darker function.


Q: How does def darker know what “image” is? Like where is “image” defined?

A1:  To load an image, you can do image=SimpleImage(“name_of_file.jpg”). Mehran did that on an earlier slide.


Q: arent these universal variables because they are before the main function?

A1:  Mehran did not use any global variables. Images still have to be passed as parameters.


Q: Do you not return things for lists/images/mutable objects in general?

A1:  You can return! On the example slide, Mehran makes a new image and returns it.


Q: in the darker example we used a for each loop to make the image darker. I thought we couldn't use this loop for persistant changes?

A1:  Its a great question. The key difference is that “dot”. The dot means, im not “reassigning” I am “modifying”. Its the reassignment which has differences in impact


Q: Will this work for tiff files as well, or just jpg, png, and gif files?

A1:  I havent tried with tiff… Im curious myself too


Q: what is the difference between using “for i in range” vs “for item in collection” when creating loops with images

A1:  For i in range gives you the index in the list whereas for item in collection you do not have the access to the index in the list.


Q: Why does he return it in the red channel example?

A1:  Mehran made a new image, so he had to return it!


Q: why did we use integer division rather than just normal division?

A1:  RGB values must be integers.


Q: When do you return or not return an image?

A1:  You return when you make a new image!


Q: So you would not have to return if you were not creating a new image and just editing the original?

A1:  Yes if you are only editing the image passed as a parameter, you would not need to return that.


Q: So the pixels are mutable unless is a new image?

A1:  if you “reassign” a variable, that change never persists unless you explicitly return it (ei image = SimpleImage)


Q: Why does it have to be green?

A1:  That version of green is very uncommon in nature!


Q: What’s the INTENSITY_THRESHOLD?

A1:  Its to controll how much green you are looking for. It doesn’t have to be perfectly green to get replaced


Q: So set_pixel() is a method in pillow?

A1:  its a method for SimpleImage which is a wrapper around pillow


Q: so image.set_pixel allows you to merge two images on certain x,y points

A1:  its a more general tool. It can be used to merge two images.. but more problems too.

A2:  Yes! You can take the pixel from one image and place it at the same x,y location in a different image.


Q: Is the intensity threshold for greenscreens always 1.6?

A1:  No! Thats why a constant is so useful


Q: How do newer computers create green screen effect without actual green screen

A1:  Different intensity thresholds / perhaps a more complicated algorithm.


Q: how do we establish the intensity threshold?

A1:  You can try out different values!


Q: How to manipulate camera image to adjust blurriness like Mehran?

A1:  Good question! http://eeweb.poly.edu/~yao/EE3414/image_filtering.pdf :-)


Q: Can you explain in a different way mutable vs. immutable objects?

A1:  Yes! Imagine a google doc. When you share a google doc, it can be editable by your friend. Mutable is just like that :-).


Q: whats the difference between using nested for loops and a for each loop to iterate thoruhg pixels?

A1:  Nested for loops allow you to have access to the (x,y) locations of the pixels. A for each loop can be used when you don’t need the location.


Q: does it matter the order if we put the y loop first or the x loop first?

A1:  It is convention to do y in range(height) as the outer loop.


Q: When would it be beneficial to use a nested for-loop instead of a for-each loop?

A1:  When you need the x,y coordinates it is helpful to use a nested for loop. If you don’t need the location, you can use a for-each loop


Q: do x and y need to be defined or does the computer associate x as width values and y as height values?

A1:  You have to define height and width and y and x take on values from 0 to height and width respectively.


Q: Can’t you just traverse the burrito image using a for-each loop?

A1:  Mehran uses the nested loops so that he has access to the x,y location.


Q: Is the get pixel function included or do we have to write it?

A1:  image.get_pixe(x,y) is included in the SimpleImage library.


Q: Will we receive starter codes during the diagnostic simmilar to assignments?

A1:  yes! Check out the bluebook exam you can download now on the diagnostic info handout


Q: Just to confirm- we don't need to write any comments for our code in the diagnostic?

A1:  No — but comments can help a grader give you more partial credit


Q: Why does the new image need to be twice as long?

A1:  Because it is twice the width of the original image.


Q: do you have to make the width larger when mirroring an image or are we just doing it for funsies?

A1:  Must make it twice as big


Q: Can’t you just use pixel.x and pixel.y in a for each loop instead of using the nested for-loop?

A1:  Yes you can also do that.


Q: Can you explain what a nested for loop is again?

A1:  a for loop in a for loop. Eg for i in range(5): for j in range(6): # do something


Q: How do you know if a function is mutable or immutable?

A1:  I would look out for “reassignment” vs “mutations”. A reassignment is when the variable (and only the variable) is on the left hand side of a change. A mutation is when there is a “dot” in the statement. One is making a new variable. The other is changing the variable. Making a new variable does *not* persist!


Q: does the third parameter pixel in map.set_pixel() map to two variables?

A1:  The third parameter is a pixel (red_value, green_value, blue_value). set_pixel(x, y, pixel) sets pixel at x,y to be the pixel value that is passed to that function.


Q: Can we put a parameter in main() so that users can send in whatever picture they want? How do we control the image size like cropping it auto?

A1:  You can ask the user for a filename as input.


Q: how did the math mehran wrote work out so that ithe mirror image would count from the right hand side? dont really see how that translates

A1:  I would recommend drawing a picture of 3x3 image and working throught he for-each loop yourself

A2:  Try tracing through it and coding it yourself. It can also be helpful to draw pictures with locations of the pixels.


Q: Can you please explain the role of ‘(width * 2) - (x + 1)’ in the mirror image code?

A1:  That is how you get the location of the mirrored pixel. Try drawing it out on a piece of paper to see how the math translates.


Q: How is the intensity threshold decided?

A1:  live answered


Q: can we use lists on any part of the diagnostic (except Karel) or is it limited to the list problem?

A1:  You can use lists on the diagnostic unless the problem statement tells you not to.


Q: Why do we subtract x - 1 from width and not x?

A1:  I think x - (x-1) would give a negative number.


Q: what is the difference between an "empty set" and a "null set"

A1:  live answered


Q: what would the transformation be to increase contrast or saturation in an image?

A1:  live answered


Q: are there techniques to protect images so we can detect if they were manipulated?

A1:  live answered


Q: whats the RBG for parts of the image that are grayed out?

A1:  live answered


Q: Katie, is it possible to get back the original image if there is image on NYTimes that I want to confirm?

A1:  live answered