Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 107 lines (86 sloc) 2.645 kb
8576e8a first commit
Dan Buettner authored
1 require 'rubygems'
2 require 'open-uri'
3 require 'hpricot'
4 require 'date'
e99d4ff @ctreptow added histograms
ctreptow authored
5 require 'ascii_charts'
8576e8a first commit
Dan Buettner authored
6
7 module Enumerable
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
8 # with a nod to Alex D who posted at StackOverflow
7d4f22f README to Markdown and credit to Alex D
Dan Buettner authored
9 # http://stackoverflow.com/questions/9127971/sort-array-by-popularity-and-time-in-ruby
8576e8a first commit
Dan Buettner authored
10 def to_histogram
11 result = Hash.new(0)
12 each { |x| result[x] += 1 }
13 result
14 end
15
16 def most_popular_n(n)
17 ret = []
18 h = self.to_histogram
19 n.times do
20 ret << max_by { |x| h[x] }
21 h.delete(ret.last)
22 end
23 ret
24 end
7d4f22f README to Markdown and credit to Alex D
Dan Buettner authored
25
26 def most_popular
27 most_popular_n(1).first
28 end
8576e8a first commit
Dan Buettner authored
29 end
30
31 white_balls = []
32 powerballs = []
33
34 end_date = Date.parse("1 November 1997").strftime("%m/%d/%Y")
35 start_date = Date.today.strftime("%m/%d/%Y")
36
37 puts "Getting Powerball drawing data from #{start_date} back to #{end_date} ..."
38 url = "http://www.powerball.com/powerball/pb_nbr_history.asp?startDate=#{start_date}&endDate=#{end_date}"
39 doc = open(url) { |f| Hpricot(f) }
40
41 puts "Got #{doc.to_s.length} bytes of Powerball data. Stand by for parsing."
42 doc.search("//table[@align='center']/tr[@align='center']").each do |row|
43 cells = row.search("/td")
44 drawing_date = Date.strptime(cells.first.inner_text, "%m/%d/%Y")
45 white_balls.concat cells.search("[@background='/images/ball_white_40.gif']").map{|c| c.inner_text.to_i}
46 powerballs << cells.search("[@background='/images/ball_red_40.gif']").last.inner_text.to_i
47 end
48
49 puts "Examining results of #{powerballs.count} drawings..."
50 puts "Top 5 numbers: #{white_balls.most_popular_n(5).join(', ')}"
51 puts "Top Powerball: #{powerballs.most_popular}"
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
52
53 puts
54 puts "Detail:"
55
56 puts
57 puts "Regular numbers"
58 puts "=================="
59 puts "Number Draws"
60 puts "------ -----"
e99d4ff @ctreptow added histograms
ctreptow authored
61
62 w_results = {}
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
63 white_balls.most_popular_n(white_balls.uniq.size).each do |num|
64 puts "#{num.to_s.rjust(6)}#{' '*7}#{white_balls.count(num).to_s.rjust(5)}"
e99d4ff @ctreptow added histograms
ctreptow authored
65 w_results[num] = white_balls.count(num)
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
66 end
67
68 puts
69 puts "Powerballs"
70 puts "=================="
71 puts "Powerball Draws"
72 puts "--------- -----"
e99d4ff @ctreptow added histograms
ctreptow authored
73
74 p_results = {}
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
75 powerballs.most_popular_n(powerballs.uniq.size).each do |num|
76 puts "#{num.to_s.rjust(9)}#{' '*4}#{powerballs.count(num).to_s.rjust(5)}"
e99d4ff @ctreptow added histograms
ctreptow authored
77 p_results[num] = powerballs.count(num)
78 end
79
80 puts
81 puts "White balls Histogram"
82
83 results = []
84 temp = []
85 w_results.keys.sort.each do |key|
86 temp << key
87 temp << w_results[key]
88 results << temp
89 temp = []
78a8c3c add output in ranked order, tweak README
Dan Buettner authored
90 end
91
e99d4ff @ctreptow added histograms
ctreptow authored
92 puts AsciiCharts::Cartesian.new(results, :bar => true).draw
93
94 puts "Powerballs Histogram"
95
96 results = []
97 temp = []
98 p_results.keys.sort.each do |key|
99 temp << key
100 temp << p_results[key]
101 results << temp
102 temp = []
103 end
104
105 puts AsciiCharts::Cartesian.new(results, :bar => true).draw
106
Something went wrong with that request. Please try again.