Blog
Posts tagged with "olympics".
Olympic Tickets Predictor - Code Review
Sid
17 Jun 2011 20:13
After getting a mail from London 2012 telling me that they would tell me what tickets we’d got soon I finally broke and decided that I needed to work out what tickets we’d got now!
I knew how much we’d been charged and the events that we’d applied for. Also, it seems that there is a 6 quid charge to deliver the tickets (I’m expecting my postman dressed as a traffic cop for that) so that means it was just a case of working out which combinations of tickets add up to that.
This is another example of where Ruby makes something that could be tedious or complicated nice and easy by having methods on their collections that make this a breeze – namely Array.combinations(1) where you can do “n Choose p” calculations and also Array.inject which makes summing easy. Data lines aside there’s probably more lines of comments than of code.
The outcome was just a single combination (there are either-ors as some of events are priced the same):
- Athletics
- Volleyball or Beach Volleyball
- Rowing or Basketball
- Football
which is good news as I was really hoping for athletics (and beach volleyball)! I’ve looked through the code a few times and can’t find any issues but any errors gratefully received before I get too excited!
# olympic tickets helper
events={}
# populate a hash of event prices and the event(s)
#
events[160]="(volley or beach)"
events[500]="athletics"
events[380]="athletics"
events[120]="football"
events[200]="cycling"
events[220]="bball"
events[260]="athletics"
events[134]="(rowing or bball)"
# pull the prices out
prices=events.keys
# the total taken from the credit card
total = 920
# according to the website 6 quid is taken off for delivery (what a rip-off!)
delivery_charge = 6
# the price of the tickets is the remainder
remainder = total - delivery_charge
#
# this loop controls the number of combinations from the 8 price events
# we know that no single event is over the remainder so start from 2. we also know
# that all 8 are over the remainder (as probably are 7 but too tiresome to work out) so
# only need to work out from 8 choose 2 to 8 choose 7
#
for i in (2 .. 7)
# prices choose i ... turn to an array to check each
possibles = prices.combination(i).to_a
# for each of the combinations
possibles.each do |posse|
# ... find the sum of the prices and check whether they match the remainder
if (posse.inject(0) {|sum,v| sum+v}) == remainder
# if they do then they are a combo (possible cos there are multiple events at the same price)
puts "Possible combo:"
posse.each do |p|
puts "#{events[p]} @ #{p}"
end
end #end if
end # end inner
end # end outer
# don't forget the delivery charge
puts "delivery @ 6"
Update 20/7/2011
We ended up with Athletics, Volleyball, Rowing, and Football. So good from a code point of view not so good as I’d convinced myself that we’d be going to the basketball! Still very lucky to get the tickets and really looking forward to it!

