Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 20, 2022 23:28
Show Gist options
  • Save codecakes/85af31d4a9b8cb761a8076cffe2d9a26 to your computer and use it in GitHub Desktop.
Save codecakes/85af31d4a9b8cb761a8076cffe2d9a26 to your computer and use it in GitHub Desktop.
The problem of set of combinations using approximation
from typing import Dict
def find_optimal_stations():
places_to_cover = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"])
hashset_places: Dict[set] = {
"kone": set(("id", "nv", "uv")),
"ktwo": set(["wa", "id", "mt"]),
"k3": set(["or", "nv", "ca"]),
"k4": set(["nv", "ut"]),
"k5": set(["ca", "az"]),
}
best_station: Optional[str] = None
is_covered = set()
stations_to_cover = []
while places_to_cover:
for station, places in hashset_places.items():
if (common_places := places & places_to_cover) and len(
common_places) > len(is_covered):
best_station = station
is_covered = common_places
stations_to_cover += [best_station]
places_to_cover -= is_covered
is_covered.clear()
return stations_to_cover
print(find_optimal_stations())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment