import math
x = {"A": 42, "B": -3, "C": math.pi}
def xAt(key: str) -> float:
"""
Return the value associated with the key key in the dictionary x.
"""
return x[key]
y = sorted(x, key=xAt, reverse=True)
print(y)
['A', 'C', 'B']
x = {
"Hello": "World",
"hello": "world"
}
print(x)
print(x["HELLO"])
{'Hello': 'World', 'hello': 'world'}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [7], in <cell line: 8>() 1 x = { 2 "Hello": "World", 3 "hello": "world" 4 } 6 print(x) ----> 8 print(x["HELLO"]) KeyError: 'HELLO'
x = dict([(0, 0), (1, 2), (2, 4), (3, 8)])
print(x)
{0: 0, 1: 2, 2: 4, 3: 8}
import typing
def toDictionary(seq: typing.Sequence) -> dict:
r = {}
for idx in range(len(seq)):
r[idx] = seq[idx]
return r
print(toDictionary("Hello"))
{0: 'H', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}
def divisors(x: int, y: int) -> list[int]:
"""
Return the common divisors of x and y, as a list.
"""
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
print(divisors(48000, 44100))
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]
import time
memo: dict[tuple[int, int], list[int]] = {}
def divisors(x: int, y: int) -> list[int]:
"""
Return the common divisors of x and y, as a list.
"""
if (x, y) in memo:
return memo[(x, y)][:]
r = []
i = 1
while i <= x and i <= y:
if x%i == 0 and y%i == 0:
r.append(i)
i = i + 1
memo[(x, y)] = r[:]
return r
#a = time.time()
#print(divisors(48000000, 44100000))
#b = time.time()
#print(divisors(48000000, 44100000))
#c = time.time()
#print(b - a, c - b)
x = divisors(48000, 44100)
print(x)
x.append("a bag of angry squirrels")
print(divisors(48000, 44100))
<cell>30: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int" [arg-type]
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300] [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300, 'a bag of angry squirrels']
x = ["zn", "dardvark", "cte", "ants"]
def rest(s: str) -> str:
print(s, "->", s[1:])
return s[1:]
x.sort(key=rest)
print(x)
y = ["n", "ardvark", "te", "nts"]
y.sort()
print(y)
zn -> n dardvark -> ardvark cte -> te ants -> nts ['dardvark', 'zn', 'ants', 'cte'] ['ardvark', 'n', 'nts', 'te']
# tuple = immutable sequence
# list = mutable sequence
# dictionary = mutable association of keys with values [order unimportant]
t = (1, 2, 3)
t.sort # impossible because of immutability
t.reverse
l = [1, 2, 3]
l.sort # makes sense
l.reverse
l[3] # out of bounds!
d = {1: "a", 2: "b", 3: "c"}
d.sort
d.reverse
x = d[4] # undefined
d[4] = # who cares that it's undefined
x = (1, 2, 3)
x = (2, 3, 4)
y = "Hello"
y = "world"
import math
x = {"A": 42, "B": -3, "C": math.pi}
def xAt(key: str) -> float:
"""
Return the value associated with the key key in the dictionary x.
"""
return x[key]
y = sorted(x, key=xAt, reverse=True)
print(y)
for e in y:
print(e, "is", x[e])
['A', 'C', 'B'] A is 42 C is 3.141592653589793 B is -3