In [ ]:
def reverse(lst: list) -> None:
    """
    Reverse the list lst.
    """
    for idx in range(len(lst)//2):
        rIdx = len(lst) - idx - 1
        tmp = lst[idx]
        lst[idx] = lst[rIdx]
        lst[rIdx] = tmp
        
x = [2, 4, 6, 0, 1]
reverse(x)
print(x)
reverse(x)
print(x)
[1, 0, 6, 4, 2]
[2, 4, 6, 0, 1]
In [ ]:
len("Hello")
len(42)
<cell>2: error: Argument 1 to "len" has incompatible type "int"; expected "Sized"  [arg-type]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [4], in <cell line: 2>()
      1 len("Hello")
----> 2 len(42)

TypeError: object of type 'int' has no len()
In [ ]:
def squareList(l: list[float]) -> None:
    """
    Square each of the nubers in the list l.
    """
    for i in range(len(l)):
        l[i] = l[i]**2
        
x = [2.0, 4.0, 6.0, 0.0, 1.0]
squareList(x)
print(x)
[4.0, 16.0, 36.0, 0.0, 1.0]
In [ ]:
def removeFactorsOfTwo(l: list[int]) -> None:
    """
    Mutate l to remove all factors of 2 from every contained integer.
    """
    for idx in range(len(l)):
        val = l[idx]
        while val%2 == 0 and val > 1:
            val = val // 2
        l[idx] = val
        
x = [2, 4, 6, 0, 1]
removeFactorsOfTwo(x)
print(x)
[1, 1, 3, 0, 1]
In [ ]:
x = [2, 8, 6, 0, 1]
y = [2, 4, 6, 0, 1]
print(x == y)
print(x)
print(y)
x[1] = 4
print(x == y)
print(x is y)
z = x
print(x is z)
print(x is x)
False
[2, 8, 6, 0, 1]
[2, 4, 6, 0, 1]
True
False
True
True
In [ ]:
import math

def greatest(lst: list[float]) -> float:
    """
    Return the greatest value in lst.
    """
    assert len(lst) > 0, "No greatest in an empty list"
    r = lst[0]
    for val in lst:
        if val > r:
            r = val
    return r

print(greatest([-math.pi, -math.e, -1024]))
-2.718281828459045
In [ ]:
e = [2, 6, 8]
print("e[2] =", e[2], len(e))
e.insert(1, 4)
print(e)
print("e[2] =", e[2], len(e))
e.insert(0, 0)
print(e)
print("e[2] =", e[2], len(e))
e[2] = 8 3
[2, 4, 6, 8]
e[2] = 6 4
[0, 2, 4, 6, 8]
e[2] = 4 5
In [ ]:
e = [0, 2, 4, 6, 8]
e.append(10)
print(e)
[0, 2, 4, 6, 8, 10]
In [ ]:
lst = []
for i in range(0, 100, 2):
    lst.append(i)
print(lst)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
In [ ]:
def divisors(x: int, y: int) -> list[int]:
    """
    Return a list of the common divisors of x and y.
    """
    r = []
    i = 1
    while i <= x and i <= y:
        if x%i == 0 and y%i == 0:
            r.append(i)
        i = i + 1
    return r

d = divisors(44100, 48000)
print(d)
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]
In [ ]:
e = [0, 2, 4, 6, 8]
e.pop(0)
print(e)
print(e[0])
print("Popped", e.pop())
print(e)
[2, 4, 6, 8]
2
Popped 8
[2, 4, 6]
In [ ]:
def reverseBadly(lst: list) -> list:
    r = []
    while len(lst) > 0:
        r.append(lst.pop())
    return r

x = [2, 4, 6, 0, 1]
y = reverseBadly(x)
print(y)
print(x)
[1, 0, 6, 4, 2]
[]
In [ ]:
x = [2, 4, 6, 0, 1]
for v in x:
    print(v)
    x.pop(0)
2
6
1
In [ ]:
x = [2, 4, 6, 0, 1]
y = x[:]
y[0] = 4
print(x)
print(y)
[2, 4, 6, 0, 1]
[4, 4, 6, 0, 1]