Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 13, 2021 16:00
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/a7711ee1890852abfcbc1ca21b70e2c0 to your computer and use it in GitHub Desktop.
Save codecademydev/a7711ee1890852abfcbc1ca21b70e2c0 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'], ['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]
# 1 Update Recorded Damages
# Step 1: define the function to accept a single variable (the list in question we want to convert). You are already provided with conversions, but you will need to create an empty list to populate.
# Step 2: Create a for loop that will convert each damage in damages. You can do so by checking to make sure that each 'M' and 'B' (indexed as -1!) is converted against the right conversion provided in the sample problem. Once converted, append to the empty list from step 1. Remember that you will also need to check for 'damages not recorded' as well.
def data_cleanup(damages):
conversion = {"M": 1000000,
"B": 1000000000}
updated_damages = []
for damage in damages:
if damage == "Damages not recorded":
updated_damages.append(damage)
if damage [-1] == "M":
updated_damages.append(float(damage.strip("M"))*conversion["M"])
if damage [-1] == "B":
updated_damages.append(float(damage.strip("B"))*conversion["B"])
return updated_damages
updated_damages = data_cleanup(damages)
print(updated_damages)
# 2 Create a Table
# Step 1: create a new list and make it equal to the zipped list for the variables to be contained in values.
#Step 2: using the same method create a list for the keys.
#Step 3: use a dict comprehension to convert both lists into a single dictionary.
hurricane_value_list = list(zip(names, months, years, max_sustained_winds, areas_affected, damages, deaths))
hurricane_key_val_list = list(zip(names, hurricane_value_list))
hurricane_dict = {key: value for key, value in hurricane_key_val_list}
print(hurricane_dict["Carol"])
# 3 Organizing by Year
# Step 1: Similar to above we will want to create a new list that we will convert via dict comprehension. This time, use 'years' as the first variable in your zipped list.
hurricane_y_v_list = list(zip(years, hurricane_value_list))
hurricane_dict_update = {key: value for key, value in hurricane_y_v_list}
print(hurricane_dict_update)
# 4 Counting Damaged Areas
#Step 1: start by creating an empty dictionary which you will populate.
#Step 2: create a nested for loop that will check each area. If the area is not in the dictionary, then the function will add a count of 1. If it is in the dictionary, it will add 1 to the current count.
affected_area_count = {}
for areas in areas_affected:
for area in areas:
if area not in affected_area_count:
affected_area_count[area] = 1
else:
affected_area_count[area] +=1
print(affected_area_count)
# 5 Calculating Maximum Hurricane Count
#Step 1: Start by defining a new variable for maximum area and the count of how many times the area was affected.
# Step 2: create a for loop that will check each area, using the dictionary you created in the previous problem, against another and find which is largest and populate the variable defined in step 1 with that areas name.
max_area = "Central America"
max_area_count = 0
for area in affected_area_count:
if affected_area_count[area]>=max_area_count:
max_area = area
max_area_count = affected_area_count[area]
print("The most affected area is "+str(max_area)+". It was impacted "+str(max_area_count)+" times.")
# 6 Calculating the Deadliest Hurricane
# Step 1: Create a new list and dictionary of hurricanes and deaths.
#Step 2: Follow similar logic to the previous problem, but this time using your new dictionary from step 1.
zipped_death_count = list(zip(names, deaths))
death_count = {key: value for key, value in zipped_death_count}
max_death_name = ""
max_death = 0
for name in death_count:
if death_count[name] > max_death:
max_death = death_count[name]
max_death_name = name
print("The deadliest hurricane was "+str(max_death_name)+". It caused "+str(max_death)+" deaths.")
#7 mortality
#Step 1: create a dictionary with key values from the mortality scale dictionary. You will populate this with your function.
#Step 2: create a for loop that will check each hurricane, using the dictionary you created in the previous problem, and assess which mortality scale it fits into then append it to the correct key in the dictionary.
mortality_scale = {0: 0,
1: 100,
2: 500,
3: 1000,
4: 10000}
mortality_rating = {0: [], 1: [], 2: [], 3: [], 4: []}
for name in death_count:
if mortality_scale[0] == death_count[name]:
mortality_rating[0].append(name)
elif mortality_scale[0] < death_count[name] <= mortality_scale[1]:
mortality_rating[1].append(name)
elif mortality_scale[1] < death_count[name] <= mortality_scale[2]:
mortality_rating[2].append(name)
elif mortality_scale[2] < death_count[name] <= mortality_scale[3]:
mortality_rating[3].append(name)
elif mortality_scale[3] < death_count[name] <= mortality_scale[4]:
mortality_rating[4].append(name)
else:
mortality_rating[4].append(name)
print(mortality_rating)
#8 Damage rating
#Step 1: create a new list and dictionary for damages.
#Step 2: follow the same logic as above (problem 6) to compare each hurricane against the other to find the max and populate that variable.
zipped_damages_count = list(zip(names, updated_damages))
damages_count = {key: value for key, value in zipped_damages_count}
max_damage_name = ""
max_damage = 0
for name in damages_count:
if damages_count[name] == "Damages not recorded":
continue
if damages_count[name] > max_damage:
max_damage = damages_count[name]
max_damage_name = name
print("The costliest hurricane was "+str(max_damage_name)+". It caused $"+str(max_damage)+" dollars in damages.")
#9 Damage rating
#Follow the same steps as problem 7 above.
damage_scale = {0: 0,
1: 100000000,
2: 1000000000,
3: 10000000000,
4: 50000000000}
damage_rating = {0: [], 1: [], 2: [], 3: [], 4: []}
for name in damages_count:
if damages_count[name] == "Damages not recorded":
damage_rating[0].append(name)
elif damage_scale[0] < damages_count[name] <= damage_scale[1]:
damage_rating[1].append(name)
elif damage_scale[1] < damages_count[name] <= damage_scale[2]:
damage_rating[2].append(name)
elif damage_scale[2] < damages_count[name] <= damage_scale[3]:
damage_rating[3].append(name)
elif damage_scale[3] < damages_count[name] <= damage_scale[4]:
damage_rating[4].append(name)
else:
damage_rating[4].append(name)
print(damage_rating)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment