Created
May 11, 2011 22:01
-
-
Save avaynshtok/967479 to your computer and use it in GitHub Desktop.
capital to capital
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
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
Returns 1,224,725 (miles I assume)