Hurrican Project Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

11/9/2020 Codecademy export

Please verify your email address to access all of GitHub’s features. Configure email settings
An email containing verification instructions was sent to
shobha000123@gmail.com.

codecademydev / script.py Secret

Created 9 months ago • Report abuse

Star

Code Revisions 1 Forks 1

Codecademy export

script.py

1 # names of hurricanes
2 names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II', 'CubaBrownsville', 'Tampico',
3
4 # months of hurricanes
5 months = ['October', 'September', 'September', 'November', 'August', 'September', 'September', 'Se
6
7 # years of hurricanes
8 years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971,
9
10 # maximum sustained winds (mph) of hurricanes
11 max_sustained_winds = [165, 160, 160, 175, 160, 160, 185, 160, 160, 175, 175, 160, 160, 175, 160,
12
13 # areas affected by each hurricane
14 areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antill
15
16 # damages (USD($)) of hurricanes
17 damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages
18
19 # deaths for each hurricane
20 deaths = [90,4000,16,3103,179,184,408,682,5,1023,43,319,688,259,37,11,2068,269,318,107,65,19325,51
21
22
23 # Convert damages data from string to float and return converted data as a list.
24
25 def convert_damages_data(damages):
26
27 conversion = {"M": 1000000, "B": 1000000000}

28 updated_damages = []
29

https://gist.github.com/codecademydev/8b66e78dab271ee7291588dcde8fb4aa 1/5
11/9/2020 Codecademy export
30 for damage in damages:
31 if damage == "Damages not recorded":
32 updated_damages.append(damage)
33 if damage[-1] == 'M':
34 updated_damages.append(float(damage.strip('M'))*conversion["M"])
35 if damage[-1] == 'B':
36 updated_damages.append(float(damage.strip('B'))*conversion["B"])
37
38 return updated_damages
39
40 updated_damages = convert_damages_data(damages)
41 #print(updated_damages)
42
43
44 #Create dictionary of hurricanes with hurricane name as the key and a dictionary of hurricane data
45
46 def create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_damages,
47
48 hurricanes = {}
49 num_hurricanes = len(names)
50
51 for i in range(num_hurricanes):
52 hurricanes[names[i]] = {"Name": names[i],
53 "Month": months[i],
54 "Year": years[i],
55 "Max Sustained Wind": max_sustained_winds[i],
56 "Areas Affected": areas_affected[i],
57 "Damage": updated_damages[i],
58 "Deaths": deaths[i]}
59 return hurricanes
60
61 hurricanes = create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_
62 #print(hurricanes)
63
64 #Convert dictionary with hurricane name as key to a new dictionary with hurricane year as the key
65 # I tried something different from the hurricanes dictionary.
66
67 years_dict= []
68
69 for n, m, y, ma, a, da, z in zip(names, months, years, max_sustained_winds, areas_affected, updat
70 dict = {y: {'Name': n, 'Month':m, 'Year': y, 'Max_sustained_wind': ma, 'Area_affected': a, 'Da
71 years_dict.append(dict)
72
73 #print(years_dict)
74
75 #Find the count of affected areas across all hurricanes and return as a dictionary with the affect
76
77 areas_count={}
78

https://gist.github.com/codecademydev/8b66e78dab271ee7291588dcde8fb4aa 2/5
11/9/2020 Codecademy export
79 for areas in areas_affected:
80 for i in areas:
81 if i not in areas_count:
82 areas_count[i] = 1
83 else:
84 areas_count[i] += 1
85
86 #print(areas_count)
87
88 #Find most affected area and the number of hurricanes it was involved in.
89
90 def max_areas_affected(areas_count):
91
92 max_area = ''
93 max_count = 0
94
95 for area in areas_count:
96 if areas_count[area] > max_count:
97 max_area = area
98 max_count = areas_count[area]
99 return max_area, max_count
100
101 max_area, max_count = max_areas_affected(areas_count)
102 #print(max_area, max_count)
103
104 #Find the highest mortality hurricane and the number of deaths it caused.
105
106 def fatality(hurricanes):
107
108 hurricane_most_deaths=''
109 number_of_deaths=0
110
111 for hurricane in hurricanes:
112 if hurricanes[hurricane]['Deaths'] > number_of_deaths:
113 hurricane_most_deaths = hurricane
114 number_of_deaths = hurricanes[hurricane]['Deaths']
115 return number_of_deaths, hurricane_most_deaths
116
117 most_deaths, number_of_deaths = fatality(hurricanes)
118 #print(most_deaths, number_of_deaths)
119
120 #Categorize hurricanes by mortality rates and return a dictionary.
121
122 def mortality(hurricanes):
123
124 mortality_rates = {0:[], 1:[], 2:[], 3:[], 4:[]}
125
126 for hurricane in hurricanes:
127

https://gist.github.com/codecademydev/8b66e78dab271ee7291588dcde8fb4aa 3/5
11/9/2020 Codecademy export
128 rate = 0
129 deaths = hurricanes[hurricane]['Deaths']
130
131 if deaths < 100:
132 rate = 0
133 elif deaths >= 100 and deaths < 500:
134 rate = 1
135 elif deaths >= 500 and deaths < 1000:
136 rate = 2
137 elif deaths >= 1000 and deaths < 10000:
138 rate = 3
139 else:
140 rate = 4
141
142 if rate not in mortality_rates:
143 mortality_rates[rate] = hurricanes[hurricane]
144 else:
145 mortality_rates[rate].append(hurricanes[hurricane])
146
147 return mortality_rates
148
149 mortality_rates = mortality(hurricanes)
150 #print(mortality_rates)
151
152 #Find the highest damage inducing hurricane and its total cost.
153
154 def max_damage(hurricanes):
155
156 max_damage_hurricane= ''
157 max_damage_number= 0
158
159 for hurricane in hurricanes:
160 if hurricanes[hurricane]['Damage'] == 'Damages not recorded':
161 continue
162 if hurricanes[hurricane]['Damage'] > max_damage_number:
163 max_damage_hurricane = hurricanes[hurricane]['Name']
164 max_damage_number = hurricanes[hurricane]['Damage']
165 return max_damage_hurricane, max_damage_number
166
167 max_damage_hurricane, max_damage_number = max_damage(hurricanes)
168 #print(max_damage_hurricane, max_damage_number)
169
170 #Categorize hurricanes by damage rates and return a dictionary
171
172 def damage_scaled(hurricanes):
173
174 damage_scale = {0: [], 1: [], 2: [], 3: [], 4: []}
175
176 for hurricane in hurricanes:

https://gist.github.com/codecademydev/8b66e78dab271ee7291588dcde8fb4aa 4/5
11/9/2020 Codecademy export
177
178 rate = 0
179 damage = hurricanes[hurricane]['Damage']
180
181 if damage == 'Damages not recorded':
182 continue
183 elif damage < 100000000:
184 rate = 0
185 elif damage >= 100000000 and damage < 1000000000:
186 rate = 1
187 elif damage >= 1000000000 and damage < 10000000000:
188 rate = 2
189 elif damage >= 10000000000 and damage < 50000000000:
190 rate = 3
191 else:
192 rate = 4
193
194 if rate not in damage_scale:
195 damage_scale[rate] = hurricanes[hurricane]
196 else:
197 damage_scale[rate].append(hurricanes[hurricane])
198
199 return damage_scale
200
201 damage_scale = damage_scaled(hurricanes)
202 #print(damage_scale)
203
204

https://gist.github.com/codecademydev/8b66e78dab271ee7291588dcde8fb4aa 5/5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy