2024-01-13 17:31:56 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
stations = {
|
|
|
|
"kone": set(["id", "nv", "ut"]),
|
|
|
|
"ktwo": set(["wa", "id", "mt"]),
|
|
|
|
"kthree": set(["or", "nv", "ca"]),
|
|
|
|
"kfour": set(["nv", "ut"]),
|
|
|
|
"kfive": set(["ca", "az"]),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-15 12:18:31 +00:00
|
|
|
def fget():
|
|
|
|
needed = set()
|
|
|
|
for states in stations.values():
|
|
|
|
needed |= states
|
|
|
|
return needed
|
|
|
|
|
2024-01-13 17:31:56 +00:00
|
|
|
|
2024-01-15 12:18:31 +00:00
|
|
|
def greedy():
|
2024-01-13 17:31:56 +00:00
|
|
|
states_needed = fget()
|
|
|
|
logger.debug(states_needed)
|
|
|
|
final_stations = set()
|
|
|
|
|
|
|
|
while states_needed:
|
|
|
|
best_station = None
|
|
|
|
states_covered = set()
|
|
|
|
for station, states in stations.items():
|
|
|
|
covered = states_needed & states
|
|
|
|
|
|
|
|
if len(covered) > len(states_covered):
|
|
|
|
best_station = station
|
|
|
|
states_covered = covered
|
|
|
|
|
|
|
|
states_needed -= states_covered
|
|
|
|
final_stations.add(best_station)
|
|
|
|
|
|
|
|
return final_stations
|
|
|
|
|
|
|
|
|
|
|
|
final_stations = greedy()
|
|
|
|
print(f"final stations greedy approximation: {final_stations}")
|