コード例 #1
0
ファイル: mini-yelp.py プロジェクト: RideGreg/LintCode
    def neighbors(self, location, k):
        def getBitLength(k):
            errors = [
                2500, 630, 78, 20, 2.4, 0.61, 0.076, 0.019, 0.0024, 0.0006,
                0.000074
            ]
            for i, v in enumerate(errors):
                if k > v:
                    return i
            return len(errors)

        # the first bitLength bits must match in order to be within distance k Km.
        bitLength = getBitLength(k)
        code = GeoHash.encode(location)[:bitLength]
        left = bisect.bisect_left(self.geo_value, code)
        right = bisect.bisect_right(self.geo_value,
                                    code + '{')  # '{' is after 'z'

        ans = []
        for index in xrange(left, right):
            hashcode = self.geo_value[index]
            r = self.restaurants[hashcode]
            distance = Helper.get_distance(location, r.location)
            if distance < k:
                ans.append((distance, r.name))
        ans.sort(key=lambda x: x[0])
        return [x[1] for x in ans]
コード例 #2
0
ファイル: mini-yelp.py プロジェクト: kid1211/AlgoPrep
 def add_restaurant(self, name, location):
     restaurant = Restaurant.create(name, location)
     hashcode = "%s.%s" % (GeoHash.encode(location), restaurant.id)
     bisect.insort(self.geo_value, hashcode)
     self.restaurants[hashcode] = restaurant
     self.ids[restaurant.id] = hashcode
     return restaurant.id
コード例 #3
0
 def add_restaurant(self, name, location):
     # Write your code here
     rest = Restaurant.create(name, location)
     code = "%s.%s" % (GeoHash.encode(location), rest.id)
     self.codes[rest.id] = code
     self.rests[code] = rest
     bisect.insort(self.geos, code)
     return rest.id
コード例 #4
0
    def remove_restaurant(self, restaurant_id):
        if restaurant_id not in self.id2restaurant:
            return

        restaurant = self.id2restaurant.pop(restaurant_id)
        geocode = GeoHash.encode(restaurant.location)
        for i in range(9):
            self.loc_table[i][geocode[:i]].remove(restaurant_id)
コード例 #5
0
ファイル: mini-yelp.py プロジェクト: wuhao007/LeetCode
 def add_restaurant(self, name, location):
     # Write your code here
     restaurant = Restaurant.creat(name, location)
     hashcode = "%s.%s" % (GeoHash.encode(location), restaurant.id)
     bisect.insort(self.geo_value, hashcode)
     self.restaurants[hashcode] = restaurant
     self.ids[restaurant.id] = hashcode
     return restaurant.id
コード例 #6
0
ファイル: mini-yelp.py プロジェクト: RideGreg/LintCode
    def add_restaurant(self, name, location):
        r = Restaurant.create(name, location)
        # add r.id incase two locations has the same geohash
        hashcode = "{}.{}".format(GeoHash.encode(location), r.id)

        self.restaurants[hashcode] = r
        self.id2hashcode[r.id] = hashcode
        bisect.insort(self.geo_value, hashcode)
        return r.id
コード例 #7
0
 def remove_restaurant(self, restaurant_id):
     if restaurant_id in self.id2restaurant:
         restanrant = self.id2restaurant[restaurant_id]
         del self.id2restaurant[restaurant_id]
         geohash = GeoHash.encode(restanrant.location)
         for i in range(0, 6):
             key = geohash[:i]
             if key not in self.geohash_table:
                 continue
             self.geohash_table[key].remove(restaurant_id)
コード例 #8
0
    def add_restaurant(self, name, location):
        restaurant = Restaurant.create(name, location)
        r_id = restaurant.id

        self.id2restaurant[r_id] = restaurant

        geocode = GeoHash.encode(location)

        for i in range(9):
            self.loc_table[i][geocode[:i]].add(r_id)

        return r_id
コード例 #9
0
    def add_restaurant(self, name, location):
        restaurant = Restaurant(name, location)

        self.restaurant_serial_id += 1
        restaurant_id = self.restaurant_serial_id
        self.id2restaurant[restaurant_id] = restaurant

        geohash = GeoHash.encode(location)
        for i in range(0, 6):
            key = geohash[:i]
            if key not in self.geohash_table:
                self.geohash_table[key] = set()
            self.geohash_table[key].add(restaurant_id)
        return restaurant_id
コード例 #10
0
ファイル: mini-yelp.py プロジェクト: wuhao007/LeetCode
 def neighbors(self, location, k):
     # Write your code here
     length = self.get_length(k)
     hashcode = GeoHash.encode(location)[0:length]
     left = bisect.bisect_left(self.geo_value, hashcode)
     right = bisect.bisect_right(self.geo_value, hashcode + '{')
     results = []
     for index in range(left, right):
         hashcode = self.geo_value[index]
         restaurant = self.restaurants[hashcode]
         distance = Helper.get_distance(location, restaurant.location)
         if distance <= k:
             results.append((distance, restaurant))
     results = sorted(results, key=lambda obj: obj[0])
     return [rt[1].name for rt in results]
コード例 #11
0
    def neighbors(self, location, k):
        length = self.get_length(k)
        prefix = GeoHash.encode(location)[:length]
        hashcodes = self.trie.get_keys_by_prefix(prefix)

        neighbors = []
        restaurant = distance = None
        for hashcode in hashcodes:
            restaurant = self.restaurants[hashcode]
            distance = Helper.get_distance(location, restaurant.location)
            if distance <= k:
                neighbors.append((distance, restaurant))

        neighbors.sort(key=lambda item: item[0])
        return [restr.name for _, restr in neighbors]
コード例 #12
0
 def neighbors(self, location, k):
     # Write your code here
     length = self.get_length(k)
     hashcode = GeoHash.encode(location)[0:length]
     left = bisect.bisect_left(self.geo_value, hashcode)
     right = bisect.bisect_right(self.geo_value, hashcode + '{')
     results = []
     for index in range(left, right):
         hashcode = self.geo_value[index]
         restaurant = self.restaurants[hashcode]
         distance = Helper.get_distance(location, restaurant.location)
         if distance <= k:
             results.append((distance, restaurant))
     results = sorted(results, key=lambda obj: obj[0])
     return [rt[1].name for rt in results]
コード例 #13
0
    def neighbors(self, location, k):
        code_len = self.get_code_len(k)
        geocode = GeoHash.encode(location)
        r_ids = list(self.loc_table[code_len][geocode[:code_len]])
        dist_lists = []
        for r_id in r_ids:
            restaurant = self.id2restaurant[r_id]
            dist = Helper.get_distance(restaurant.location, location)
            if dist < k:
                heappush(dist_lists, (dist, restaurant.name))

        name_lists = []
        while dist_lists:
            _, name = heappop(dist_lists)
            name_lists.append(name)

        return name_lists
コード例 #14
0
    def neighbors(self, location, k):
        length = self.get_length(k)
        prefix = GeoHash.encode(location)[:length]

        # chr(ord('z') + 1) == '{'
        left = bisect.bisect_left(self.geohashs, prefix)
        right = bisect.bisect(self.geohashs, prefix + '{')

        neighbors = []
        hashcode = restaurant = distance = None
        for i in range(left, right):
            hashcode = self.geohashs[i]
            restaurant = self.restaurants[hashcode]
            distance = Helper.get_distance(location, restaurant.location)
            if distance <= k:
                neighbors.append((distance, restaurant))

        neighbors.sort(key=lambda item: item[0])
        return [restr.name for _, restr in neighbors]
コード例 #15
0
    def neighbors(self, location, k):
        geohash_len = self.get_geohash_length(k)
        if geohash_len > 5:
            geohash_len = 5
        cur_geohash = GeoHash.encode(location)
        key = cur_geohash[:geohash_len]
        if key not in self.geohash_table:
            return []

        restaurants_id = self.geohash_table[key]

        restaurants = [
            (self.id2restaurant[restaurant_id].name,
             Helper.get_distance(location,
                                 self.id2restaurant[restaurant_id].location))
            for restaurant_id in restaurants_id
        ]
        print(restaurants)
        restaurants.sort(key=lambda restaurant: restaurant[1])
        return [name for (name, dist) in restaurants if dist < k]
コード例 #16
0
    def neighbors(self, location, k):
        length = self.get_length(k)
        hashcode = GeoHash.encode(location)[0:length]
        left = bisect.bisect_left(self.geo_value, hashcode)
        right = bisect.bisect_right(self.geo_value, hashcode + '{')
        #首先字符串之间比较大小,是从左往右比较ASCII码的大小,
        # 比如从第一个字符开始比较,ASCII大的视为整个字符串大于另个一比较的字符串,
        # 若相等则向右一位接着比较。‘{’的ASCII码的大小为123,‘z’的ASCII码的大小为122,
        # 所以你只要选择一个ASCII码比'z'大的字符即可。

        results = []
        for idx in range(left, right):
            hashcode = self.geo_value[idx]
            restaurant = self.restaurants[hashcode]
            distance = Helper.get_distance(location, restaurant.location)
            if distance <= k:
                results.append((distance, restaurant))

        results = sorted(results, key=lambda x: x[0])
        return [r[1].name for r in results]
コード例 #17
0
    def neighbors(self, location, k):
        # Write your code here
        dist = self.get_length(k)
        hashcode = GeoHash.encode(location)[:dist]
        left = bisect.bisect_left(self.geo_values, hashcode)
        # "{" is the next character after "z".
        right = bisect.bisect_right(self.geo_values, hashcode + "{")

        ret = []
        print left, right, self.geo_values

        for i in range(left, right):
            hashvalue = self.geo_values[i]
            restaurant = self.restaurants[hashvalue]
            d = Helper.get_distance(location, restaurant.location)
            if d <= k:
                ret.append((d, restaurant))

        ret = sorted(ret, key=lambda r: r[0])

        print "find neighbors ", ret
        return [r[1].name for r in ret]
コード例 #18
0
    def neighbors(self, location, k):
        # Write your code here
        errors = [
            2500, 630, 78, 20, 2.4, 0.61, 0.076, 0.01911, 0.00478, 0.0005971,
            0.0001492, 0.0000186
        ]
        pre = 12
        for i in xrange(12):
            if k > errors[i]:
                pre = i
                break
        code = GeoHash.encode(location)[:pre]
        l = bisect.bisect_left(self.geos, code)
        r = bisect.bisect_right(self.geos, code + '{')
        ans = []
        for i in xrange(l, r):
            code = self.geos[i]
            rest = self.rests[code]
            dist = Helper.get_distance(location, rest.location)
            if dist <= k:
                ans.append((dist, rest.name))

        ans = sorted(ans, key=lambda x: x[0])
コード例 #19
0
 def get_restr_hashcode(self, restaurant):
     return '{0}:{1}'.format(GeoHash.encode(restaurant.location),
                             restaurant.id)