Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle No Centers Within Absolute Distance Threshold #53

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 27 additions & 5 deletions school_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,39 @@ def sort_key(c):
school_long), float(c.get('lat')), float(c.get('long')))
if school['scode'] == c['cscode']:
continue
if nearest_center == None or distance < nearest_distance:
if nearest_center is None or distance < nearest_distance:
nearest_center = c
nearest_distance = distance

if distance <= distance_threshold and get_pref(school['scode'], c['cscode']) > PREF_CUTOFF:
within_distance.append(center_to_dict(c, distance))

if len(within_distance) > 0:
return sorted(within_distance, key=sort_key)
else: # if there are no centers within given threshold, return one that is closest
return [center_to_dict(nearest_center, nearest_distance)]
if len(within_distance) > 0:
return sorted(within_distance, key=sort_key)
else:

# Get all centers with their distances from the school
all_centers_with_distance = [
center_to_dict(c, haversine_distance(float(school_lat), float(school_long), float(c.get('lat')), float(c.get('long'))))
for c in centers
if school['scode'] != c['cscode']
]

# Filter centers based on preference cutoff
qualified_centers = [
center for center in all_centers_with_distance
if get_pref(school['scode'], center['cscode']) > PREF_CUTOFF
]

# If qualified centers are found, sort them by preference score and return
if qualified_centers:
sorted_centers = sorted(qualified_centers, key=sort_key)
return sorted_centers
else:
# If no qualified centers, return the nearest center
return [center_to_dict(nearest_center, nearest_distance)]




def read_tsv(file_path: str) -> List[Dict[str, str]]:
Expand Down