-
Notifications
You must be signed in to change notification settings - Fork 4
/
powerball.rb
96 lines (76 loc) · 2.69 KB
/
powerball.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'date'
require 'ascii_charts'
module Enumerable
# with a nod to Alex D who posted at StackOverflow
# http://stackoverflow.com/questions/9127971/sort-array-by-popularity-and-time-in-ruby
def to_histogram
result = Hash.new(0)
each { |x| result[x] += 1 }
result
end
def most_popular_n(n)
ret = []
h = self.to_histogram
n.times do
ret << max_by { |x| h[x] }
h.delete(ret.last)
end
ret
end
def most_popular
most_popular_n(1).first
end
end
white_balls = []
powerballs = []
end_date = Date.parse("1 November 1997").strftime("%m/%d/%Y")
start_date = Date.today.strftime("%m/%d/%Y")
puts "Getting Powerball drawing data from #{start_date} back to #{end_date} ..."
url = "http://www.powerball.com/powerball/pb_nbr_history.asp?startDate=#{start_date}&endDate=#{end_date}"
doc = open(url) { |f| Hpricot(f) }
puts "Got #{doc.to_s.length} bytes of Powerball data. Stand by for parsing."
doc.search("//table[@align='center']/tr[@align='center']").each do |html_table_row|
html_table_cells = html_table_row.search("/td")
drawing_date = Date.strptime(html_table_cells.first.inner_text, "%m/%d/%Y")
white_balls.concat html_table_cells.search("[@background='/images/ball_white_40.gif']").map{ |cell| cell.inner_text.to_i }
powerballs << html_table_cells.search("[@background='/images/ball_red_40.gif']").last.inner_text.to_i
end
puts "Examining results of #{powerballs.count} drawings..."
puts
puts
puts "====================================="
puts "Top 5 white balls: #{white_balls.most_popular_n(5).join(', ')}"
puts "Top Powerball: #{powerballs.most_popular}"
puts "====================================="
puts
puts "Detail:"
puts
puts "White balls"
puts "=================="
puts "Number Draws"
puts "------ -----"
w_results = {}
white_balls.most_popular_n(white_balls.uniq.size).each do |num|
puts "#{num.to_s.rjust(6)}#{' '*7}#{white_balls.count(num).to_s.rjust(5)}"
w_results[num] = white_balls.count(num)
end
puts
puts "Powerballs"
puts "=================="
puts "Powerball Draws"
puts "--------- -----"
p_results = {}
powerballs.most_popular_n(powerballs.uniq.size).each do |num|
puts "#{num.to_s.rjust(9)}#{' '*4}#{powerballs.count(num).to_s.rjust(5)}"
p_results[num] = powerballs.count(num)
end
puts
puts "White ball histogram"
white_ball_histogram = w_results.collect{ |key, val| [key, val] }.sort_by{ |arr| arr.first }
puts AsciiCharts::Cartesian.new(white_ball_histogram, bar: true).draw
puts "Powerball histogram"
powerball_histogram = p_results.collect{ |key, val| [key, val] }.sort_by{ |arr| arr.first }
puts AsciiCharts::Cartesian.new(powerball_histogram, bar: true).draw