Hello everyone! This is the second part of practice questions for the upcoming Quiz 02. The first set covered Dicts, and this one will be focused on memory diagrams. The solutions for these problems can be found here.
NOTE (10/20/2020): On the video, none of the counter variables for the for in loops were defined on the frames, even though they should be. Please make sure that on the quiz, that whenever we have something like:
That we also define x on the frame and update that on your diagram as you go through each item on the list. Just like how you would declare a variable i whenever we have a while loop, you do the same for the variable declared in the for .. in statement.
Another note is that when we use the actual item versus the index in our for in loop, i.e.:
versus
On the heap, the first set of code (where x
represents the item on the list) nothing should change on the heap! This is because we define x
itself to be each item on that list, and therefore doing x = x + 1
would only affect the x
variable. The variable x
will not always be a direct reference to the item on the list itself. Remember, strings, ints, floats, and bools are not reference types, so a List of these variable types would mean that a for loop directly accessing each of these elements would not be a reference. Again, this is why only x
in the first example changes, not the actual element on the xs
list.
On the other hand, if we used index accessing like we do in the second set of code, that will change the element on the heap List, since we are directly referring to the list xs
when using the bracket notation.
Instructions
For these problems, please create a memory diagram for each documenting the entire execution of the program like you have practiced in class.
Review Problems
1. Pass Fail
def main() -> None:
"""main function."""
grades: List[int] = [30, 62, 88, 100]
passfail: List[bool] = []
pass_grade: int = 60
for grade in grades:
if grade >= pass_grade:
passfail.append(True)
else:
passfail.append(False)
print(passfail)
if __name__ == "__main__":
main()
2. The Return of Weather Stats (updated 10/20/2020)
def main() -> None:
"""main function."""
weather: List[int] = [73, 55, 21, 101]
temps: List[int] = weather
warm: int = 70
cold: int = 32
normalize_temps(temps, warm, cold)
print(weather)
print(temps)
def normalize_temps(temps: List[int], warm: int, cold: int) -> None:
i: int = 0
while i < len(temps):
if temps[i] >= warm:
temps[i] = temps[i] - 10
elif temps[i] <= cold:
temps[i] = temps[i] + 20
if __name__ == "__main__":
main()
3. Let’s Make Friends (updated 10/20/2020)
def main() -> None:
"""main function."""
phrases: List[str] = ["hi", "hiya", "wassup", "howdy"]
people: List[str] = ["anna", "kush", "kris", "kaki"]
talk(phrases, people)
print(phrases)
print(people)
def talk(greetings: List[str], people: List[str]) -> None:
i: int = 0
while i < len(greetings):
greetings[i] = greetings[i] + " " + people[i]
i = i + 1
happy(greetings)
def happy(xs: List[str]) -> None:
this_happy: int = 1
for ind in range(len(xs)):
i: int = 0
while i < this_happy:
xs[ind] = xs[ind] + "!"
i = i + 1
this_happy = this_happy + 1
if __name__ == "__main__":
main()