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

Refactor/center remaining capacity formatting #33

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions school_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import argparse
import os
from typing import Dict, List
from pprint import pprint

from utils.utils import filter_dict_with_non_zero_values, sort_dict_by_value
from utils.custom_logger import configure_logging


Expand Down Expand Up @@ -212,6 +214,13 @@ def is_allocated(scode1: str, scode2:str) -> bool:


logger.info("Remaining capacity at each center (remaining_capacity cscode):")
logger.info(sorted([(v,k) for k, v in centers_remaining_cap.items() if v != 0]))
logger.info(f"Total remaining capacity across all centers: {sum({k:v for k, v in centers_remaining_cap.items() if v != 0}.values())}")
remaining_capacity_data=sort_dict_by_value(filter_dict_with_non_zero_values(centers_remaining_cap))
vacent_seat_data=[]
for cscode, remaining_seat in remaining_capacity_data.items():
school=next(filter(lambda x: x['cscode']==cscode, centers))
school_name=school["name"]
vacent_seat_data.append({"cscode":cscode, "remaining_capacity":remaining_seat, "school":school_name})
pprint(vacent_seat_data)
logger.info(f"Total remaining capacity across all centers: {sum(centers_remaining_cap.values())}")
logger.info(f"Students not assigned: {remaining}")

Empty file added utils/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import math
def filter_dict_with_non_zero_values(d:dict)->dict:
return {k: v for k, v in d.items() if v != 0}
def sort_dict_by_value(d:dict)->dict:
return dict(sorted(d.items(), key=lambda item: item[1]))

def haversine_distance(lat1, lon1, lat2, lon2):
"""
Calculate the great circle distance between two points
on the earth specified in decimal degrees
"""
RADIUS_EARTH = 6371 # Radius of Earth in kilometers
# Convert decimal degrees to radians
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])

# Haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = RADIUS_EARTH * c
return distance