Hurricane Analysis Challenge Project (Python)

My Codes

I did it in a more efficient way.
Since some functions are very similar, I just did one and set the category, such as “Deaths” or “Damages” as parameter.

# 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]


# write your update damages function here:
def update_list_damages(damages_list):
    for index, damage in enumerate(damages_list):
        if damage.endswith("M"):
            damages_list[index] = float(damage[:-1]) * 1000000
        if damage.endswith("B"):
            damages_list[index] = float(damage[:-1]) * 1000000000


update_list_damages(damages)


# write your construct hurricane dictionary function here:
def generate_dictionary(names_list,
                        months_list,
                        years_list,
                        max_sustained_winds_list,
                        areas_affected_list,
                        damages_list,
                        deaths_list):
    dictionary = {}
    for index in range(len(names_list)):
        dictionary[names_list[index]] = {
            "Name": names_list[index],
            "Month": months_list[index],
            "Year": years_list[index],
            "Max Sustained Wind": max_sustained_winds_list[index],
            "Areas Affected": areas_affected_list[index],
            "Damages": damages_list[index],
            "Deaths": deaths_list[index]
        }
    return dictionary


hurricanes_data = generate_dictionary(names, months, years, max_sustained_winds, areas_affected, damages, deaths)


# write your construct hurricane by year dictionary function here:
def set_dictionary_keys_by_year(hurricanes_dictionary):
    new_dictionary = {}
    for key, value in hurricanes_dictionary.items():
        year = hurricanes_dictionary[key]["Year"]
        if not new_dictionary.get(year):
            new_dictionary[year] = [value]
        else:
            new_dictionary[year].append(value)
    return new_dictionary


hurricanes_data_by_years = set_dictionary_keys_by_year(hurricanes_data)


# write your count affected areas function here:
def find_most_affected_area(areas_affected_list):
    affliction_count = {}
    for areas in areas_affected_list:
        for area in areas:
            if affliction_count.get(area):
                affliction_count[area] += 1
                continue
            affliction_count[area] = 1
    most_affected_area = max(affliction_count, key=affliction_count.get)
    return most_affected_area, affliction_count[most_affected_area]


# write your find most affected area function here:
max_area, max_area_count = find_most_affected_area(areas_affected)
print(f"The most affected area is {max_area} with {max_area_count} hurricanes.")


# Find the highest value of a specified category:
def find_highest_number_of(hurricanes_dictionary, category):
    highest_number = 0
    hurricane = str()
    for hurricane_name, hurricane_data in hurricanes_dictionary.items():
        category_count = hurricane_data[category]
        if (isinstance(category_count, float) or isinstance(category_count, int)) and category_count > highest_number:
            highest_number = category_count
            hurricane = hurricane_name
    return hurricane, highest_number


# write your greatest number of deaths function here:
max_mortality_cane, max_mortality = find_highest_number_of(hurricanes_data, "Deaths")
print(f"The deadliest hurricane is {max_mortality_cane} with {max_mortality} deaths.")


# Function to categorize in 5 scales:
def categorize_data_five_scales(hurricanes_dictionary, category, *scales):
    scales_dictionary = {0: [], 1: [], 2: [], 3: [], 4: [], 5: []}
    for hurricane_data in hurricanes_dictionary.values():
        category_count = hurricane_data[category]
        if isinstance(category_count, float) or isinstance(category_count, int):
            if category_count > scales[4]:
                scales_dictionary[5].append(hurricane_data)
            elif category_count > scales[3]:
                scales_dictionary[4].append(hurricane_data)
            elif category_count > scales[2]:
                scales_dictionary[3].append(hurricane_data)
            elif category_count > scales[1]:
                scales_dictionary[2].append(hurricane_data)
            elif category_count > scales[0]:
                scales_dictionary[1].append(hurricane_data)
            else:
                scales_dictionary[0].append(hurricane_data)
    return scales_dictionary


# write your categorize by mortality function here:
hurricanes_by_mortality = categorize_data_five_scales(hurricanes_data, "Deaths",
                                                      0, 100, 500, 1000, 10000)

# write your greatest damage function here:
max_damage_cane, max_damage = find_highest_number_of(hurricanes_data, "Damages")
print(f"The most expensive hurricane is {max_damage_cane} with {max_damage}$ worth of damages.")

# write your categorize by damage function here:
hurricanes_by_damage = categorize_data_five_scales(hurricanes_data, "Damages",
                                                   0, 100000000, 1000000000, 10000000000, 50000000000)

4 Likes

This was a fun and challenging project with a lot of good practice iterating over dictionaries. I enjoyed this one.

Here’s my code. I welcome any feedback or suggestions. I found a lambda function on SO to handle some of the ‘find maximum’ functions and it seems to work pretty well.

1 Like

Here’s my solution. It’s a bit rustic, but it works:

Here is my code. This was an excellent challenge to review list and dictionary concepts!

https://github.com/wanjapm/hurricane_analysis/blob/master/script.py

I think my version of updated_damages() may be a bit neater than in the solution. Please see below:

def update_damages(damages):
    updated_damages=[]
    conversion = {"M": 1000000,
              "B": 1000000000}
    for d in damages:
        converted=False
        for conv_key in conversion:
            if d.find(conv_key) > 0:
                updated_damages.append(float(d.replace(conv_key,''))*conversion[conv_key])
                converted = True
        if not converted: updated_damages.append(d)
    return updated_damages

Here’s mine! It was pretty daunting to look at, at first. But doing a step at a time truly is remarkable.

[https://github.com/BJRomPal/hurricanes/blob/master/hurricans.py]

This is my code. I think it could be better. If someone has any suggestiones, it would be welcome.
Thank you.

1 Like

Completed this project! Here is my code:

1 Like

Just finished! Was pleased to be able to reuse some of my functions instead of having to write completely new ones for ALL the challenges :slight_smile:
However, I did do a little copy/pasting and slight tweaking with if statements for the damage ones later on… idk if there is a way to update functions subtly like that without putting them into a class, that would be cool though haha

1 Like

Here is my solution

Here is my completed code for this project. For the one with the hurricane years, more specifically the 2005 hurricanes, the information for the hurricanes came out like this:

[[[{‘Name’: ‘Emily’, ‘Month’: ‘July’, ‘Year’: 2005, ‘Max Sustained Wind’: 160, ‘Areas Affected’: [‘Windward Islands’, ‘Jamaica’, ‘Mexico’, ‘Texas’], ‘Damage’: 1010000000.0, ‘Deaths’: 17}, {‘Name’: ‘Katrina’, ‘Month’: ‘August’, ‘Year’: 2005, ‘Max Sustained Wind’: 175, ‘Areas Affected’: [‘Bahamas’, ‘United States Gulf Coast’], ‘Damage’: 125000000000.0, ‘Deaths’: 1836}], {‘Name’: ‘Rita’, ‘Month’: ‘September’, ‘Year’: 2005, ‘Max Sustained Wind’: 180, ‘Areas Affected’: [‘Cuba’, ‘United States Gulf Coast’], ‘Damage’: 12000000000.0, ‘Deaths’: 125}], {‘Name’: ‘Wilma’, ‘Month’: ‘October’, ‘Year’: 2005, ‘Max Sustained Wind’: 185, ‘Areas Affected’: [‘Greater Antilles’, ‘Central America’, ‘Florida’], ‘Damage’: 29400000000.0, ‘Deaths’: 87}]

I know the problem is that I’m appending a list into a list repeatedly, but any time I tried to fix the code, it would come out very differently. Any ideas on how to fix this?

I’ve got here from the “This Is Jeopardy!” Project Link! Hope it gets fixed :smiley:

Had to do this challenge anyway :laughing:

append just the object! You don’t put them in the multiple_hurricanes = , your issue is just figuring out how to do all that a bit cleaner. I think you could check my code!

Thank you for your response! Your project helped me out by making me realize I had to use a “for” loop, but because we both did this step of the project differently, I had to fix it differently than what you suggested.

I ended up fixing my code like this:

I added an “if else” based on the length of what’s in the dictionary for the certain year

By doing this, the computer can now realize if a certain year has had more than one hurricane that occured. If it does, it breaks it up in the for loop and re-appends it into one list. If it doesn’t, it skips the “for” loop and appends it normally. This got me to get the correct result of:

[{‘Name’: ‘Emily’, ‘Month’: ‘July’, ‘Year’: 2005, ‘Max Sustained Wind’: 160, ‘Areas Affected’: [‘Windward Islands’, ‘Jamaica’, ‘Mexico’, ‘Texas’], ‘Damage’: 1010000000.0, ‘Deaths’: 17}, {‘Name’: ‘Katrina’, ‘Month’: ‘August’, ‘Year’: 2005, ‘Max Sustained Wind’: 175, ‘Areas Affected’: [‘Bahamas’, ‘United States Gulf Coast’], ‘Damage’: 125000000000.0, ‘Deaths’: 1836}, {‘Name’: ‘Rita’, ‘Month’: ‘September’, ‘Year’: 2005, ‘Max Sustained Wind’: 180, ‘Areas Affected’: [‘Cuba’, ‘United States Gulf Coast’], ‘Damage’: 12000000000.0, ‘Deaths’: 125}, {‘Name’: ‘Wilma’, ‘Month’: ‘October’, ‘Year’: 2005, ‘Max Sustained Wind’: 185, ‘Areas Affected’: [‘Greater Antilles’, ‘Central America’, ‘Florida’], ‘Damage’: 29400000000.0, ‘Deaths’: 87}]

Thanks for your help!

Niice! just want to know, why

if len(hurricane_years[year]) == 7:

try not hard coding 7 so you can make this code flexible!

I did make one small edit to the code to make it no longer using the if len(hurricane_years[year]) == 7: line:

if count == 0:

This sort of serves as a “second time through” detector, because for some reason if the dictionary goes through the else statement first, the code breaks. After the dictionary goes through the “if” statement once, it moves on to the else statement and produces the same result as before without hard coding 7. I tested this by changing some of the years at the very top and the code still functions the same way, even with 10 hurricanes having the same year.

Here is the full code:

Once again thanks for all of your help!


Hello! Here’s my code!

My solution to this!

[https://gist.github.com/9d5f0de650dcb3020de6b4119e9a84bf](http://Code Here!)

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 category 5 hurricanes (wind speeds ≥ 157 mph (252 km/h ))
# "Prefix-B/M", where B stands for billions (1,000,000,000) and M stands for millions (1,000,000).
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():
        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()


1 Like