-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.txt
257 lines (223 loc) · 6.7 KB
/
temp.txt
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
from typing import List
def get_average_elevation(m: List[List[int]]) -> float:
"""
Returns the average elevation across the elevation map m.
Examples
#>>> get_average_elevation([])
0
#>>> m = [[1,2,3],[4,5,6],[7,8,9]]
#>>> get_average_elevation(m)
5.0
#>>> m = [[1,2,2,5],[4,5,4,8],[7,9,9,1],[1,2,1,4]]
#>>> get_average_elevation(m)
4.0625
"""
#Your code goes here
def find_peak(m: List[List[int]]) -> List[int]:
"""
Given an non-empty elevation map m, returns the cell of the
highest point in m.
Examples (note some spacing has been added for human readablity)
#>>> m = [[1,2,3],
[9,8,7],
[5,4,6]]
#>>> find_peak(m)
[1,0]
#>>> m = [[6,2,3],
[1,8,7],
[5,4,9]]
#>>> find_peak(m)
[2,2]
"""
#Your code goes here
def is_sink(m: List[List[int]], c: List[int]) -> bool:
"""
Returns True if and only if c is a sink in m.
Examples (note some spacing has been added for human readablity)
#>>> m = [[1,2,3],
[2,3,3],
[5,4,3]]
#>>> is_sink(m, [0,0])
True
#>>> is_sink(m, [2,2])
True
#>>> is_sink(m, [3,0])
False
#>>> m = [[1,2,3],
[2,1,3],
[5,4,3]]
#>>> is_sink(m, [1,1])
True
"""
#Your code goes here
def find_local_sink(m: List[List[int]], start: List[int]) -> List[int]:
"""
Given a non-empty elevation map, m, starting at start,
will return a local sink in m by following the path of lowest
adjacent elevation.
Examples (note some spacing has been added for human readablity)
#>>> m = [[ 5,70,71,80],
[50, 4,30,90],
[60, 3,35,95],
[10,72, 2, 1]]
#>>> find_local_sink(m, [0,0])
[3,3]
#>>> m = [[ 5,70,71,80],
[50, 4, 5,90],
[60, 3,35, 2],
[ 1,72, 6, 3]]
#>>> find_local_sink(m, [0,3])
[2,3]
#>>> m = [[9,2,3],
[6,1,7],
[5,4,8]]
#>>> find_local_sink(m, [1,1])
[1,1]
"""
#Your code goes here
def can_hike_to(m: List[List[int]], s: List[int], d: List[int], supplies: int) -> bool:
"""
Given an elevation map m, a start cell s, a destination cell d, and
the an amount of supplies returns True if and only if a hiker could reach
d from s using the strategy dscribed in the assignment .pdf. Read the .pdf
carefully. Assume d is always south, east, or south-east of s. The hiker
never travels, north, west, nor backtracks.
Examples (note some spacing has been added for human readablity)
#>>> m = [[1,4,3],
[2,3,5],
[5,4,3]]
#>>> can_hike_to(m, [0,0], [2,2], 4)
True
#>>> can_hike_to(m, [0,0], [0,0], 0)
True
#>>> can_hike_to(m, [0,0], [2,2], 3)
False
#>>> m = [[1, 1,100],
[1,100,100],
[1, 1, 1]]
#>>> can_hike_to(m, [0,0], [2,2], 4)
False
#>>> can_hike_to(m, [0,0], [2,2], 202)
True
"""
#Your code goes here
while s!=d and supplies >= 0:
# going south
if s[1] == d[1]:
if s[0] < (len(m) -1):
supplies = supplies - abs(m[s[0]][s[1]] - m[s[0]+1][s[1]])
s[0] = s[0] + 1
# index out of map
else:
return False
# going east
elif s[0] == d[0]:
if s[1] < (len(m) -1):
supplies = supplies - abs(m[s[0]][s[1]] - m[s[0]][s[1]+1])
s[1] = s[1] + 1
# index out of map
else:
return False
else:
gosouth = -1
goeast = -1
if s[0] < (len(m) - 1):
gosouth = abs(m[s[0]][s[1]] - m[s[0]+1][s[1]])
if s[1] < (len(m) -1):
goeast = abs(m[s[0]][s[1]] - m[s[0]][s[1]+1])
if goeast != -1 and gosouth != -1:
if gosouth >=goeast:
supplies = supplies - goeast
s[1] = s[1] + 1
else:
supplies = supplies - gosouth
s[0] = s[0] + 1
elif goeast == -1 and gosouth != -1:
supplies = supplies - gosouth
s[0] = s[0] + 1
elif goeast != -1 and gosouth == -1:
supplies = supplies - goeast
s[1] = s[1] + 1
else:
return False
return s==d and supplies >= 0
def rotate_map(m: List[List[int]]) -> None:
"""
Rotates the orientation of an elevation map m 90 degrees counter-clockwise.
See the examples to understand what's meant by rotate.
Examples (note some spacing has been added for human readablity)
#>>> m = [[1,2,3],
[2,3,3],
[5,4,3]]
#>>> rotate_map(m)
#>>> m
[[3,3,3],
[2,3,4],
[1,2,5]]
#>>> m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
#>>> rotate_map(m)
#>>> m
[[8,7,2,3],
[1,5,3,6],
[9,4,3,7],
[5,2,6,1]]
"""
#Your code goes here
for x in range(len(m)):
for y in range(x):
temp = m[x][y]
m[x][y] = m[y][x]
m[y][x] = temp
m.reverse()
"""
You are not required to understand or use the code below. It is there for
curiosity and testing purposes.
"""
def create_real_map()-> List[List[int]]:
"""
Creates and returns an elevation map from the real world data found
in the file elevation_data.csv.
Make sure this .py file and elevation_data.csv are in the same directory
when you run this function to ensure it works properly.
"""
data = open("elevation_data.csv")
m = []
for line in data:
m.append(line.split(","))
data.close()
for i in range(len(m)):
for j in range(len(m[i])):
m[i][j] = int(m[i][j])
return m
###############test for q5
m = [[1,4,3],
[2,3,5],
[5,4,3]]
assert can_hike_to(m, [0,0], [2,2], 4)
assert not can_hike_to(m, [0,0], [2,2], 3)
assert can_hike_to(m, [0,0], [0,0], 0)
assert not can_hike_to(m, [0,0], [3,2], 400)
assert not can_hike_to(m, [0,0], [30,20], 400)
assert not can_hike_to(m, [0,0], [2,2], 3)
m = [[1, 1,100],
[1,100,100],
[1, 1, 1]]
assert not can_hike_to(m, [0,0], [2,2], 4)
assert can_hike_to(m, [0,0], [2,2], 202)
assert can_hike_to(m, [0,0], [2,2], 198)
assert not can_hike_to(m, [0,0], [2,2], 197)
###############test for q6
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_map(m)
assert m == [[3,3,3], [2,3,4], [1,2,5]]
m=[[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
rotate_map(m)
assert m == [[8,7,2,3], [1,5,3,6], [9,4,3,7], [5,2,6,1]];