Skip to content

Instantly share code, notes, and snippets.

@kostor
Last active January 15, 2022 16:24
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 kostor/bc1bc764900ed01962ce6671f8a09948 to your computer and use it in GitHub Desktop.
Save kostor/bc1bc764900ed01962ce6671f8a09948 to your computer and use it in GitHub Desktop.
# names of hurricanes
names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II', 'CubaBrownsville', 'Tampico', 'Labor Day', 'New England', 'Carol', 'Janet', 'Carla', 'Hattie', 'Beulah', 'Camille', 'Edith', 'Anita', 'David', 'Allen', 'Gilbert', 'Hugo', 'Andrew', 'Mitch', 'Isabel', 'Ivan', 'Emily', 'Katrina', 'Rita', 'Wilma', 'Dean', 'Felix', 'Matthew', 'Irma', 'Maria', 'Michael']
# months of hurricanes
months = ['October', 'September', 'September', 'November', 'August', 'September', 'September', 'September', 'September', 'September', 'September', 'October', 'September', 'August', 'September', 'September', 'August', 'August', 'September', 'September', 'August', 'October', 'September', 'September', 'July', 'August', 'September', 'October', 'August', 'September', 'October', 'September', 'September', 'October']
# years of hurricanes
years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 2017, 2018]
# maximum sustained winds (mph) of hurricanes
max_sustained_winds = [165, 160, 160, 175, 160, 160, 185, 160, 160, 175, 175, 160, 160, 175, 160, 175, 175, 190, 185, 160, 175, 180, 165, 165, 160, 175, 180, 185, 175, 175, 165, 180, 175, 160]
# areas affected by each hurricane
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]
# damages (USD($)) of hurricanes
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B']
# deaths for each hurricane
deaths = [90,4000,16,3103,179,184,408,682,5,1023,43,319,688,259,37,11,2068,269,318,107,65,19325,51,124,17,1836,125,87,45,133,603,138,3057,74]
# 2
# Update Recorded Damages
conversion = {"M": 1000000,
"B": 1000000000}
def updated_damages(damages_list):
# declare variables for future use
numerical_list = []
# this loop goes thru damages list that uses M/B
# format and switches the letter with the correct
# factor
for item in damages_list:
# if damage isn't recorded leave it as it is
if item == 'Damages not recorded':
numerical_list.append('Damages not recorded')
else:
# find the factor by ##.#M/B format using conversion
# dict above. Take the number and multiply by the
# factor
numerical_list.append(float(item[:-1])*conversion[item[-1]])
return numerical_list
# test function by updating damages
updated_damages=updated_damages(damages)
print(updated_damages)
print("-----------------END of Task 2-----------------")
# 3
# Create a Table
# construct a name ordrered dictionary from rightly
# formatted general-ordered dictionary
def dictionary_constructor():
dictionary = {names[i]:
{"Name": names[i],
"Month": months[i],
"Year": years[i],
"Max Sustained Winds": max_sustained_winds[i],
"Areas Affected": areas_affected[i],
"Damages": updated_damages[i],
"Deaths": deaths[i]} for i in range(0,len(names)-1)}
return dictionary
# Create and view the hurricanes dictionary
hurricanes_dictionary = dictionary_constructor()
print(hurricanes_dictionary)
print("-----------------END of Task 3-----------------")
# 4
# Organizing by Year
def dictionary_by_year(dictionary):
# declare variables for future use
years_list = []
year_sorted_dictionary = {}
# define years_list, sorted set that will accept
# hurrican dicts data
for key in dictionary:
years_list.append(dictionary[key].get("Year","Dictionary isn't valid"))
years_list = list(sorted(set(years_list)))
# define empty yearly sorted hurricane data dictionary
# sorted by the year list
year_sorted_dictionary = {year:[] for year in years_list}
# go thru hurricane dictionary once again, and
# get every hurricane into its new yearly place
for hurricane in dictionary:
year_sorted_dictionary[dictionary[hurricane].get("Year","Dictionary isn't valid")].append(dictionary[hurricane])
return year_sorted_dictionary
# create a new dictionary of hurricanes with year and key
dictionary_by_year = dictionary_by_year(hurricanes_dictionary)
print(dictionary_by_year)
print("-----------------END of Task 4-----------------")
# 5
# Counting Damaged Areas
def count_affected_areas(dictionary):
# declare variables for future use
affected_areas_list = []
# this goes thru the given dictionary and creates a
# list of lists of affected areas
for hurricane in dictionary:
affected_areas_list.append(dictionary[hurricane].get("Areas Affected","Dictionary isn't valid"))
# go thru the affected areas list of lists, unpack it
# into a single list, sort it, set it, and then use
# the item in index i as a key. i ranges from the
# unpacked, ordered, set affected areas list
# for now all the values are 0
affected_areas_list_stripped = sorted(set([item for sublist in affected_areas_list for item in sublist]))
affected_areas_dict = {affected_areas_list_stripped[i]: 0 for i in range(0,len(affected_areas_list_stripped))}
#print(affected_areas_dict)
#this goes thru the given dictionary once again and
# counts every area that is affected, adding 1 in the
# correct place in the affected areas dict
for area in affected_areas_dict:
for hurricane in dictionary:
if area in dictionary[hurricane].get("Areas Affected"):
affected_areas_dict[area] += 1
print(affected_areas_dict)
return affected_areas_dict
# create dictionary of areas to store the number of hurricanes involved in
hurricanes_by_areas = count_affected_areas(hurricanes_dictionary)
print(hurricanes_by_areas)
print("-----------------END of Task 5-----------------")
# 6 Acciedently returning a full dictionary instead of a
# simple specific hurricane
# Calculating Maximum Hurricane Count
def max_hurricane_count(dictionary):
# dict of area affected by the most hurricanes, sorted from high
# ammount of hits to low:
hurricanes_by_areas_sorted = {k: v for k, v in reversed(sorted(hurricanes_by_areas.items(), key=lambda item: item[1]))}
# create a list of the affected area that is sorted from high to
# low
areas_affected = list(hurricanes_by_areas_sorted.keys())
# declare a list of lists for the years each area (sorted high to
# low) was affected in
years_affected = []
# run thru every area
for area in areas_affected:
# help list, stores the years for the specific area
years_for_area = []
# run thru every year in the yearly sorted dict to find the
# areas affected in said year
for year in dictionary_by_year:
# yearly sorted dict containes a list of every hurricane for
# that year, this run thru those hurricanes
for hurricane in dictionary_by_year[year]:
# if the hurricane in said year hit said area, add the year
# to the help list
if area in hurricane.get("Areas Affected"):
years_for_area.append(year)
# now add the list of years for said area to the general list
# of lists for all areas
years_affected.append(years_for_area)
# declare a list of mean intervals for each area, sorted from
# most hit area to least
mean_year_interval = []
# run thru the years that specific area was hit
for years_for_area in years_affected:
# decalre difference counte, it counts the year passed from the
# latest year of hit to the one before it
difference_counter = 0.0
# run thru the years for said area and substract each from the
# previous to get the time between hits for said area
for i in reversed(range(1,len(years_for_area))):
# if it was only hit once we can continue, there is no average
if len(years_for_area) == 1:
continue
else:
difference_counter += years_for_area[i]-years_for_area[i-1]
# if the area was hit only once, add a notice
if len(years_for_area)-1 == 0:
mean_year_interval.append("Area Was Only Hit Once")
# if the area was hit more than once, add the mean time interval
# in years between each hit for said area
else:
mean_year_interval.append(difference_counter / (len(years_for_area)-1))
# create a dict for most hit areas (how many times?) and the mean
# interval time betwean each hit
most_hit_areas = {areas_affected[i]: [list(reversed(sorted(hurricanes_by_areas.values())))[i], mean_year_interval[i]] for i in range(0,len(areas_affected)-1)}
return most_hit_areas
# find most frequently affected area and the number of hurricanes involved in
most_hit_areas = max_hurricane_count(hurricanes_dictionary)
print(most_hit_areas)
print("-----------------END of Task 6-----------------")
# 7
# Calculating the Deadliest Hurricane
def most_deathly_hurricane(dictionary):
hurricanes_by_deaths = {list(dictionary.keys())[i]: dictionary[list(dictionary.keys())[i]].get("Deaths") for i in range(len(dictionary))}
hurricanes_by_deaths = {k: v for k, v in reversed(sorted(hurricanes_by_deaths.items(), key=lambda item: item[1]))}
return {list(hurricanes_by_deaths.keys())[0]:list(hurricanes_by_deaths.values())[0]}
# find highest mortality hurricane and the number of deaths
most_deadly_hurricane = most_deathly_hurricane(hurricanes_dictionary)
print(most_deadly_hurricane)
print("-----------------END of Task 7-----------------")
# 8
# Rating Hurricanes by Mortality
def hurricane_by_mortality(dictionary):
# declare an empty mortality scale dict in which we'll
# store the hurricanes in list of dicts, each dicts for
# corresponding hurricane
mortality_dictionary = {i:[] for i in range(6)}
# go thru every hurricane and append it to the list of
# its correct mortality.
for hurricane in dictionary:
if dictionary[hurricane].get("Deaths") <= 0:
mortality_dictionary[0].append(dictionary[hurricane])
elif dictionary[hurricane].get("Deaths") <= 100:
mortality_dictionary[1].append(dictionary[hurricane])
elif dictionary[hurricane].get("Deaths") <= 500:
mortality_dictionary[2].append(dictionary[hurricane])
elif dictionary[hurricane].get("Deaths") <= 1000:
mortality_dictionary[3].append(dictionary[hurricane])
elif dictionary[hurricane].get("Deaths") <= 10000:
mortality_dictionary[4].append(dictionary[hurricane])
elif dictionary[hurricane].get("Deaths") > 10000:
mortality_dictionary[5].append(dictionary[hurricane])
return mortality_dictionary
# categorize hurricanes in new dictionary with mortality severity as key
hurricanes_by_mortality = hurricane_by_mortality(hurricanes_dictionary)
print(hurricanes_by_mortality)
print("-----------------END of Task 8-----------------")
# 9
# Calculating Hurricane Maximum Damage
def get_max_damage(dictionary):
# declare the max recorded damage from the hurricanes in the dict
# and list of names of hurricanes that caused said damage
max_damage = 0.0
hurricane_name = []
# loop thru hurricanes in the dict
for hurricane in dictionary:
# of the hurricane's damage isn't recorded, skip (uncompareable)
if dictionary[hurricane].get("Damages") != "Damages not recorded":
# if the damage of the iterated hurricane is bigger than
# the damage recorded, reset the list to the current
# hurricane and reset the max_damage threshold
if dictionary[hurricane].get("Damages") > max_damage:
hurricane_name = [hurricane]
max_damage = dictionary[hurricane].get("Damages")
# if the damage is the same, append the new hurriance to
# the list of most damaging hurricanes
elif dictionary[hurricane].get("Damages") == max_damage:
hurricane_name.append(hurricane)
# return a dict of {hurricane: max_damage}. Possible multiple
# keys with the same value if max_damage was achieved more than
# once
return {hurricane_name[i]: max_damage for i in range(0,len(hurricane_name))}
# find highest damage inducing hurricane and its total cost
max_damage_hurricane = get_max_damage(hurricanes_dictionary)
print(max_damage_hurricane)
print("-----------------END of Task 9-----------------")
# 10
# Rating Hurricanes by Damage
damage_scale = {0: 0,
1: 100000000,
2: 1000000000,
3: 10000000000,
4: 50000000000}
# categorize hurricanes in new dictionary with damage severity as key
def hurricane_by_damage(dictionary):
# declare an empty damage scale dict in which we'll
# store the hurricanes in list of dicts, each dicts for
# corresponding hurricane
damage_dictionary = {i:[] for i in range(6)}
# go thru every hurricane and append it to the list of
# its correct mortality.
for hurricane in dictionary:
if dictionary[hurricane].get("Damages") == "Damages not recorded":
continue
elif dictionary[hurricane].get("Damages") <= damage_scale[0]:
damage_dictionary[0].append(dictionary[hurricane])
elif dictionary[hurricane].get("Damages") <= damage_scale[1]:
damage_dictionary[1].append(dictionary[hurricane])
elif dictionary[hurricane].get("Damages") <= damage_scale[2]:
damage_dictionary[2].append(dictionary[hurricane])
elif dictionary[hurricane].get("Damages") <= damage_scale[3]:
damage_dictionary[3].append(dictionary[hurricane])
elif dictionary[hurricane].get("Damages") <= damage_scale[4]:
damage_dictionary[4].append(dictionary[hurricane])
elif dictionary[hurricane].get("Damages") > damage_scale[4]:
damage_dictionary[5].append(dictionary[hurricane])
return damage_dictionary
# categorize hurricanes in new dictionary with damage severity as key
hurricanes_by_damage = hurricane_by_damage(hurricanes_dictionary)
print(hurricanes_by_damage)
print("-----------------END of Task 10-----------------")
#print(hurricanes_by_damage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment