Created
December 29, 2016 04:16
-
-
Save jmsevold/b0127fda3edf7b6ec4e173f520a14ee9 to your computer and use it in GitHub Desktop.
Set covering problem using easy visuals and friendly variable names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colors_needed = set(["blue", "yellow", "green", "orange", "purple", "white", "black", "pink"]) | |
boxes = {} | |
boxes["box_1"] = set(["orange", "purple", "white"]) | |
boxes["box_2"] = set(["yellow", "orange", "blue"]) | |
boxes["box_3"] = set(["green", "purple", "black"]) | |
boxes["box_4"] = set(["purple", "white"]) | |
boxes["box_5"] = set(["black", "pink"]) | |
final_boxes = set() | |
while colors_needed: | |
best_box = None | |
best_coverage_so_far = set() | |
for box, colors in boxes.items(): | |
colors_covered_by_box = colors_needed & colors | |
if len(colors_covered_by_box) > len(best_coverage_so_far): | |
best_box = box | |
print(best_box) | |
best_coverage_so_far = colors_covered_by_box | |
colors_needed -= best_coverage_so_far | |
final_boxes.add(best_box) | |
print(final_boxes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment