Skip to content
Advertisement

Getting City from IP Address range

  1. I have an IP address. For example, 192.168.2.10
  2. Also I have a dictionary:
JavaScript

Question: How should I find the city from my IP address and use this dictionary spending less time (time complexity) as possible?

Advertisement

Answer

The “proper answer” if you want the best complexity for arbitrarily large data sets is the one given given by Ji Bin.

To really optimize performances over multiple calls, you indeed need to restructure your data, and use the inbuilt bisect function.

But if you REALLY do not want to touch your data, you can still use a band-aid custom implementation of bisect which would look like that

JavaScript

-> None

-> munich

-> london

-> None

-> munich

-> london

etc.

link to playground : https://trinket.io/python/e1f9deb1c7

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement