Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: [QUIZ] IP to Country (#139
steve d
9/16/2007 1:21:00 PM
On 9/14/07, Ruby Quiz <james@grayproductions.net> wrote:> This week's Ruby Quiz is to write a simple utility. Your program should accept> an IP address as a command-line argument and print out the two letter code for> the country that IP is assigned in. You can find a database for the matching> at:>>
http://software77.net/cgi-bin/ip-country/geo-ip....
To keep the problem interesting though, let's write our programs with a focus on> speed and memory efficiency.Hi All,Here's my submission. It's almost exactly the same as Jesus'. I have a feeling everyone is going to have very similar code for this one.
http://pastie.caboo....
= ARGV[0].split(/\./)ip = os[3].to_i + os[2].to_i*256 + os[1].to_i*256**2 + os[0].to_i*256**3f = File.open("IpToCountry.csv")# perform binary search on the filelow = 0high = f.stat.sizewhile low <= high mid = (low + high) / 2 f.seek mid # seek to the middle of our search window f.seek -2, IO::SEEK_CUR until f.getc == ?\n # walk backwards until we hit a newline new_high = f.pos - 1 line = f.gets new_low = f.pos from, to, x, x, country = line[1..-1].split(/","/) if to.to_i < ip # we are too low, set new low to after this line low = new_low elsif from.to_i > ip # we are too high, set new high to before the last newline high = new_high else puts country; exit endendputs "no country"- steve
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: [QUIZ] IP to Country (#139
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password