Index out of bound error Python Greedy Algorithm

Viewed 25

My code is :

states_needed = {"mt", "wa", "or", "id", "nv", "ut", "ca", "az"}
stations = {}
stations["kone"] = {"id", "nv", "ut"}
stations["ktwo"] = {"wa", "id", "mt"}
stations["kthree"] = {"or", "nv", "ca"}
stations["kfour"]= {"nv", "ut"}
stations["kfive"]= {"ca", "az"}
final_stations = set()
best_station = None
states_covered = set()
    for station, states_for_station in stations.items():
covered = states_needed & states_for_station
if len(covered) > len(states_covered):
    best_station = station
    states_covered = covered
    final_stations.add(best_station)
states_needed -= states_covered
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)
print final_stations

I receive an index out of bound error - how do I fix? I am running a greedy algorithm exercise. I am unsure whether this is a syntax or indenting issue.

0 Answers
Related