Created
July 9, 2019 10:36
-
-
Save tatmush/a5e3ca3495bdaef8fdf07c839976b3f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
""" | |
Gets all silver plans out of plans.csv. | |
Returns a silverPlans dictionary consisting of | |
a tuple key and array value. | |
{(state, rate_area): [...], } | |
""" | |
def getSilverPlans(): | |
with open("plans.csv", "r") as plans: | |
plans = csv.DictReader(plans, delimiter = ",") | |
silverPlans = {} | |
for plan in plans: | |
if plan["metal_level"] == "Silver": | |
if silverPlans.__contains__((plan["state"], plan["rate_area"])): | |
rates = silverPlans[(plan["state"], plan["rate_area"])] | |
rates.append(plan["rate"]) | |
silverPlans[(plan["state"], plan["rate_area"])] = rates | |
else: | |
silverPlans[(plan["state"], plan["rate_area"])] = [plan["rate"]] | |
return silverPlans | |
""" | |
Get the second lowest cost silver plan out of an array of silver plans. | |
""" | |
def getSlcsp(rates): | |
rates = set(rates) # remove duplicates out of the array | |
rates = sorted(rates) # sort the list in ascending order | |
try: | |
slcsp = float(rates[1]) | |
return "{0:.2f}".format(slcsp) | |
except IndexError: | |
return "" | |
""" | |
Get the zipcodes for all rate areas in silverPlans dictionary. | |
Return zipcode dictionary with a zipcode key and rate value. | |
""" | |
def getZipCodes(silverPlans): | |
with open("zips.csv", "r") as zips: | |
zips = csv.DictReader(zips, delimiter=",") | |
zipCodes = {} | |
for zip in zips: | |
state_RateAreaTuple = (zip["state"], zip["rate_area"]) | |
if silverPlans.__contains__(state_RateAreaTuple): | |
rate = silverPlans[state_RateAreaTuple] | |
zipCodes[zip["zipcode"]] = silverPlans[state_RateAreaTuple] | |
return zipCodes | |
def getRate(zipcode): | |
with open("slcsp.csv", "r") as slcps: | |
slcps = csv.DictReader(slcps, delimiter=",") | |
for slcp in slcps: | |
if zipcode == slcp["zipcode"]: | |
return(zipCodes[slcp["zipcode"]]) | |
return "slcsp not found" | |
silverPlans = getSilverPlans() | |
for rateArea, rates in silverPlans.items(): | |
silverPlans[rateArea] = getSlcsp(rates) | |
zipCodes = getZipCodes(silverPlans) | |
zipcode = "" | |
try: | |
if len(sys.argv) != 2: | |
raise Exception("Arguments error") | |
zipcode = sys.argv[1] | |
except Exception as e: | |
print("Error! {}".format(e)) | |
sys.exit(1) | |
print(getRate(zipcode)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment