Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Searching for Lottery drawing list of ticket match...

22 views
Skip to first unread message

MrPink

unread,
Aug 10, 2011, 2:55:27 PM8/10/11
to
I need a little nudge into the right direction with this problem.
As an exercise for me to learn about Python, I am trying to determine
the best way to search a list of "lottery drawings" for a match with a
lottery ticket.

Possible numbers for a drawing are:
5 whiteballs (wb): 1-55
1 blackball (bb): 1-49

example: wb1, wb2, wb3, wb4, wb5, bb

Example list of lottery drawings:
date,wb,wb,wb,wb,wb,bb
4/1/2011,5,1,45,23,27,27
5/1/2011,15,23,8,48,22,32
6/1/2011,33,49,21,16,34,1
7/1/2011,9,3,13,22,45,41
8/1/2011,54,1,24,39,35,18
...

Typically a lottery ticket can have multiple combinations for the
ticket to be a winner. For example:
2 wb, 1 bb
3 wb
3 wb, 1 bb
4 wb
4 wb, 1 bb
5 wb
5 wb, 1 bb (jackpot winner)

An object oriented solution might be to create a list of "Drawing"
objects and then loop through the list to find a match for the ticket
object. For example:
if oDrawing[x] == ticket then "Do XYZ"

Or I could just perform this task with procedures and loops and skip
the object solution idea.

Do Python developers have a preference?
Would it be worth the overhead to initialize a list of drawing objects
to search through?

There is no database back-end, so no SQL, etc.

I hope all that makes sense.
Thanks for you help.

Miki Tebeka

unread,
Aug 10, 2011, 11:35:54 PM8/10/11
to
Python built in types are enough for this problem IMO. You can use sets of tuples to specify ticket and drawings and then just do set intersection.

Say the drawing is set([(5, 'wb'), (1, 'wb'), (45, 'wb'), (23, 'wb'), (27, 'wb')]) (keeping black ball out). The you can create a score function:

Side note: I might used a namedtuple to have drawing.whites, drawing.black.

def score(ticket_whites, ticket_black, drawing_whites, drawing_black):
num_whites = ticket_whites & drawing_whites
black_matching = ticket_black == drawing_black
return {
(2, True) : 1,
(3, False) : 2,
(3, True) : 3,
(4, False) : 4,
(4, True) : 5,
(5, False) : 6,
(5, True) : 10
}[(num_whites, black_matching)]

MrPink

unread,
Aug 12, 2011, 4:47:40 PM8/12/11
to
Boy, that was a lot more elegant that I would have thought.
Though hard for a greenhorn like me to really understand how the
assignment are working, etc.

Anyway, what are these kind of statement call so that I can read up
more on this?
What Python feature are you using?

num_whites = ticket_whites & drawing_whites
black_matching = ticket_black == drawing_black

Thanks,

Miki Tebeka

unread,
Aug 12, 2011, 5:31:13 PM8/12/11
to
0 new messages