Skip to content

Instantly share code, notes, and snippets.

@avaynshtok
Created May 11, 2011 22:01
Show Gist options
  • Select an option

  • Save avaynshtok/967479 to your computer and use it in GitHub Desktop.

Select an option

Save avaynshtok/967479 to your computer and use it in GitHub Desktop.
capital to capital
require "rubygems"
require "bundler/setup"
require 'geokit'
require 'open-uri'
require 'hpricot'
require 'pp'
require 'find'
coder = Geokit::Geocoders::GoogleGeocoder
url = "http://en.wikipedia.org/wiki/List_of_capital_cities"
doc = open(url) { |f| Hpricot(f) }
table = doc.search('//.wikitable').first
places = table.search('tr').map do |tr|
city = tr.children[1].children.first.inner_text.strip
country = tr.children[3].children.first.inner_text.strip
[city, country].join(',')
end
locations = places.map { |p| sleep 1; coder.geocode(p); }.compact
distances = locations.each_cons(2).map { |a, b| sleep 1; a.distance_to(b); }
distance = distances.reduce(0) { |s, d| s += d }
pp distance
@avaynshtok
Copy link
Author

Returns 1,224,725 (miles I assume)

@avaynshtok
Copy link
Author

Naive guess:
Circumference of world: 15k miles
Countries: 195
Average distance between capitals: 1/4 circumference

(25,000 / 4) * 194 = 1,212,500 ... seems to match up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment