def search():
    s = getSession()
    
    # TODO
    # test this function
    
    try:
        res = {}
        phone_number = request.json['phone_number']
        group_name = request.json['group_name']
        
        pq = PriorityQueue(0)
        
        
        #get all the groups whos drivers phone number matches the query exactly
        for row in _dbcon.execute("SELECT * FROM users WHERE phone_number  = ?", (phone_number,)):
            user = row[user_id]
            for row2 in _dbcon.execute("SELECT * FROM groups WHERE user_id  = ?", (user,)):
                #create group object from row
                group = {}
                for g_field in _group_fields:
                    group[g_field] = row[g_field]
                    
                #add group to priority queue with priority .1
                pq.add(.1,group)
        
        
        #get all the groups with a name resembles the query (2/3 of the characters match)
        for row in _dbcon.execute("SELECT * FROM groups"):
            #create group object from row
            group = {}
            for g_field in _group_fields:
                group[g_field] = row[g_field]
                
            #add group to priority queue with edit distance / length as their priority
            priority = distance(group['group_name'],group_name)
            ps.add(priority,group)
                
            #add the groups where 2/3 of the charatcers match
            count = 0
            while True:
                g = ps.get()
                if g[0] <= .75:
                    res[count] = g[1]
                    count = count+1     
        return res
    except HTTPError:
        raise
    except:
        print('Bad search request.')
        traceback.print_exc()
        abort(400, 'Bad search request.')
Example #2
0
def best_first_search(problem, f):
    "Search nodes with minimum f(node) value first."
    frontier = PriorityQueue([Node.Node(problem.initial)], key=f)
    reached = {}
    while frontier:
        node = frontier.pop()
        if problem.is_goal(node.state):
            return node, len(frontier)
        for child in Node.expand(problem, node):
            s = child.state
            if s not in reached or child.path_cost < reached[s].path_cost:
                reached[s] = child
                frontier.add(child)
    return failure, None
Example #3
0
	def mergeKLists(self, lists):
		"""
		:type lists: List[ListNode]
		:rtype: ListNode
		"""
		pq = PriorityQueue()
		dummy = ListNode(0)
		curr = dummy
		for node in lists:
			if node:
				pq.add((curr, curr.val))
		while pq:
			node = pq.pop()
			curr.next = ListNode(node.val)
			curr = curr.next
			pq.add((node.next, node.next.val))