more exercises

This commit is contained in:
2023-08-08 18:33:24 -07:00
parent 137de68361
commit 5c417600e7
2 changed files with 65 additions and 0 deletions

View File

@@ -23,6 +23,14 @@ def dict_comprehension(l1: list):
return None
def generator(l1: list, l2: list):
"""For input lists l1 and l2 of integers, return a generator
that gives all pairings (e1, e2) of element e1 from l1 and
element e2 of l2."""
raise NotImplementedError("Not implemented")
if __name__ == "__main__":
l1 = [1, 2, 3, 4]
l2 = [2, 4, 6, 8]
@@ -47,3 +55,28 @@ if __name__ == "__main__":
print("Desired output: {1: 1, 2: 4, 3: 9, 4: 16}")
print(f"Output: : {dict_comprehension(l1)}")
print("\n-------------------\n")
print("generator:\n")
print("Desired output:")
print(" (1, 2)")
print(" (1, 4)")
print(" (1, 6)")
print(" (1, 8)")
print(" (2, 2)")
print(" (2, 4)")
print(" (2, 6)")
print(" (2, 8)")
print(" (3, 2)")
print(" (3, 4)")
print(" (3, 6)")
print(" (3, 8)")
print(" (4, 2)")
print(" (4, 4)")
print(" (4, 6)")
print(" (4, 8)")
print("Output: :")
try:
for pair in generator(l1, l2):
print(f" {pair}")
except NotImplementedError as e:
print(e)

View File

@@ -34,6 +34,16 @@ def dict_comprehension(l1: list):
return result
def generator(l1: list, l2: list):
"""For input lists l1 and l2 of integers, return a generator
that gives all pairings (e1, e2) of element e1 from l1 and
element e2 of l2."""
for e1 in l1:
for e2 in l2:
yield (e1, e2)
if __name__ == "__main__":
l1 = [1, 2, 3, 4]
l2 = [2, 4, 6, 8]
@@ -58,3 +68,25 @@ if __name__ == "__main__":
print("Desired output: {1: 1, 2: 4, 3: 9, 4: 16}")
print(f"Output: : {dict_comprehension(l1)}")
print("\n-------------------\n")
print("generator:\n")
print("Desired output:")
print(" (1, 2)")
print(" (1, 4)")
print(" (1, 6)")
print(" (1, 8)")
print(" (2, 2)")
print(" (2, 4)")
print(" (2, 6)")
print(" (2, 8)")
print(" (3, 2)")
print(" (3, 4)")
print(" (3, 6)")
print(" (3, 8)")
print(" (4, 2)")
print(" (4, 4)")
print(" (4, 6)")
print(" (4, 8)")
print("Output: :")
for pair in generator(l1, l2):
print(f" {pair}")