Advertisement
go6odn28

5_set_cover

Jun 19th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. def set_cover(universe_, sets_):
  2.     chosen_sets = []
  3.  
  4.     while universe_:
  5.         best_set = max(sets_, key=lambda s: len(universe_.intersection(s)))
  6.         chosen_sets.append(best_set)
  7.         universe_ -= best_set
  8.  
  9.     return chosen_sets
  10.  
  11.  
  12. universe = {int(x) for x in input().split(', ')}  # int
  13. n = int(input())
  14. sets = [set(map(int, input().split(', '))) for _ in range(n)]  # int
  15. result = set_cover(universe, sets)
  16.  
  17. for i in range(len(result)):
  18.     result[i] = sorted(result[i])
  19.  
  20. print(f'Sets to take ({len(result)}):')
  21. [print("{ " + f"{', '.join(map(str, s))}" + " }") for s in result]
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement