def stop_by(curLati, curLong, desLati, desLong, keywords, maxLen): result = [] for s in data: isAllContain = True for keyword in keywords: if not keyword in s['keywords']: isAllContain = False break if isAllContain and (ctp17hw1.dist(curLati, curLong, s['latitude'], s['longitude']) + \ ctp17hw1.dist(s['latitude'], s['longitude'], desLati, desLong)) < maxLen: result.append(s['name']) return result # print(stop_by(37.477155, 126.963423, 37.459250, 126.950786, ["Matjip", "Honbap"], 3500))
def nearest(curLati, curLong, keyword): name = "" for s in data: if keyword in s['keywords']: minDist = ctp17hw1.dist(curLati, curLong, s['latitude'], s['longitude']) name = s['name'] break for s in data: if keyword in s['keywords']: dist = ctp17hw1.dist(curLati, curLong, s['latitude'], s['longitude']) print(dist, s['name']) if dist < minDist : minDist = dist name = s['name'] return name
def count_near(loc_lst, k): cur = loc_lst[0] visit = [0] * len(loc_lst) visit[0] = 1 for i in range(len(loc_lst) - 1): if ctp17hw1.dist(cur[0], cur[1], loc_lst[i + 1][0], loc_lst[i + 1][1]) <= k and visit[i + 1] == 0: visit[i + 1] = 1 visit = count_helper(loc_lst, i + 1, visit, k) return sum(visit) - 1
def shortest_route(loc_lst, my_loc): visit = [0]*len(loc_lst) result = -1 for i in range(len(loc_lst)): l = dfs(visit, i, loc_lst, my_loc) + ctp17hw1.dist(loc_lst[i][0],loc_lst[i][1],my_loc[0],my_loc[1]) if result == -1: result = l else: result = min(l, result) return result
def dfs(visit, start, loc_lst, my_loc): result = -1 visit[start] = 1 if sum(visit) == len(visit): visit[start] = 0 return ctp17hw1.dist(loc_lst[start][0],loc_lst[start][1],my_loc[0],my_loc[1]) for i in range(len(loc_lst)): if visit[i] == 0: l = ctp17hw1.dist(loc_lst[start][0],loc_lst[start][1],loc_lst[i][0],loc_lst[i][1])+\ dfs(visit, i, loc_lst, my_loc) if result == -1: result = l else: result = min(l, result) visit[start] = 0 return result
def suggest_similar(lst, loc_lst, d, k): const = 1 result = [] str_list = list(map(lambda x: x[2], lst)) filtered_list = filtering(str_list, loc_lst) for name in str_list: if str_list.count(name) == 1: cur_loc = find_loc(name, loc_lst) for loc in filtered_list: print(name, loc[2]) print( ctp17hw1.dist(cur_loc[0] / const, cur_loc[1] / const, loc[0] / const, loc[1] / const)) print(similarity(cur_loc, loc)) if ctp17hw1.dist( cur_loc[0] / const, cur_loc[1] / const, loc[0] / const, loc[1] / const) < d and similarity(cur_loc, loc) >= k: if not loc[2] in result: result.append(loc[2]) return result
def count_helper(loc_lst, cur, visit, k): for i in range(len(loc_lst) - 1): if ctp17hw1.dist(loc_lst[cur][0], loc_lst[cur][1], loc_lst[i + 1][0], loc_lst[i + 1][1]) <= k and visit[i + 1] == 0: visit[i + 1] = 1 visit = count_helper(loc_lst, i + 1, visit, k) return visit # print(ctp17hw1.dist(0.4,0.5,0.4,0.6)) # # print(count_near([(0.3,0.2),(0.4,0.),(0.3,0.3),(0.4,0.6)],25000))5
def total_dist(lst): distant = 0 for i in range(len(lst) - 1): distant += ctp17hw1.dist(lst[i]['latitude'], lst[i]['longitude'], lst[i + 1]['latitude'], lst[i + 1]['longitude']) return distant # test example; should be erased before submitting. # loc1 = {'latitude':1.2, 'longitude':3.4, 'timestamp':100} # loc3 = {'latitude':9.1, 'longitude':2.3, 'timestamp':200} # loc2 = {'latitude':5.6, 'longitude':7.8, 'timestamp':300} # print(total_dist([loc1, loc3, loc2])) # print(ctp17hw1.dist(loc1['latitude'], \ # loc1['longitude'], \ # loc2['latitude'], \ # loc2['longitude']))
# print(union) intersection = [] keywords1 = loc1[3][:] keywords2 = loc2[3][:] for i in keywords1: if i in keywords2: a = keywords1.count(i) b = keywords2.count(i) for _ in range(b): keywords2.remove(i) intersection += [i] * min(a, b) # print(intersection) print(intersection, union) print(len(intersection) / len(union)) return len(intersection) / len(union) print(1 / 2) print(ctp17hw1.dist(1.1, 1.1, 1.7, 1.7)) loc_list = [(1.1, 5.0 , "seoul", ["agd","ab"]), \ (1.7, 5.0, "newyork", ["agd", "ab"]), (90.0, 5.0, "cafe", ["agd", "ab"]), (90.0, 5.0, "home", ["agd", "ab"]), (90.0, 5.0, "bus", ["agd", "ab"]), (1.6, 5.0, "lib", ["agd", "ab"]), (1.4, 5.0, "usa", ["agd","ab"]), (1.7, 5.0 , "europe", ["agd"])] print(suggest_similar([(1,5,"home"),(7,9,"home"), (11,17,"school"),(20, 25, "school"),(26,40,"cafe"),(41,57,"lib"),(58,59,"bus"),\ (59,60,"seoul"),(61,62,"seoul"),(63,64,"seoul")],loc_list,40000,0.2))