-
-
Save garolo10/d5f5b5c26169bb70339afcc9f34ccf2e to your computer and use it in GitHub Desktop.
Hurricane analysis challenge - Python (Codecademy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ['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 = [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] | |
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 = [['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 = ['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 = [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 | |
updated_damages = [] | |
for damage in damages: | |
if damage[-1] == "M": | |
y = float(damage[:-1])*1000000 | |
elif damage[-1] == "B": | |
y = float(damage[:-1])*1000000000 | |
else: | |
y = damage | |
updated_damages.append(y) | |
#print(updated_damages) | |
#3 - Dictionary of hurricanes | |
hurricanes = {} | |
count = 0 | |
for name in names: | |
hurricanes[name] = {"Name":name, | |
"Month":months[count], | |
"Year":years[count], | |
"Max Sustained Wind":max_sustained_winds[count], | |
"Areas Affected":areas_affected[count], | |
"Damage":updated_damages[count], | |
"Deaths":deaths[count]} | |
count = count + 1 | |
#print(hurricanes) | |
#4 - dictionary of hurricanes by year | |
hurricanes_date = {} | |
for key,values in hurricanes.items(): | |
if values["Year"] not in hurricanes_date: | |
hurricanes_date[values["Year"]] = [] | |
hurricanes_date[values["Year"]].append(hurricanes[key]) | |
elif values["Year"] in hurricanes_date: | |
hurricanes_date[values["Year"]].append(hurricanes[key]) | |
#for x in hurricanes_date.items(): | |
# print("\n",x) | |
#5 - count of areas affected | |
areas_affected = {} | |
for y in hurricanes.keys(): | |
araf = hurricanes[y]["Areas Affected"] | |
for area in araf: | |
if area not in areas_affected: | |
areas_affected[area] = 1 | |
elif area in areas_affected: | |
areas_affected[area] += 1 | |
#print("\nAreas affected:\n",sorted(areas_affected.items())) | |
#6.1 - Most affected area | |
for key, value in areas_affected.items(): | |
if areas_affected[key] == sorted(areas_affected.values())[-1]: | |
# print("\nMost hurricanes:\n"+key,":",value) | |
#6.2 - Most affected area | |
nam = [] | |
valdic = 0 | |
for key, value in areas_affected.items(): | |
if value > valdic: | |
valdic = value | |
nam = key | |
#print("\nMost hurricanes:\n"+nam,":",valdic) | |
#7 - Most letal hurricane | |
nam = [] | |
death_dic = 0 | |
for key, values in hurricanes.items(): | |
if values["Deaths"] > death_dic: | |
death_dic = values["Deaths"] | |
nam = values["Name"] | |
#print("\nDeadliest hurricane:\n"+nam,":",death_dic) | |
#8 - Hurricanes by mortality | |
for key,values in list(hurricanes.items()): | |
nk = values["Deaths"] | |
hurricanes[nk] = hurricanes[key] | |
del hurricanes[key] | |
hurricanes_scale = {0:[],1:[],2:[],3:[],4:[],5:[]} | |
for key in hurricanes: | |
if key == 0: | |
hurricanes_scale[0].append(hurricanes[key]) | |
elif key <= 100: | |
hurricanes_scale[1].append(hurricanes[key]) | |
elif key <= 500: | |
hurricanes_scale[2].append(hurricanes[key]) | |
elif key <= 1000: | |
hurricanes_scale[3].append(hurricanes[key]) | |
elif key <= 10000: | |
hurricanes_scale[4].append(hurricanes[key]) | |
else: | |
hurricanes_scale[5].append(hurricanes[key]) | |
for key,values in hurricanes_scale.items(): | |
# print("Categoría {} : {}\n".format(key,values)) | |
damages = {} | |
for key, value in hurricanes.items(): | |
nom = value["Name"] | |
dam = value["Damage"] | |
if nom not in damages and dam != "Damages not recorded": | |
damages[nom] = dam | |
#9 - Most damage caused by hurricane | |
for k,v in sorted(damages.items(), key=lambda item: item[1]): | |
print(k+":",v) | |
#10 - Hurricanes by damages caused | |
hurricanes_damage = {0:[],1:[],2:[],3:[],4:[],5:[],"Damages not recorded":[]} | |
for key,values in hurricanes.items(): | |
if values["Damage"] == "Damages not recorded": | |
hurricanes_damage["Damages not recorded"].append(hurricanes[key]) | |
elif values["Damage"] == 0: | |
hurricanes_damage[0].append(hurricanes[key]) | |
elif values["Damage"] <= 100000000: | |
hurricanes_damage[1].append(hurricanes[key]) | |
elif values["Damage"] <= 1000000000: | |
hurricanes_damage[2].append(hurricanes[key]) | |
elif values["Damage"] <= 10000000000: | |
hurricanes_damage[3].append(hurricanes[key]) | |
elif values["Damage"] <= 50000000000: | |
hurricanes_damage[4].append(hurricanes[key]) | |
elif values["Damage"] > 50000000000: | |
hurricanes_damage[5].append(hurricanes[key]) | |
for k,v in hurricanes_damage.items(): | |
print(k,":",v,"\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment