83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
import re
|
|
import pandas as pd
|
|
|
|
|
|
def list_comprehension(l1: list):
|
|
"""For input list l1 of integers, return list of elements
|
|
whose square is less than sum of all elements"""
|
|
|
|
return None
|
|
|
|
|
|
def nested_list_comprehension(l1: list, l2: list):
|
|
"""For input lists l1, l2, return a list of elements common
|
|
to both l1 and l2."""
|
|
|
|
return None
|
|
|
|
|
|
def dict_comprehension(l1: list):
|
|
"""For input list l1 of integers, return a dictionary with
|
|
keys from l1, and values are squares of keys"""
|
|
|
|
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]
|
|
|
|
print("\n-------------------\n")
|
|
print("Inputs:\n")
|
|
print(f"l1: {l1}")
|
|
print(f"l2: {l2}")
|
|
print("\n-------------------\n")
|
|
|
|
print("list_comprehension:\n")
|
|
print(f"Desired output: [2, 4]")
|
|
print(f"Output : {list_comprehension(l2)}")
|
|
print("\n-------------------\n")
|
|
|
|
print("nested_list_comprehension:\n")
|
|
print(f"Desired output: [2, 4]")
|
|
print(f"Output : {nested_list_comprehension(l1, l2)}")
|
|
print("\n-------------------\n")
|
|
|
|
print("dict_comprehension:\n")
|
|
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)
|