Skip to content

Instantly share code, notes, and snippets.

@BenjiWiebe
Last active August 29, 2015 14:23
Show Gist options
  • Save BenjiWiebe/0400caab9335875c8e93 to your computer and use it in GitHub Desktop.
Save BenjiWiebe/0400caab9335875c8e93 to your computer and use it in GitHub Desktop.
#!/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]}" }
@BenjiWiebe
Copy link
Author

Output:

States with higher highs OR lower lows than Kansas:
Alaska: 100, -80
Arizona: 128, -40
California: 134, -45
Colorado: 118, -61
Idaho: 118, -60
Iowa: 118, -47
Maine: 105, -50
Michigan: 112, -51
Minnesota: 114, -60
Montana: 117, -70
Nebraska: 118, -47
Nevada: 125, -50
New hampshire: 106, -50
New mexico: 122, -50
New york: 108, -52
North dakota: 121, -60
Oregon: 117, -54
Pennsylvania: 111, -42
South dakota: 120, -58
Utah: 117, -69
Vermont: 105, -50
Washington: 118, -48
Wisconsin: 114, -55
Wyoming: 115, -66

States with higher highs AND lower lows than Kansas:
California: 134, -45
Nevada: 125, -50
New mexico: 122, -50

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