Created
July 10, 2019 21:45
-
-
Save iris9112/a40ca7ec7e605eb87f1a2d80e468c8d1 to your computer and use it in GitHub Desktop.
Calculate second lowest cost silver plan (SLCSP)
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Calculate second lowest cost silver plan (SLCSP) | |
https://homework.adhoc.team/slcsp/ | |
The instruction for run program is: | |
python SLCSP_iris9112.py -s files/slcsp.csv -p files/plans.csv -z files/zips.csv | |
""" | |
import argparse | |
from collections import OrderedDict | |
import csv | |
silver_rate_area_state = OrderedDict() | |
silver_plans = [] | |
answers = OrderedDict() | |
def parse_csv(file_path=None, silver_plan_flag=False): | |
""" | |
Turn CSV rows into an array of dictionaries per row | |
""" | |
data = [] | |
with open(file_path, 'r') as file_obj: | |
reader = csv.DictReader(file_obj) | |
if silver_plan_flag: | |
for row in reader: | |
if row['metal_level'] == 'Silver': | |
data.append(row) | |
else: | |
for row in reader: | |
data.append(row) | |
next(reader, None) | |
return data | |
def write_csv(file_path=None, data_dict={}): | |
with open(file_path, 'w') as f: | |
writer = csv.DictWriter(f, ['zipcode', 'rate'], lineterminator='\n') | |
writer.writeheader() | |
for zipcode, rates in data_dict.items(): | |
if len(rates) > 1: | |
rates.sort() | |
writer.writerow({'zipcode': zipcode, 'rate': rates[1]}) | |
else: | |
writer.writerow({'zipcode': zipcode, 'rate': str(rates)}) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-s', '--slcsp', required=True) | |
parser.add_argument('-p', '--plans', required=True) | |
parser.add_argument('-z', '--zips', required=True) | |
args = vars(parser.parse_args()) | |
# lists of dictionaries for maintain order | |
slcsp = parse_csv(args['slcsp'], False) | |
plans = parse_csv(args['plans'], True) # has metal_level | |
zips = parse_csv(args['zips'], False) | |
for silver_plans in slcsp: | |
silver_zip = silver_plans['zipcode'] | |
for zip in zips: | |
if silver_zip == zip['zipcode']: | |
if silver_zip in silver_rate_area_state: | |
# Verifies if that zipcode already has a rate_area, if rate_area differs provide blank answer | |
if silver_rate_area_state[silver_zip]['rate_area'] != zip['rate_area']: | |
silver_rate_area_state[silver_zip] = '' | |
break | |
else: | |
silver_rate_area_state[silver_zip] = {'rate_area': zip['rate_area'], 'state': zip['state']} | |
for zipcode, rate_area_state in silver_rate_area_state.items(): | |
if not silver_rate_area_state[zipcode]: # if this valuates to None then set to a blank answer | |
answers[zipcode] = '' | |
else: | |
for plan in plans: | |
if rate_area_state['rate_area'] == plan['rate_area'] and rate_area_state['state'] == plan['state']: | |
if zipcode in answers: | |
answers[zipcode].append(plan['rate']) | |
else: | |
answers[zipcode] = [plan['rate']] | |
if zipcode not in answers: | |
# Zipcode doesn't have a rate_area/state in plans.csv | |
answers[zipcode] = '' | |
# Write each dictionary of second lowest silver plans to slcsp.csv | |
write_csv(args['slcsp'], answers) | |
print('-------------finish---------------') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment