Skip to content

Instantly share code, notes, and snippets.

@tatmush
Created Jul 9, 2019
Embed
What would you like to do?
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