Skip to content

Instantly share code, notes, and snippets.

@NdibeRaymond
Last active July 2, 2019 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NdibeRaymond/ac0cffdaa154c5510c5f32e8f5e870a0 to your computer and use it in GitHub Desktop.
Save NdibeRaymond/ac0cffdaa154c5510c5f32e8f5e870a0 to your computer and use it in GitHub Desktop.
# NOTE: TO RUN THIS SCRIPT, ENSURE THAT ALL THE REQUIRED FILES ARE IN THE SAME DIRECTORY AS THIS SCRIPT, # TO RUN OPEN YOUR COMMAND LINE AND CD TO THE DIRECTORY THAT CONTAINS THE FILES, THEN TYPE "python slcsp_assignment_sourcecode.py"NOTE: YOU NEED A WORKING VERSION OF PYTHON INSTALLED ON YOUR MACHING TO RUN THIS SCRIPT
# NOTE: TO RUN THIS SCRIPT, ENSURE THAT ALL THE REQUIRED FILES ARE IN THE SAME DIRECTORY AS THIS SCRIPT,
# TO RUN OPEN YOUR COMMAND LINE AND CD TO THE DIRECTORY THAT CONTAINS THE FILES, THEN TYPE "python slcsp_assignment_sourcecode.py"
# NOTE: YOU NEED A WORKING VERSION OF PYTHON INSTALLED ON YOUR MACHING TO RUN THIS SCRIPT
import csv
def get_all_slcsp():
# /******load the csv files into lists***************/
slcsp_list = list(csv.reader(open("slcsp.csv","r")))
zips_list = list(csv.reader(open("zips.csv","r")))
plans_list = list(csv.reader(open("plans.csv","r")))
# /***************************************************/
for each_row in slcsp_list[1:]: # 1. iterate through all the zip codes in slcsp_list and compare
var = None # the value to all items in the zips_list.
ambigious = False
for each_zip in zips_list[1:]:
if each_row[0] == each_zip[0]:
if not var:
var = each_zip # 2. if a match is found, record the row that matched the zipcode
elif each_zip[1] != var[1] or each_zip[4] != var[4]: # 3. if a duplicate is found, flag the zip code as abigious
ambigious = True
if not ambigious: # 4. check if the zipcode is abigious, if not proceed, if abigious, ignore the zipcode
var1 = [] #
for each_plan in plans_list[1:]: # 5. iterate through all the rows in the plans_list and compare the recorded row in 2. with every row in plans_list
if var[1] == each_plan[1] and var[4] == each_plan[4] and each_plan[2].lower() == "silver":
var1.append(each_plan[3]) #6. append to an empty list if a match is found in 5. above
if len(var1) > 1: # 7. if the length of the list is less than 2, it means the list doesn't contain a second lowest cost silver plan and therefore, is to be ignored
# /*********** figure out the second lowest value in the list, and append it to the initial slcsp_list rate column****************/
highest = var1[0]
lowest = var1[0]
for each in var1:
if each > highest:
highest = each
if each < lowest:
lowest = each
second_lowest = highest
for each in var1:
if each > lowest and each < second_lowest:
second_lowest = each
each_row[1] = second_lowest
#/************************************************************************************************/
return slcsp_list
#/*****************open a new csv file and write to it********************/
def write_to_new_csv(slcsp_list):
file = open("DSF_slcsp_assigment.csv","w")
surrogate = csv.writer(file)
for each in slcsp_list:
surrogate.writerow(each)
file.close()
#/***************************************************************************/
#call the functions
write_to_new_csv(get_all_slcsp())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment