Last active
August 29, 2015 14:23
-
-
Save BenjiWiebe/0400caab9335875c8e93 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/ruby | |
require 'nokogiri' | |
require 'open-uri' | |
a=Nokogiri::HTML(open("https://en.wikipedia.org/?title=U.S._state_temperature_extremes")) | |
root=a.root | |
trs=root.css("table.wikitable > tr") | |
records = trs.map {|x| [x.children[1].text, x.children[3].text, x.children[9].text] } | |
records.shift | |
hsh = Hash.new | |
records.each {|x| hsh[x[0].downcase.to_sym] = [x[1],x[2]]} | |
hsh.merge!(hsh) {|k,v1,v2| v1.map{|x| x.split.shift.gsub("−","-").to_i}} | |
kansas=hsh[:kansas] | |
hsh.delete :kansas | |
puts "States with higher highs OR lower lows than Kansas:" | |
hsh.each {|k,v| puts "#{k.to_s.capitalize}: #{v[0]}, #{v[1]}" if v[0] > kansas[0] or v[1] < kansas[1] } | |
puts | |
puts "States with higher highs AND lower lows than Kansas:" | |
hsh.each {|k,v| hsh.delete k if v[0] <= kansas[0] or v[1] >= kansas[1] } | |
hsh.each {|k,v| puts "#{k.to_s.capitalize}: #{v[0]}, #{v[1]}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: