def return_avg(path_array): location_key = return_arrays() path_length = len(path_array) total_danger = 0 for i in range(path_length): location = path_array[i] long_index = binarySearch(location_key[0], 0, len(location_key[0]), location[0]) lat_index = binarySearch(location_key[1], 0, len(location_key[1]), location[1]) total_danger += grid[day][hour][long_index][lat_index] return total_danger / path_length
def test_binarySearch_Cmp(self): data = [] for i in range(10000): data.append(random.randint(1, 100000000)) for i in range(100): number = random.randint(1, 100000000) self.assertEqual(search.binarySearch(data, number), search.binarySearchRecursive(data, number))
def test_BS(self): sa = self.assertEqual l = range(0,100000) sa(3, search.binarySearch(l, 3)) sa(9, search.binarySearch(l, 9)) sa(0, search.binarySearch(l, 0)) sa(100, search.binarySearch(l, 100)) sa(3, search.binarySearch(l, 3, "rec")) sa(9, search.binarySearch(l, 9), "rec") sa(0, search.binarySearch(l, 0), "rec") sa(100, search.binarySearch(l, 100), "rec")
def test_BS(self): sa = self.assertEqual l = range(0, 100000) sa(3, search.binarySearch(l, 3)) sa(9, search.binarySearch(l, 9)) sa(0, search.binarySearch(l, 0)) sa(100, search.binarySearch(l, 100)) sa(3, search.binarySearch(l, 3, "rec")) sa(9, search.binarySearch(l, 9), "rec") sa(0, search.binarySearch(l, 0), "rec") sa(100, search.binarySearch(l, 100), "rec")
from search import binarySearch # This list must be sorted. If it is not given as sorted, sort it first, then call the binarySearch method testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42] print(binarySearch(3, testlist)) print(binarySearch(13, testlist))
def test_bynarySearh_1(self): data = [1, 2, 45, 56, 100, 178, 290] self.assertEqual(search.binarySearch(data, 100), 4) self.assertEqual(search.binarySearch(data, 178), 5) self.assertEqual(search.binarySearch(data, 1), 0) self.assertEqual(search.binarySearch(data, 2), 1)
latitude_length = len(latitude_key) baseline_matrix = [[], [], [], [], [], [], []] for day in range(7): for i in range(longitude_length): baseline_matrix[day].append([]) for j in range(latitude_length): baseline_matrix[day][i].append([]) for index, row in df_arrest_con.iterrows(): long_value = row['Longitude'] lat_value = row['Latitude'] day_of_week = int(row['ARREST_DATE']) long_index = int( binarySearch(longitude_key, 0, longitude_length, long_value)) lat_index = int(binarySearch(latitude_key, 0, latitude_length, lat_value)) baseline_matrix[day_of_week][long_index][lat_index].append( (row['AGE_GROUP'], row['LAW_CAT_CD'])) grid = [[], [], [], [], [], [], []] for day in range(7): for hour in range(24): grid[day].append(baseline_matrix[day]) for index, row in df_shooting_con.iterrows(): long_value = row['Longitude'] lat_value = row['Latitude'] day_of_week = int(row['OCCUR_DATE']) hour = int(row['OCCUR_TIME'])
def test_search(self): data = [1, 2, 3, 5, 6, 12, 7, 4, 8] self.assertEqual(linear_search(data, 6), 4) self.assertEqual(linear_search(data, 10), -1) self.assertEqual(binarySearch(data, 6), 4)
import random from search import linear_search from time import time from search import binarySearch data = range(100000) start = time() index = binarySearch(data, data[0]) end = time() elapsed = end - start print('Total time elspsed for binary search is: ' + str(elapsed)) datalin = random.sample(range(1000), 1000) startlin = time() length = int(len(datalin) / 2) indexlin = linear_search(datalin, datalin[length]) endlin = time() elapsedlin = endlin - startlin print('Total time elapsed for linear search is: ' + str(elapsedlin))