Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 24, 2020 20:39
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/9d5f0de650dcb3020de6b4119e9a84bf to your computer and use it in GitHub Desktop.
Save codecademydev/9d5f0de650dcb3020de6b4119e9a84bf to your computer and use it in GitHub Desktop.
Codecademy export
from math import inf
# 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 - write your update damages function here:
def update_damages(damages_list):
new_damages = []
float_cost = 0
for record in damages_list:
if record == "Damages not recorded":
new_damages.append(record)
else:
if record.endswith("M"):
float_cost = float(record[:-1]) * 1000000
elif record.endswith("B"):
float_cost = float(record[:-1]) * 1000000000
new_damages.append(float_cost)
return new_damages
# 3 - write your construct hurricane dictionary function here:
def hurricane_dict(h_names, h_months, h_years, h_max_wind, h_areas, h_damages, h_deaths):
h_dictionary = {}
for idx in range(0, len(names)):
h_dictionary[h_names[idx]] = {"Name": h_names[idx],
"Month": h_months[idx],
"Year": h_years[idx],
"Max Sustained Wind": h_max_wind[idx],
"Areas Affected": h_areas[idx],
"Damage": h_damages[idx],
"Deaths": h_deaths[idx]}
return h_dictionary
# 4 - write your construct hurricane by year dictionary function here:
def hurricanes_by_year(h_dictionary):
h_by_year = {}
for name, h_data in h_dictionary.items():
new_entry = [{"Name": h_data["Name"],
"Month": h_data["Month"],
"Year": h_data["Year"],
"Max Sustained Wind": h_data["Max Sustained Wind"],
"Areas Affected": h_data["Areas Affected"],
"Damage": h_data["Damage"],
"Deaths": h_data["Deaths"]}]
if h_data["Year"] in h_by_year:
h_by_year[h_data["Year"]] += new_entry
else:
h_by_year[h_data["Year"]] = new_entry
return h_by_year
# 5 - write your count affected areas function here:
# Counts how often each area is listed as an affected area of a hurricane.
# Return the results in a dictionary where the keys are the affected areas and
# the values are counts of how many times the areas were affected.
def count_affected_areas(h_dictionary):
areas_count = {}
for name, h_data in h_dictionary.items():
for area in h_data["Areas Affected"]:
if area in areas_count:
areas_count[area] += 1
else:
areas_count[area] = 1
return areas_count
# 6 - write your find most affected area function here:
# Returns a list containing the number of times the most affected areas were hit and a list of those areas
# (if more than one area has the same number of hits) [ number of hits, [list of areas affected] ]
def find_most_affected_area(h_area_count):
most_affected = [1, []]
for key, value in h_area_count.items():
if value not in most_affected:
if value > most_affected[0]:
most_affected[0] = value
most_affected[1] = [key]
elif value == most_affected[0]:
most_affected[0] = value
most_affected[1].append(key)
return most_affected
# 7 - write your greatest number of deaths function here:
def find_greatest_no_of_deaths(h_dictionary):
most_deaths = [-1, []]
for name, h_data in h_dictionary.items():
if h_data["Deaths"] not in most_deaths:
if h_data["Deaths"] > most_deaths[0]:
most_deaths[0] = h_data["Deaths"]
most_deaths[1] = [name]
elif h_data["Deaths"] == most_deaths[0]:
most_deaths[0] = h_data["Deaths"]
most_deaths[1].append(name)
return most_deaths
# 8 - write your catgeorize by mortality function here:
def create_mortality_dictionary(h_dictionary):
def get_rating(no_deaths):
mortality_scale = {
0: (0, 99),
1: (100, 499),
2: (500, 999),
3: (1000, 9999),
4: (10000, inf)
}
for d_rating, values in mortality_scale.items():
if values[0] <= no_deaths <= values[1]:
return d_rating
dictionary_of_death = {}
for name, h_data in h_dictionary.items():
# print(name," : ", h_data["Deaths"], " : ", get_rating(h_data["Deaths"]))
rating = get_rating(h_data["Deaths"])
new_entry = [{"Name": h_data["Name"],
"Month": h_data["Month"],
"Year": h_data["Year"],
"Max Sustained Wind": h_data["Max Sustained Wind"],
"Areas Affected": h_data["Areas Affected"],
"Damage": h_data["Damage"],
"Deaths": h_data["Deaths"]}]
if rating in dictionary_of_death:
dictionary_of_death[rating] += new_entry
else:
dictionary_of_death[rating] = new_entry
return dictionary_of_death
# 9 - write your greatest damage function here:
def find_most_destructive(h_dictionary):
most_damage = [-1, []]
for key, value in h_dictionary.items():
if value["Damage"] not in most_damage:
if value["Damage"] != "Damages not recorded":
if value["Damage"] > most_damage[0]:
most_damage[0] = value["Damage"]
most_damage[1] = [key]
elif value["Damage"] == most_damage[0]:
most_damage[0] = value["Damage"]
most_damage[1].append(key)
most_damage[0] = "${:,.2f}".format(most_damage[0])
return most_damage
# write your categorize by damage function here:
def create_categorise_by_damage_dictionary(h_dictionary):
def get_scale(cost):
damage_scale = {
0: (0, 99999999),
1: (100000000, 999999999),
2: (1000000000, 9999999999),
3: (10000000000, 49999999999),
4: (50000000000, inf)
}
for d_rating, values in damage_scale.items():
if values[0] <= cost <= values[1]:
return d_rating
dictionary_of_destruction = {}
for name, h_data in h_dictionary.items():
if h_data["Damage"] != "Damages not recorded":
rating = get_scale(h_data["Damage"])
new_entry = [{"Name": h_data["Name"],
"Month": h_data["Month"],
"Year": h_data["Year"],
"Max Sustained Wind": h_data["Max Sustained Wind"],
"Areas Affected": h_data["Areas Affected"],
"Damage": h_data["Damage"],
"Deaths": h_data["Deaths"]}]
if rating in dictionary_of_destruction:
dictionary_of_destruction[rating] += new_entry
else:
dictionary_of_destruction[rating] = new_entry
return dictionary_of_destruction
def test_print_dictionary():
for record, data in hurricane_data.items():
print(record)
for key, value in data.items():
print(key, " : ", value)
print("\n")
def test_print_by_year():
for record, data in hurricane_year.items():
print(record)
for entry in data:
for key, value in entry.items():
print(key, " : ", value)
print("\n")
def test_print_affected_areas_count():
for key, value in hurricane_area_count.items():
print(key, value)
#print(hurricane_area_count)
def test_print_by_rating():
for record, data in mortality_dictionary.items():
print(record)
for entry in data:
for key, value in entry.items():
print(key, " : ", value)
print("\n")
print("\n")
def test_print_by_damage():
for record, data in damage_dictionary.items():
print("------- ", record, " -------")
for entry in data:
for key, value in entry.items():
print(key, " : ", value)
print("\n")
print("\n")
# Q2
float_damages = update_damages(damages)
# Q3
hurricane_data = hurricane_dict(names, months, years, max_sustained_winds, areas_affected, float_damages, deaths)
# test_print_dictionary()
# Q4
hurricane_year = hurricanes_by_year(hurricane_data)
# test_print_by_year()
# Q5
hurricane_area_count = count_affected_areas(hurricane_data)
# test_print_affected_areas_count()
# Q6
most_affected_areas = find_most_affected_area(hurricane_area_count)
# print(most_affected_areas)
# Q7
# print(find_greatest_no_of_deaths(hurricane_data))
# Q8
mortality_dictionary = create_mortality_dictionary(hurricane_data)
# test_print_by_rating()
# Q9
most_destructive = find_most_destructive(hurricane_data)
# print(most_destructive)
# Q10
damage_dictionary = create_categorise_by_damage_dictionary(hurricane_data)
test_print_by_damage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment