Lecture Materials

Questions & Answers


Q: how many more classes do we have in 106A?

A1:  We’re in wk 5 now. After this lecture we have two more lectures and then 4 in wk 6, 7, 8 so 14 lectures :)


Q: what do i do if i havent received a grade for hw 2?

A1:  Please email your section leader and cc the head TA Tara


Q: Can anyone see juliette’s screen?

A1:  I can see her screen. Maybe try rejoining the Zoom?


Q: can’t you just do ans = c + ans for all c in s?

A1:  Yep that works as well :)


Q: can we use s[-1::-1]

A1:  Yep that also works!


Q: what does the code ch = s[i_rev] do?

A1:  ch =s[i_rev] gets the character in the string s at index i_rev and stores it in a variable named ch :)


Q: Is one method "better" than another? Should we write what makes the most sense to us?

A1:  And Juliette just said that reverse2 and 3 are the best stylistically

A2:  Yeah some of the code is cleaner than other methods. So far, reverse2 is my favorite solution :)

A3:  But to follow up on this, none of the methods shown today are bad


Q: Is I represents the position of elements in string ?

A1:  Yes, if you are referencing the i in for i in range(len(s)). That i will take on different values and at each point be an index in the string s


Q: for result = ‘’ and result = [] is the difference ‘’ is string and [] is list?

A1:  Yes that is exactly right!


Q: i miss the music! it was great last week :)

A1:  We’ll bring it back next time! I like it too :)


Q: does return reversed(result) work?

A1:  No that does not work. result is the empty string. reversed works on numbers. reversed(s) also does not work because it is a string


Q: can you explain result = ch + result vs result = result + ch again?

A1:  Yes! So result = result + ch takes whatever is in result and adds ch to the end of that string. result = ch + result adds result to the end of the ch string. Please follow up if you have more questions!


Q: For reversing could we just do s[::-1]?

A1:  Yep that also works!


Q: how to access the first element in a tuple?

A1:  Tuple syntax is very similar to string and list syntax. Here’s a guide to tuples: https://cs.stanford.edu/people/nick/py/python-tuple.html To answer your question, t[0] would access the first element of a tuple named t


Q: Which reverse is more effective?

A1:  We like reverse 2 and reverse 3 the best stylistically :)


Q: for shorter code, is there something like #define, typedef or using in python? so if I do using pii = pair<int, int>; and then afterward I only need to type pii everytime instead of pair<int, int>

A1:  We do not learn about global variables in 106A so I’m not sure if Python has a similar feature to C/C++. Let’s ask Juliette at the end. As an aside, we prefer that you do not use global variables in 106A.


Q: we can say also limit += 5 instead of limit = 55? what seems more declarative or better in style?

A1:  I would use limit = 55 instead of limit += 5. I would keep with the theme of the problem description. It says if speed under 55, fine is 100, other fine is 200…


Q: If you are only looping through one string, how do you acess the indicies of the other string to see if the same character is there.

A1:  Say you have a for loop; for i in range(len(s)). i will take on the indices of s. Inside this for loop, you can use i however you choose. There could be another string result that you could index inside (being careful to not index outside of the bounds of result). s = “Hello” result = “There” for i in range(len(s)): if s[i] == result[i]: do something


Q: follow up question to: for shorter code, is there something like #define, typedef or using in python? so if I do using pii = pair<int, int>; and then afterward I only need to type pii everytime instead of pair<int, int> I don’t think it is really global variable. These basically just replaces all the pii in the code with pair<int, int> instead of actually setting pii to a variable.

A1:  There is not an way to do this in python that I know of. This is because python isn’t type sensitive the way that other languages are.


Q: oh that makes so much sense! thank you

A1:  You’re welcome :)


Q: how many indicies can you put in .startswith?

A1:  You can put as many characters as you want as the parameter. Here’s a page with more information: https://www.w3schools.com/python/ref_string_startswith.asp


Q: what is the other way to get rid of the newline character?

A1:  I don’t know of another way other than s.strip(). Let’s ask Juliette at the end


Q: Is there a reference sheet with all these built-in functions?

A1:  Yep! Here’s the string reference guide: https://cs.stanford.edu/people/nick/py/python-string.html You can find it on the class website inside the Python guide (on the side bar)


Q: Why does the replace one start with str, but the others all start with s? are those interchangable?

A1:  s or str are typically used to name a string variable. Either one can be used :)


Q: How do we know when to use /n?

A1:  Usually you use it when file processing. You’ll open the file, iterate through the lines, strip off the newline character, and then continue processing each line however you choose. I believe you do not need to use it in Crypto.


Q: how many bytes do an int have? I heard that int in python actually become something like bigInteger in java when the number become bigger, how does that work?

A1:  An int occupies 4 bytes. I believe that ints are pretty much always 4 bytes. On 32bit machines ints are 2 bytes (but most machines are 64bit these days)


Q: can you explain string.split again?

A1:  Yes! string.split takes in a string and splits it into smaller strings based off a delimeter (symbol like space or comma) and puts those strings in a list


Q: So what exactly is unicode? I'm confused

A1:  Unicode maps bit patterns to characters. Previously we used ascii but then we couldn’t represent many characters from other languages, emojis etc. It’s a system to tell your computer which zeros and ones corresponds to which characters :)


Q: So writting in unicode is heavier tan typing normal text?

A1:  Yes, unicode takes up more bytes than a typing in ascii


Q: if you used unicode in the experimental sever would it work?

A1:  It does!


Q: is there a type in python where it represents a number way bigger than the 2^32 - 1. It stores the number with a list of int and does math in vertical form like how we do math.

A1:  live answered