primes = {2,3,5,7,11,13,17,23,29,31} # set d = {2:'two', 3:'three'} # dict # def factors(num : int) -> list[int]: # ans = [] # list # for n in primes: # if num % n == 0: # ans.append(n) # return ans def factors(num : int) -> list[int]: return [n for n in primes if num % n == 0] # def byfactor(nums : list[int]) -> dict[int,set[int]]: # ans = {} # dict # for num in nums: # for f in factors(num): # if f not in ans: # ans[f] = set() # Q # ans[f].add(num) # inserting into a set # return ans def byfactor(nums : list[int]) -> dict[int,set[int]]: return { f:{ num for num in nums if f in factors(num) } for num in nums for f in factors(num) } print('factors of 340:', factors(340)) print('courses of factors:', byfactor([124,128,173,225,340]))