Dry Run 2 Solutions
| categories: Practice
Solutions for the problems on the second final dry run.
def F1(p):
'''Number of points within unit circle'''
d = np.sqrt(np.sum(p ** 2, axis=1))
return np.sum(d <= 1)
def F2(p, d):
'''Number of points with at least one neighbor within distance d.'''
count = 0
for i, p1 in enumerate(p):
for j, p2 in enumerate(p):
if i != j:
d12 = np.sqrt(np.sum((p1 - p2) ** 2))
if d12 <= d:
count += 1
break
return count
def F3(rain):
'''Largest number of consecutive days of rain.'''
longest = 0
current = 0
for r in rain:
if r > 0:
current += 1
if current > longest:
longest = current
else:
current = 0
return longest
def F4(table):
'''Average rainfall for each month.'''
result = np.zeros(12) # you are to return an array of 12 means
for i in range(12):
result[i] = np.mean(table[table[:, 1] == (i + 1), 3])
return result
def F5(rain):
'''Number of weeks with no rain.'''
rain.shape = (-1, 7) # reshape into weeks
weekly = np.sum(rain, axis=1)
dry = weekly == 0
dryweeks = np.sum(dry)
return dryweeks