solutions
This commit is contained in:
92
solutions.py
Normal file
92
solutions.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
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"""
|
||||||
|
|
||||||
|
sum_l1 = sum(l1)
|
||||||
|
return [elem for elem in l1 if elem * elem < sum_l1]
|
||||||
|
|
||||||
|
|
||||||
|
def nested_list_comprehension(l1: list, l2: list):
|
||||||
|
"""For input lists l1, l2, return a list of elements common
|
||||||
|
to both l1 and l2."""
|
||||||
|
|
||||||
|
# result = []
|
||||||
|
# for e1 in l1:
|
||||||
|
# for e2 in l2:
|
||||||
|
# if e1 == e2:
|
||||||
|
# result.append(e1)
|
||||||
|
|
||||||
|
result = [e1 for e2 in l2 for e1 in l1 if e1 == e2]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def dict_comprehension(l1: list):
|
||||||
|
"""For input list l1 of integers, return a dictionary with
|
||||||
|
keys from l1, and values are squares of keys"""
|
||||||
|
|
||||||
|
result = {e: e * e for e in l1}
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
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: :")
|
||||||
|
for pair in generator(l1, l2):
|
||||||
|
print(f" {pair}")
|
||||||
Reference in New Issue
Block a user