Hello, this is my code for this Project, any feedback would be great
Hello, below is a link to my code for this project. Trying to learn how to use GitHub properly, but the link provided brings me to 404 Page Not Found error. I did download and complete the project on Jupyter Notebook as well. Any feedback/help would be great, thank you.
Here is my code on GitHub! Any review comments are much appreciated!
Here is my solution! CodeAcademy/Hurricane Analysis Complete .ipynb at main · alparison/CodeAcademy · GitHub
hi folks here’s my hurrican analysis project code
Hi cssninja52482,
I noticed your message on my email was posted quite a few hours ago so maybe you have already arrived at an answer. But in case you might still need it, here is my explanation for the code:
Since the values inside the list areas_affected
were also grouped into a list, this prevents us from getting the count the individual elements (which are the areas).
Creating an empty dictionary would allow you to store the name of the areas as the key and its count as the value.
The areas_count is originally an empty variable, but once we perform a nested loop (w/c is a loop inside a loop), we can then access the individual elements that are inside the variable areas_affected (w/c is a list within a list). We can then count how many times the area was affected by the typhoon using the if statement.
Example:
empty_dictionary = { }
For the list of areas that are inside the areas_affected variable:
For the areas that are inside the list of areas (e.g. Central America):
If Central America was already added in the empty_dictionary as the key:
add 1 to the value of Central America
else:
add Central America to the dictionary (w/c will be the key) and assign a value of 1
A dictionary should have a key-value pair, which means that once we perform a nested loop and an if statement, we are already adding key-value pairs to the empty dictionary. Remember that key are unique identifiers which means that python would only put the distinct area names as keys. If the program encounters the same area name, it will only add to the count of the are instead.
Output:
I hope this helps!
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
updated_damages =
def convert(damages_data):
for i in damages_data:
if i[-1] == “M”:
updated_damages.append(float(i[:-1]) * conversion[“M”])
if i[-1] == “B”:
updated_damages.append(float(i[:-1]) * conversion[“B”])
if i == “Damages not recorded”:
updated_damages.append(i)
return updated_damages
updated_damages = convert(damages)
#print(updated_damages)
2
Create a Table
def organize(names, months, years, max_sustained_winds, areas_affected, damage, deaths):
hurricans = {}
num = len(names)
for i in range(num):
hurricans[names[i]] = {
“Name”: names[i],
“Month”: months[i],
“Year”: years[i],
“Max Sustained Wind”: max_sustained_winds[i],
“Areas Affected”: areas_affected[i],
“Damage”: damage[i],
“Deaths”: deaths[i]
}
return hurricans
Create and view the hurricanes dictionary
hurricanes_dictionary = organize(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)
#print(hurricanes_dictionary)
3
Organizing by Year
def organize_by_year(dictionary):
year_dict = {}
year_list =
for hurricane in dictionary:
current_year = dictionary[hurricane][“Year”]
current_cane = dictionary[hurricane]
if current_year not in year_dict:
year_dict[current_year] = [current_cane]
else:
year_dict[current_year].append(current_cane)
return year_dict
#print(organize_by_year(hurricanes_dictionary))
create a new dictionary of hurricanes with year and key
year_dictionary = organize_by_year(hurricanes_dictionary)
#print(year_dictionary)
4
Counting Damaged Areas
def count_areas(dictionary):
areas = {}
for hurricane in dictionary.values():
areas_list = hurricane[“Areas Affected”]
for area in areas_list:
if area not in areas:
areas[area] = 1
else:
areas[area] += 1
return areas
create dictionary of areas to store the number of hurricanes involved in
areas_dictionary = count_areas(hurricanes_dictionary)
#print(areas_dictionary)
5
Calculating Maximum Hurricane Count
def max_hits(dictionary):
max_area = ‘Central America’
max_area_count = 0
for area in areas_dictionary:
if max_area_count < areas_dictionary[area]:
max_area = area
max_area_count += 1
return ("area " + max_area + " got hit " + str(max_area_count))
#print(max_hits(areas_dictionary))
find most frequently affected area and the number of hurricanes involved in
most_affected = max_hits(areas_dictionary)
6
Calculating the Deadliest Hurricane
def deadliest(dictionary):
max_death_name = ‘’
max_death_count = 0
for hur in dictionary.values():
if hur[“Deaths”] > max_death_count:
max_death_name = hur[“Name”]
max_death_count = hur[“Deaths”]
return (max_death_name + " is deadliest. count: " + str(max_death_count))
#print(deadliest(hurricanes_dictionary))
find highest mortality hurricane and the number of deaths
deadliest_hur = deadliest(hurricanes_dictionary)
7
Rating Hurricanes by Mortality
def hur_by_mor(dictionary):
hurricanes_by_mortality = {0:,1:,2:,3:,4:,5:}
for hur in dictionary.values():
if hur[“Deaths”] < 100:
hurricanes_by_mortality[0].append(hur)
if hur[“Deaths”] >= 100 and hur[“Deaths”] < 500:
hurricanes_by_mortality[1].append(hur)
if hur[“Deaths”] >= 500 and hur[“Deaths”] < 1000:
hurricanes_by_mortality[2].append(hur)
if hur[“Deaths”] >= 1000 and hur[“Deaths”] < 10000:
hurricanes_by_mortality[3].append(hur)
if hur[“Deaths”] > 100000:
hurricanes_by_mortality[4].append(hur)
return hurricanes_by_mortality
#print(hur_by_mor(hurricanes_dictionary))
categorize hurricanes in new dictionary with mortality severity as key
by_mor = hur_by_mor(hurricanes_dictionary)
8 Calculating Hurricane Maximum Damage
def most_damage(dictionary):
max_damage_name = ‘’
max_cost_count = 0
for hur in dictionary.values():
if hur[“Damage”] == “Damages not recorded”:
max_cost_count = 0
elif hur[“Damage”] > max_cost_count:
max_damage_name = hur[“Name”]
max_cost_count = hur[“Damage”]
return (max_damage_name + " costs: " + str(max_cost_count))
#print(most_damage(hurricanes_dictionary))
find highest damage inducing hurricane and its total cost
damage_dictionary = most_damage(hurricanes_dictionary)
9
Rating Hurricanes by Damage
def hur_by_damage(dictionary):
hurricanes_by_damage = {0:,1:,2:,3:,4:}
for hur in dictionary.values():
if hur[“Damage”] == “Damages not recorded”:
hurricanes_by_damage[0].append(hur)
elif hur[“Damage”] < 100000000:
hurricanes_by_damage[0].append(hur)
elif hur[“Damage”] >= 100000000 and hur[“Damage”] < 1000000000:
hurricanes_by_damage[1].append(hur)
elif hur[“Damage”] >= 1000000000 and hur[“Damage”] < 10000000000:
hurricanes_by_damage[2].append(hur)
elif hur[“Damage”] >= 10000000000 and hur[“Damage”] < 50000000000:
hurricanes_by_damage[3].append(hur)
elif hur[“Damage”] > 50000000000:
hurricanes_by_damage[4].append(hur)
return hurricanes_by_damage
print(hur_by_damage(hurricanes_dictionary))
categorize hurricanes in new dictionary with damage severity as key
damage_dictionary = hur_by_damage(hurricanes_dictionary)
Hi guys, Here is my solution, I know my coding style is a bit weird so I commented as much as I could so its easier to follow.
Any feedback is appreciated, hope everybody is doing well!
Hi!
I have a question on the solution for the hurricane analysis project. In line 16 <if damage.find(‘M’) != -1:> how would you translate this? Specifically what is the -1 referring to?
~Thanks
One might suspect the minus 1 refers to, ‘not found’?
Could <if damage.find(‘M’) != -1:> be read as. If it is True/False that the letter M is not found? Is this a boolean?
I appreciate it man!
In that expression we are asking if ‘M’ is not not found. In other words it is found if the yield is, True
. Any expression that boils down to truthiness is a boolean expression, which is tantamount to being a boolean in every way except primitive, True
or False
.
All comparisons are boolean.
Ahhh! Thank You Good Sir!
Merry Christmas bud!
P.S. Are you on a discord?
I’m not on the CC server as it is too busy for an old fart like me. You can find me on the Project Nu server with the alias, mtf
.
I’m not sure how I would go about doing that. I did have a question on a solution for a medical insurance project on codecademy. If you care to take a look. Here is the link to the Jupyter notebook.
My question is in the 5th section where classes are defined. I’m confused on the first parameter (self) of that first function. I’m also a little confused on the purpose of that first function. Here is a screenshot…
You may wish to put this project on hold and dive into the Learn Python 3 track where objects and classes are introduced.
If you remember (or know) anything from JavaScript’s custom objects, a class is a template for new instances to be created under that class name. They will all share the same properties (attributes) but have uniquely assigned values in each instance.
class Foo:
pass
foo = Foo()
That’s the simplest we can write a class. Despite the fact nothing is created or defined in the class definition, foo
is indeed a new instance (object) of that class, per the above statement.
print (type(foo))
# <class 'Foo'>
The first method in a class is usually the constructor wherein all the parameters are assigned to the instance variables of the object. That is where self
comes in. It is the variable that tracks the current context. It is an object to which attributes are given in the __init__()
method. Without that context we would just be defining globals (per se, but not really, it will raise an exception, or at best, nothing will happen).
class Foo:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
foo = Foo(5, 7, 11)
print (foo.a) # 5
print (foo.b) # 7
print (foo.c) # 11
lol yes that project was ahead of what I have learned so far in Codecademy. Couldn’t make sense out of that first function. I watched a couple of YouTube videos and as I understand it. That first function is a method of that class. Self is the instance of that function and than the arguments. So that particular function is special in that regard.
I don’t have any prior skills in other languages. Would there be a book or video you would recommend to better understand some of definitions often used in describing code?
Everything I learned so far about Python (and it is not much on the grand scale) has been online. If you have a local library nearby, go through the books they have and see if any authors/publishers stand out that will be worth your investment. Before you buy books, check out their reviews so you know you’re getting something worthwhile for your outlay.
There are some notable sites, but that will take some rooting around on the web with search. Be sure you have visited and bookmarked the main Python sites with documentation and wikis, working examples, advice, etc.
On the whole, it is well to understand what an object is in Python. Essentially everything comes down to an object of one form or another. That comes down to type, since it is that type that determines which class of object it is, and therefore which methods it can draw upon.
FTR, Methods and Functions are sort of the same thing, but not completely the same given they are accessed differently.
A function exists in the parent namespace, global for instance. The function is called with a given argument which falls to the parameter in local scope. We need to supply the object to the function.
On the other hand, and this is where the term polymorphism comes in, as I understand it, a method already has an object, or context which is referred to locally as self
. We know this to be the instance object not just at its instantiation, but for the purposes of any method calls made by this or any other instance. The instance gives execution context, and the method accesses variables on that instance, and other methods of the same class. Essentially, the instance calls methods on itself.
I still recommend pausing this project and getting caught up with the basic and intermediate skill and comprehension level. It will save you a lot of confusion and hair pulling.