"""
Learning Goals:
 - Recognize whether two values have the same reference in memory
 - Recognize the difference between mutable vs immutable data types
 - Recognize the difference between destructive vs. non-destructive functions/operations
 - Use aliasing to write functions that destructively change lists
"""

a = "Hello"
b = a
c = "World"

print(a is b) # True
print(a is c) # False

###

x = [1, 2, 3]
y = x
y.append(4)
print("x:", x)
print("y:", y)

###

lst = ["A", "B", "C"]
lst.append("E")
lst.insert(0, "foo") # specifies where to add the value
lst += ["F", "G"]
print(lst)

lst = ["A", "B", "C"]
lst = lst + ["E"] # note that "E" needs to be in its own list
lst = ["foo"] + lst
lst = lst + ["F", "G"]
print(lst)

###

lst = ["A", "B", "C"]
lst.remove("A") # remove the value "A"
lst.pop(1) # remove the value at index 1
print(lst)

lst = ["A", "B", "C"]
lst = lst[1:]
lst = lst[:len(lst)-1]
print(lst)

###

a = ["A", "B", "C"]
b = a # a and b are aliased
a = a + [ ] # a now has a new reference, but the same values
print(a is b) # False

###

a = [ 1, 2, "x", "y" ]
b = a
c = [ 1, 2, "x", "y" ]
d = c
a.pop(2)
b = b + [ "woah" ]
c[0] = 42
d.insert(3, "yowza")

###

lst = ["a", "a", "c", "d", "e"]
i = 0
while i < len(lst):
    if lst[i] == "a" or lst[i] == "e":
        lst.pop(i)
    else:
        i = i + 1
print(lst)

###

lst = ["a", "a", "c", "d", "e"]
for i in range(len(lst)):
    if lst[i] == "a":
        lst.pop(i)
        break # exits immediately, only removes one "a"
print(lst)

###

def foo(lst):
    lst[1] = "bar"

x = [1, 2, 3]
print(foo(x)) # when lst is created, it copies x's reference
print(x)

###

def destructiveDouble(lst):
    for i in range(len(lst)):
        lst[i] = lst[i] * 2

x = [1,2,3]
y = destructiveDouble(x)
print(x, y)

###

def nondestructiveDouble(lst):
    result = []
    for i in range(len(lst)):
        result.append(lst[i] * 2)
    return result

x = [1,2,3]
y = nondestructiveDouble(x)
print(x, y)

###

def makePositive(lst):
    for i in range(len(lst)):
        if lst[i] < 0:
            lst[i] = lst[i] * -1

lst = [-3,2,4,-65]
makePositive(lst)
print(lst)