Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 10, 2021 13:37
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 codecademydev/0c7b01a78c7154529ed238ade5d57a72 to your computer and use it in GitHub Desktop.
Save codecademydev/0c7b01a78c7154529ed238ade5d57a72 to your computer and use it in GitHub Desktop.
Codecademy export
# 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', "Mexico"], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)', "Mexico"]]
# 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]
# 1
# Update Recorded Damages
conversion = {"M": 1000000,
"B": 1000000000}
def update_damages(damages_lst):
update_list = []
for i in range(len(damages_lst)):
if damages_lst[i] == "Damages not recorded":
update_list.append("Damages not recorded")
elif "M" in damages_lst[i]:
cleaned_damage = damages_lst[i].strip("M")
updated_damage = float(cleaned_damage) * 1000000
update_list.append(updated_damage)
elif "B" in damages_lst[i]:
cleaned_damage = damages_lst[i].strip("B")
updated_damage = float(cleaned_damage) * 1000000000
update_list.append(updated_damage)
return update_list
# test function by updating damages
updated_damages = update_damages(damages)
print(updated_damages)
# 2
# Create a Table
def organise_names(names, months, years, max_wind, areas, damages, deaths):
hurricanes = {}
for i in range(len(names)):
hurricanes[names[i]] = {
"Name":names[i],
"Month":months[i],
"Year":years[i],
"Max Sustained Wind":max_wind[i],
"Areas Affected":areas[i],
"Damage":damages[i],
"Deaths":deaths[i]
}
return hurricanes
# Create and view the hurricanes dictionary
all_hurricanes = organise_names(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)
# 3
# Organizing by Year
def organise_year(years):
year_dict = {}
for year in years:
year_dict[year] = [hurricane for hurricane in all_hurricanes.values() if hurricane["Year"]==year]
return year_dict
# create a new dictionary of hurricanes with year as key
hurricanes_by_year = organise_year(years)
# 4
# Counting Damaged Areas
def hurricanes_area(hurricanes, affected_areas):
hurricane_names = list(hurricanes.keys())
area_lists = []
unique_areas = []
areas_freq = {}
for name in hurricane_names:
areas = hurricanes[name]["Areas Affected"]
for i in range(len(areas)):
area_lists.append(areas[i])
for area in affected_areas:
for i in range(len(area)):
if area[i] in unique_areas:
continue
else:
unique_areas.append(area[i])
for unique in unique_areas:
areas_freq[unique] = area_lists.count(unique)
return areas_freq
# create dictionary of areas to store the number of hurricanes involved in
area_frequency = hurricanes_area(all_hurricanes, areas_affected)
# 5
# Calculating Maximum Hurricane Count
def max_hurricane_count(freq_data):
data = list(freq_data.values())
max_values = []
max_value_area = {}
highest_freq = data[0]
for i in range(len(data)):
if data[i] == highest_freq:
max_values.append(data[i])
for key, values in freq_data.items():
for val in max_values:
if val == values:
max_value_area[key] = val
print("There are {} areas that had {} hurricanes recorded.".format(len(max_value_area), max_values[0]))
for area, freq in max_value_area.items():
print("The area most affected by hurricanes is {}. There were {} hurricanes".format(area, freq))
return max_value_area
# find most frequently affected area and the number of hurricanes involved in
max_hurricane = max_hurricane_count(area_frequency)
print(max_hurricane)
# 6
# Calculating the Deadliest Hurricane
def max_hurricane_deaths(hurricanes):
death_data = []
max_deaths = []
hurricane_deaths = {}
max_deaths = {}
for name in hurricanes.keys():
death_data.append(hurricanes[name]["Deaths"])
hurricane_deaths[name] = hurricanes[name]["Deaths"]
death_data.sort(reverse = True)
max_mortality = death_data[0]
for key, value in hurricane_deaths.items():
if value == max_mortality:
max_deaths[key] = value
if len(max_deaths) == 1:
print("There was 1 hurricane that caused {} deaths".format(max_mortality))
else:
print("There were {} hurricanes that recorded {} deaths.".format(len(max_deaths), max_mortality))
for key, value in max_deaths.items():
print("The hurricane {} caused {} deaths".format(key, value))
return max_deaths
# find highest mortality hurricane and the number of deaths
highest_mortality = max_hurricane_deaths(all_hurricanes)
# 7
# Rating Hurricanes by Mortality
def rating_mortality(hurricanes):
hurricane_deaths = {}
hurricane_rating = {}
for name in hurricanes.keys():
hurricane_deaths[name] = hurricanes[name]["Deaths"]
for hurricane, deaths in hurricane_deaths.items():
if deaths > 10000:
hurricane_rating[hurricane] = 4
elif deaths > 1000 and deaths <= 10000:
hurricane_rating[hurricane] = 3
elif deaths > 500 and deaths <= 1000:
hurricane_rating[hurricane] = 2
elif deaths > 100 and deaths <= 500:
hurricane_rating[hurricane] = 1
elif deaths >= 0 and deaths <= 100:
hurricane_rating[hurricane] = 0
return hurricane_rating
# categorize hurricanes in new dictionary with mortality severity as key
mortality_ratings = rating_mortality(all_hurricanes)
print(mortality_ratings)
# 8 Calculating Hurricane Maximum Damage
def max_damage(hurricanes, no_damage_str = "Damages not recorded"):
damage_data = {}
damage_data_list = []
max_damage_dict = {}
for name in hurricanes.keys():
damage_data[name] = hurricanes[name]["Damage"]
if hurricanes[name]["Damage"] != no_damage_str:
damage_data_list.append(hurricanes[name]["Damage"])
else:
continue
damage_data_list.sort(reverse = True)
max_damage = damage_data_list[0]
for name, damage in damage_data.items():
if damage == max_damage:
max_damage_dict[name] = damage
if len(max_damage_dict) == 1:
print("There was 1 hurricane that caused {} dollars of damage".format(max_damage))
else:
print("There were {} hurricanes that caused {} dollars of damage".format(len(max_damage_dict), max_damage))
for key, value in max_damage_dict.items():
print("The hurricane {} caused {} dollars of damage".format(key, value))
return max_damage_dict
# find highest damage inducing hurricane and its total cost
max_damage_hurricane = max_damage(all_hurricanes)
# 9
# Rating Hurricanes by Damage
damage_scale = {0: 0,
1: 100000000,
2: 1000000000,
3: 10000000000,
4: 50000000000}
def damage_rate(hurricanes, no_damage_str = "Damages not recorded"):
damage_data = {}
damage_rating = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[], "Not Recorded":[], "Unknown":[]}
for name in hurricanes.keys():
damage_data[name] = hurricanes[name]["Damage"]
for name, damage in damage_data.items():
if damage == no_damage_str:
rating = "Not Recorded"
elif damage <= 0:
rating = 0
elif damage > 0 and damage <= 100000000:
rating = 1
elif damage > 100000000 and damage <= 1000000000:
rating = 2
elif damage > 1000000000 and damage <= 10000000000:
rating = 3
elif damage > 10000000000 and damage <= 50000000000:
rating = 4
elif damage > 50000000000:
rating = 5
else:
rating = "Unknown"
damage_rating[rating].append(name)
return damage_rating
# categorize hurricanes in new dictionary with damage severity as key
damage_rated_hurricanes = damage_rate(all_hurricanes)
print(damage_rated_hurricanes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment