-
Notifications
You must be signed in to change notification settings - Fork 0
/
slcsp.py
76 lines (57 loc) · 2.11 KB
/
slcsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import csv
import sys
silver_plans = list()
solvable_rate_areas = set()
zip_codes_rate_areas = dict()
def process_slcsp_file():
with open('slcsp.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
print('zipcode,rate')
for row in reader:
print(f"{row['zipcode']},{get_rate(row['zipcode'])}")
def get_rate(zipcode):
rate_areas = zip_codes_rate_areas[zipcode]
if len(rate_areas) != 1:
return ''
rate_area = list(rate_areas)[0]
if rate_area not in solvable_rate_areas:
return ''
silver_plans_for_rate_area = get_silver_plans_by_rate_area(rate_area)
rates = set(map(lambda plan: plan['rate'], silver_plans_for_rate_area))
if len(rates) > 1:
return get_slcsp((rates))
else:
return ''
def get_silver_plans_by_rate_area(rate_area):
return [silver_plan for silver_plan in silver_plans if silver_plan['rate_area'] == rate_area]
def get_slcsp(rates):
return '{0:.2f}'.format(float(sorted(rates)[1]))
def load_silver_plans():
with open('plans.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
next(reader)
for row in reader:
if row['metal_level'] == 'Silver':
silver_plans.append({
'id': row['plan_id'],
'rate_area': (row['state'], row['rate_area']),
'rate': row['rate'],
})
solvable_rate_areas.add((row['state'], row['rate_area']))
def load_zipcodes_rate_areas():
with open('zips.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['zipcode'] not in zip_codes_rate_areas.keys():
zip_codes_rate_areas[row['zipcode']] = set()
zip_codes_rate_areas[row['zipcode']].add(
(row['state'], row['rate_area']))
if __name__ == "__main__":
load_silver_plans()
load_zipcodes_rate_areas()
if len(sys.argv) > 1:
zipcode = sys.argv[1]
print('zipcode,rate')
print(f"{zipcode},{get_rate(zipcode)}")
else:
process_slcsp_file()