Re: IP to Country (#139)

On 9/14/07, Ruby Q. [email protected] wrote:

This week’s Ruby Q. 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

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.
os = ARGV[0].split(/./)
ip = os[3].to_i + os[2].to_i256 + os[1].to_i2562 + os[0].to_i*2563

f =“IpToCountry.csv”)

perform binary search on the file

low = 0
high = f.stat.size

while low <= high
mid = (low + high) / 2 mid # seek to the middle of our search window -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
puts country; exit

puts “no country”- steve