Hurricane Analysis Challenge Project (Python)

The idea of

if damage.find('M') != -1:

is to check whether 'M' is a character in the string,
.find('M') would be the index of 'M' if 'M' is in the string, otherwise it would be -1.
so damage.find('M') != -1 would be True if ‘M’ is in the damage string,
otherwise it would be False,
so
if damage.find('M') != -1:
does something only if ‘M’ is in the string stored in the damage variable.

However, @mtf is correct, we don’t actually have need to check whether there’s an 'M' anywhere in the damage string;
we only need to check whether 'M' is the last character of the string (using damage[-1] == 'M')

1 Like

Here is how I did it. The first few of these were really the most challenging:

# 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
conversion = {"M": 1000000,
              "B": 1000000000}

# test function by updating damages
def floating_damage(damages, conversion): 
  numerical_damages = []
  for number in damages:
    if number == "Damages not recorded":
      numerical_damages.append(number)
    elif (number.find("M") != -1):
      numerical_damages.append((float(number[:-1])*conversion["M"]))
    elif (number.find("B") != -1):
      numerical_damages.append((float(number[:-1])*conversion["B"]))
  return numerical_damages

numerical_damages = floating_damage(damages, conversion)
# 2 
# Create a Table

# Create and view the hurricanes dictionary
def hurricanes_dict(names, months, years, areas_affected, numerical_damages, deaths):
  hurricanes = {}
  for i in range(0, len(names)):
    hurricanes.update({
      names[i]:	{"Name": names[i],	"Month":months[i],	"Year": years[i],	"Areas Affected": areas_affected[i],	"Damage": numerical_damages[i], "Deaths" : deaths[i]}
    })

  return hurricanes

hurricanes = hurricanes_dict(names, months, years, areas_affected, numerical_damages, deaths)
#print(hurricanes)
# 3
# Organizing by Year

# create a new dictionary of hurricanes with year and key
def hurricanes_year(hurricanes):
  hurricanes_year = {}
  for hurricane in hurricanes.items():
    current_hurricane = hurricane[1]
    current_year = hurricane[1]["Year"]
    if current_year not in hurricanes_year:
      hurricanes_year[current_year] = [current_hurricane]

    elif current_year in hurricanes_year:
      hurricanes_year[current_year].append(current_hurricane)

  return hurricanes_year


# 4
# Counting Damaged Areas

# create dictionary of areas to store the number of hurricanes involved in
def area_count(hurricanes):
  area_count = {}
  #loop through the dictionary, and then the "Area Affected" key and add "Area" to a new dictionary
  for hurricane in hurricanes.items():
    for area in hurricane[1]["Areas Affected"]:
      current_area = area
      #print(current_area)
    #Update the Value with a count for Area each time it appears
      if current_area not in area_count:
        area_count[current_area] = 1
      elif current_area in area_count:
        area_count[current_area] += 1
  #print(area_count)
  return area_count

area_total = area_count(hurricanes)

# 5 
# Calculating Maximum Hurricane Count

# find most frequently affected area and the number of hurricanes involved in
def find_frequency(area_total):
  #iterate through the count and save the current area to a variable and current highest to a variable
  current_highest = 1
  for area in area_total.items():
    current_area = area[0]
    current_value = area[1]
    #move to next object
    #check if current variable > highest variable
    if current_value > current_highest:
      current_highest = current_value
      highest_area = current_area
      print("The current highest area is: " + highest_area + " with a occurence of " + str(current_highest))
      #if so, highest variable = highest variable 
  return highest_area, current_highest
print(find_frequency(area_total))
# 6
# Calculating the Deadliest Hurricane

# find highest mortality hurricane and the number of deaths
def find_deadliest(hurricanes):
  #Iterate through each hurricane to grab the name and current deaths
  highest_deathtoll = 1
  for hurricane in hurricanes.items():
    current_hurricane = hurricane[0]
    current_deaths = hurricane[1]["Deaths"]
    #move to next object
    #check if current_deaths > highest highest_deathtoll
    if current_deaths > highest_deathtoll:
      highest_deathtoll = current_deaths
      deadliest_hurricane = current_hurricane
      print("The current deadliest hurricane is: " + current_hurricane + " with a death toll of " + str(highest_deathtoll))
      #if so, highest variable = highest variable 
  return deadliest_hurricane, highest_deathtoll

find_deadliest(hurricanes)

# 7
# Rating Hurricanes by Mortality
mortality_scale = {0: 0,
                   1: 100,
                   2: 500,
                   3: 1000,
                   4: 10000}



# categorize hurricanes in new dictionary with mortality severity as key
def mortality(hurricanes):
  mortality_dict  = {0: [],
                    1: [],
                    2: [],
                    3: [],
                    4: [],
                    5: []}
  for hurricane in hurricanes.items():
    current_hurricane = hurricane[0]
    current_deaths = hurricane[1]["Deaths"]
    if mortality_scale[0] == current_deaths:
      mortality_dict[0].append(current_hurricane)    
    elif mortality_scale[0] < current_deaths <= mortality_scale[1]:
      mortality_dict[1].append(current_hurricane)
    elif mortality_scale[1] < current_deaths <= mortality_scale[2]:
      mortality_dict[2].append(current_hurricane)
    elif mortality_scale[2] < current_deaths <= mortality_scale[3]:
      mortality_dict[3].append(current_hurricane)
    elif mortality_scale[3] < current_deaths <= mortality_scale[4]:
      mortality_dict[4].append(current_hurricane)
    else:
      mortality_dict[5].append(current_hurricane)

  return mortality_dict

print(mortality(hurricanes))


# 8 Calculating Hurricane Maximum Damage

# find highest damage inducing hurricane and its total cost
def find_damagiest(hurricanes):
  #Iterate through each hurricane to grab the name and current cost
  highest_damage = 1.0
  for hurricane in hurricanes.items():
    current_hurricane = hurricane[0]
    current_damage = hurricane[1]["Damage"]
    #move to next object
    #check if current_damage > highest_damage
    if current_damage != "Damages not recorded" and current_damage > highest_damage:
      highest_damage = current_damage
      damagiest_hurricane = current_hurricane
      print("The current most damaging hurricane is: " + current_hurricane + " with a total damage cost of " + str(highest_damage))
      #if so, highest variable = highest variable 
  return damagiest_hurricane, highest_damage

find_damagiest(hurricanes)

# 9
# 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 damage_meter(hurricanes):
  damage_dict  = {0: [],
                    1: [],
                    2: [],
                    3: [],
                    4: [],
                    5: []}
  for hurricane in hurricanes.items():
    current_hurricane = hurricane[0]
    current_damages = hurricane[1]["Damage"]
    if "Damages not recorded" == current_damages:
      damage_dict[0].append(current_hurricane)    
    elif damage_scale[0] < current_damages <= damage_scale[1]:
      damage_dict[1].append(current_hurricane)
    elif damage_scale[1] < current_damages <= damage_scale[2]:
      damage_dict[2].append(current_hurricane)
    elif damage_scale[2] < current_damages <= damage_scale[3]:
      damage_dict[3].append(current_hurricane)
    elif damage_scale[3] < current_damages <= damage_scale[4]:
      damage_dict[4].append(current_hurricane)
    else:
      damage_dict[5].append(current_hurricane)

  return damage_dict

print(damage_meter(hurricanes))

My code for this project. Very challenging and interesting, learnt a lot.

Wow! This was a head-scratcher!
But a lot of fun too!

If anyone would like to review my code (I kept lots of comments so it’ll be easier) that’d be great!

note: on task 6 I actually returned the full sorted dictionary because I need to learn how to read the requirements better.

link to gist

My attempt at the Hurricane Challenge.

Any comments would be great.

Here are my thoughts coded guys, please criticize.

Here is my solution - feedback is welcome!: data_science_projects/hurricanes.py at main · glapon/data_science_projects · GitHub

feedback is welcome!

https://github.com/Alireza84/shared_public/blob/378e2a8bbc6c5184a5ac63c460ddd84e388d9644/CodeAcad_Challenge.ipynb

I struggled alot on some parts, but the last part was easier (also didn’t do exactly as the instructions said sometimes and tried out some different solutions).

Hi, this is my hurricane analysis challenge project that I did around Christmas.
I struggled with this but once I got going I enjoyed doing it. I probably spent around a week doing it but a bit chunk of that was trying to work out why some of my code wasn’t working as expected - indents. I thought that if there was a problem with your indentation the code wouldn’t run. But, I had tried to return a value at an earlier step than intended, and it wasn’t completing the loop after the first record. At least this challenge taught me about indents!

I would appreciate any feedback.

Codecademy/hurrican analysis tasks.ipynb at main · captain-81/Codecademy (github.com)