-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Code
More file actions
328 lines (291 loc) · 9.78 KB
/
Python Code
File metadata and controls
328 lines (291 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# This program takes in the user's vessel dimensions and coordinates.
# It can then check whether the vessel's approaching a dangerous zone, update its new position, or compute its maximum capacity.
import math
import random
# Constants
MIN_LAT = -90 # deg
MAX_LAT = 90 # deg
MIN_LONG = -180 # deg
MAX_LONG = 180 # deg
EARTH_RADIUS = 6371 # km
# Functions
def meter_to_feet(measure_m):
'''
(num) -> num
Returns converted measure from meter to feet
Examples:
>>> meter_to_feet(1)
3.28
>>> meter_to_feet(5)
16.4
>>> meter_to_feet(100)
328.0
'''
METER = 3.28 # ft
measure_ft = float(round((measure_m * METER), 2))
return measure_ft
def degrees_to_radians(angle_deg):
'''
(num) -> num
Returns converted angle from degree to radian
Examples:
>>> degrees_to_radians(1)
0.02
>>> degrees_to_radians(90)
1.57
>>> degrees_to_radians(270)
4.71
'''
angle_rad = float(round((angle_deg * math.pi / MAX_LONG), 2))
return angle_rad
def get_vessel_dimensions():
'''
(None) -> (num, num)
Returns coverted vessel length and width from meter to feet
Examples:
>>> get_vessel_dimensions()
Enter the vessel length (in meter): 10
Enter the vessel width (in meter): 6
(32.8, 19.68)
>>> get_vessel_dimensions()
Enter the vessel length (in meter): 8
Enter the vessel width (in meter): 4
(26.24, 13.12)
>>> get_vessel_dimensions()
Enter the vessel length (in meter): 14
Enter the vessel width (in meter): 8.5
(45.92, 27.88)
'''
# Vessel dimensions (meter)
prompt_length = 'Enter the vessel length (in meter): '
prompt_width = 'Enter the vessel width (in meter): '
length_m = float(input(prompt_length))
width_m = float(input(prompt_width))
# Meter to feet conversion
length_ft = meter_to_feet(length_m)
width_ft = meter_to_feet(width_m)
return length_ft, width_ft
def get_valid_coordinate(val_name, min_float, max_float):
'''
(str, num, num) -> num
Returns valid coordinate value within min and max
Examples:
>>> get_valid_coordinate('latitude', -90, 90)
What is your latitude ?-100
Invalid latitude
What is your latitude ?-87.6
-87.6
>>> get_valid_coordinate('y-coordinate', 0, 10)
What is your y-coordinate ?-2
Invalid y-coordinate
What is your y-coordinate ?15
Invalid y-coordinate
What is your x-coordinate ?9.5
9.5
>>> get_valid_coordinate('longitude', -180, 180)
What is your longitude ?150
150
'''
prompt = 'What is your ' + val_name + ' ?'
coordinate = float(input(prompt))
while min_float > coordinate or coordinate > max_float:
print('Invalid', val_name)
prompt = 'What is your ' + val_name + ' ?'
coordinate = float(input(prompt))
return coordinate
def get_gps_location():
'''
(None) -> (num, num)
Returns valid latitude and longitude values within set min and max
Examples:
>>> What is your latitude ?-100
Invalid latitude
What is your latitude ?50
What is your longitude ?-200
Invalid longitude ?0
(50.0, 0.0)
>>> What is your latitude ?40
What is your longitude ?-75.48
(40.0, -75.48)
>>> What is your latitude ?89
What is your longtitude? -179
(89.0, -179.0)
'''
lat = get_valid_coordinate('latitude', MIN_LAT, MAX_LAT)
long = get_valid_coordinate('longitude', MIN_LONG, MAX_LONG)
return lat, long
def distance_two_points(lat_1, long_1, lat_2, long_2):
'''
(num, num, num, num) -> num
Returns distance in km between two locations
Examples:
>>> distance_two_points(45.508888,-73.561668,19.432608,-99.133209)
3719.22 km
>>> distance_two_points(57,-82,45,-75)
1360.39 km
>>> distance_two_points(68.5, 100.4, 20.6, -99)
9953.79 km
'''
# Coordinate conversion (deg to rad)
lat_1_rad = degrees_to_radians(lat_1)
long_1_rad = degrees_to_radians(long_1)
lat_2_rad = degrees_to_radians(lat_2)
long_2_rad = degrees_to_radians(long_2)
# Distance calculation
cal_1 = math.sin((lat_2_rad - lat_1_rad) / 2)**2
cal_2 = math.cos(lat_1_rad)
cal_3 = math.cos(lat_2_rad)
cal_4 = math.sin((long_2_rad - long_1_rad) / 2)**2
cal_5 = cal_1 + cal_2 * cal_3 * cal_4
formula = 2 * math.atan2(math.sqrt(cal_5), math.sqrt(1 - cal_5))
dist_km = round(EARTH_RADIUS * formula, 2)
return dist_km
def check_safety(lat, long):
'''
(num, num) -> None
Prints safety message depending on current position
Examples:
>>> check_safety(21.804132, -72.305832)
Error: Restricted zone!
>>> check_safety(40.78, -70.1)
Warning: Hazardous area! Navigate with caution.
>>> check_safety(55, -70.9)
Safe navigation.
'''
# Restricted zone constants
RESTR_LAT = 25 # deg
RESTR_LONG = -71 # deg
RESTR_DIST = 400 # km
# Safety conditions
dist_km = distance_two_points(lat, long, RESTR_LAT, RESTR_LONG)
if dist_km <= RESTR_DIST:
print('Error: Restricted zone!')
elif lat >= 40 and lat <= 41 and long >= RESTR_LONG and long <= -70:
print('Warning: Hazardous area! Navigate with caution.')
else:
print('Safe navigation.')
def get_max_capacity(length_ft, width_ft):
'''
(num, num) -> num
Returns maximum number of adults with avg weight of 150lbs on vessel
Examples:
>>> get_max_capacity(18.0, 6.0)
7
>>> get_max_capacity(0.3, 0.1)
0
>>> get_max_capacity(23.7, 7.8)
12
'''
if length_ft <= 26:
capacity = length_ft * width_ft / 15
elif length_ft > 26:
capacity = length_ft * width_ft / 15 + (length_ft - 26) * 3
return int(capacity)
def passengers_on_boat(length_ft, width_ft, passenger_num):
'''
(num, num, num) -> Bool
Returns True or False whether the vessel can fit the number of passengers
Examples:
>>> passengers_on_boat(18, 6, 6)
False
>>> passengers_on_boat(0.3, 0.1, 0)
True
>>> passengers_on_boat(23.7, 7.8, 12)
True
'''
# Max capacity of vessel
max_passenger = get_max_capacity(length_ft, width_ft)
# Equal distribution of passengers across the four corners of vessel
eq_distribution = passenger_num % 4
if passenger_num > max_passenger or eq_distribution != 0:
return False
elif passenger_num <= max_passenger and eq_distribution == 0:
return True
def update_coordinate(position, min_float, max_float):
'''
(num, num, num) -> num
Returns new valid position within set min and max
Examples:
>>> update_coordinate(15, 0, 40)
6.05
>>> update_coordinate(35, -90, 90)
40
>>> update_coordinate(67, 0, 90)
77
'''
random.seed(123)
random_num = random.random() * 20 - 10
new_position = float(position + random_num)
while new_position < min_float or new_position > max_float:
random_num = random.random() * 20 - 10
new_position = float(position + random_num)
return round(new_position, 2)
def wave_hit_vessel(lat, long):
'''
(num, num) -> (num, num)
Returns new location coordinates
Examples:
>>> wave_hit_vessel(22, -72.561668)
Safe navigation.
(17.05, -80.07)
>>> wave_hit_vessel(45.51, -73.56)
Safe navigation.
(46.03, -73.04)
>>> wave_hit_vessel(15.87, 90.436)
Safe navigation.
(16.39, 90.96)
'''
new_lat = update_coordinate(lat, MIN_LAT, MAX_LAT)
new_long = update_coordinate(long, MIN_LONG, MAX_LONG)
new_lat = update_coordinate(lat, MIN_LAT, MAX_LAT)
new_long = update_coordinate(long, MIN_LONG, MAX_LONG)
check_safety(new_lat, new_long)
return new_lat, new_long
def vessel_menu():
'''
(None) -> None
Displays vessel menu
'''
# Menu start
print('Welcome to the boat menu!')
lat, long = get_gps_location()
print('Your current position is at latitude', lat, 'and longitude', long)
# Vessel dimensions
length_ft, width_ft = get_vessel_dimensions()
print('Your boat measures', length_ft, 'feet by', width_ft, 'feet')
# Option menu
option = 'Please select an option below: '
opt_1 = '1. Check the safety of your boat'
opt_2 = '2. Check the maximum number of people that can fit on the boat'
opt_3 = '3. Update the position of your boat'
opt_4 = '4. Exit boat menu'
print(option)
print(opt_1)
print(opt_2)
print(opt_3)
print(opt_4)
selection = int(input('Your selection: '))
# User inputs
while selection != 4:
if selection == 1:
check_safety(lat, long)
elif selection == 2:
num_adults = int(input('How many adults go on the boat? '))
capacity = passengers_on_boat(length_ft, width_ft, num_adults)
if capacity == True:
print('Your boat can hold', num_adults, 'adults')
elif capacity == False:
print('Your boat cannot hold', num_adults, 'adults')
elif selection == 3:
lat, long = wave_hit_vessel(lat, long)
print('Your new position is latitude of', lat,
'and longitude of', long)
print(option)
print(opt_1)
print(opt_2)
print(opt_3)
print(opt_4)
selection = int(input('Your selection: '))
# Input 4
print('End of boat menu.')
vessel_menu()