# Quiz 2 Environment Diagram Practice

### Overview

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:

``````for x in xs:
x = x + 1``````

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.:

``````for x in xs:
x = x + 1``````

versus

``````for i in range(len(xs)):
xs[i] = xs[i] + 1``````

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] = []

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()          ``````

### 4. Biscuits!

``````class Game:
unc: int
duke: int

def main() -> None:
game1: Game = Game()
game1.unc = 20
game1.duke = 30
biscuits(game1)
print(game1.unc)

def biscuits(g: Game) -> None:
biscuits: int = 100 - g.unc
g.unc += biscuits
print("Free biscuits!")

if __name__ == "__main__":
main()      ``````