Searching for Lottery drawing list of ticket match...
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Searching for Lottery drawing list of ticket match...
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,
On Aug 10, 11:35 pm, Miki Tebeka wrote:
> 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)]
--
http://mail.python.org/mailman/listinfo/python-list
How do I convert String into Date object
Is this the correct way to convert a String into a Date?
I only have dates and no time.
import time, datetime
oDate = time.strptime('07/27/2011', '%m/%d/%Y')
print oDate
Thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Re: How do I convert String into Date object
I have file of records delimited by spaces.
I need to import the date string and convert them into date datatypes.
'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'
I just discovered that my oDate is not an object, but a structure and
not a date datatype.
I'm stumped. Is there a way to convert a string into a date datatype
for comparisons, equality, etc?
Thanks,
On Aug 13, 3:14 pm, MrPink wrote:
> Is this the correct way to convert a String into a Date?
> I only have dates and no time.
>
> import time, datetime
>
> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> print oDate
>
> Thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Re: How do I convert String into Date object
BTW, here is the Python version I'm using.
Will this make a difference with the solutions you guys provided?
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Also, what editor do you guys use?
There are so many to chose from.
On Aug 13, 4:11 pm, Rafael Durán Castañeda
wrote:
> You can use datetime objects:
>
> >>> dt1 = datetime.datetime.strptime('07/27/2011',"%m/%d/%Y")
> >>> dt2 =datetime.datetime.strptime('07/28/2011',"%m/%d/%Y")
> >>> dt1 == dt2
> False
> >>> dt1 > dt2
> False
> >>> dt1 < dt2
> True
> >>> dt1 - dt2
> datetime.timedelta(-1)
>
> On 13/08/11 21:26, MrPink wrote:
>
>
>
>
>
>
>
> > I have file of records delimited by spaces.
> > I need to import the date string and convert them into date datatypes.
>
> > '07/27/2011' 'Event 1 Description'
> > '07/28/2011' 'Event 2 Description'
> > '07/29/2011' 'Event 3 Description'
>
> > I just discovered that my oDate is not an object, but a structure and
> > not a date datatype.
> > I'm stumped. Is there a way to convert a string into a date datatype
> > for comparisons, equality, etc?
>
> > Thanks,
>
> > On Aug 13, 3:14 pm, MrPink wrote:
> >> Is this the correct way to convert a String into a Date?
> >> I only have dates and no time.
>
> >> import time, datetime
>
> >> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> >> print oDate
>
> >> Thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Re: How do I convert String into Date object
I found this solution.
Python: Convert String Date to Date Object
http://slaptijack.com/programming/python-convert-string-date-to-date-object/
On Aug 13, 3:14 pm, MrPink wrote:
> Is this the correct way to convert a String into a Date?
> I only have dates and no time.
>
> import time, datetime
>
> oDate = time.strptime('07/27/2011', '%m/%d/%Y')
> print oDate
>
> Thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Searching Sets (Lottery Results)
This is a continuation of my pursuit to learn Python. I have been tinkering
with this for a number of years and I am back at it again. I am stuck and need
some guidance.
This is related to other posts that I have made in the past.
For example: Searching for Lottery drawing list of ticket match:
https://groups.google.com/forum/#!topic/comp.lang.python/sHLPrfmY3q4
I have a text file with the results of lottery drawing like so:
Draw Date WB1 WB2 WB3 WB4 WB5 PB PP
01/02/2016 42 15 06 05 29 10 2
12/30/2015 12 61 54 38 36 22 3
12/26/2015 65 40 44 59 27 20 2
12/23/2015 67 16 63 38 55 25 4
12/19/2015 30 68 59 41 28 10 2
12/16/2015 09 42 10 55 32 06 2
12/12/2015 62 02 30 19 14 22 2
12/09/2015 16 46 10 56 07 01 2
12/05/2015 47 33 68 27 13 13 2
12/02/2015 14 18 19 64 32 09 2
11/28/2015 47 02 66 67 06 02 3
11/25/2015 53 16 69 58 29 21 2
11/21/2015 37 57 47 50 52 21 3
11/18/2015 40 17 46 69 41 06 2
11/14/2015 66 37 22 14 45 05 3
11/11/2015 26 04 32 55 64 18 3
11/07/2015 50 53 07 16 25 15 2
11/04/2015 12 02 17 20 65 17 4
10/31/2015 09 47 20 25 68 07 2
10/28/2015 56 62 54 63 04 10 2
10/24/2015 20 31 56 64 60 02 3
10/21/2015 57 32 30 42 56 11 4
I load the lottery drawings into memory for searching with the following code
although, it is incomplete. I am stuck and need some guidance.
The set datatype seems to be the best for searching, but how best can I
implement it?
And I want the results to highlight the numbers that were matched. For
example, if the white balls in the drawing are:
"42 15 06 05 29"
AND the numbers on the lottery ticket are:
"06 15 32 42 56"
THEN the display might look like:
"06* 15* 32 42* 56"
WHERE * signifies a match.
###
from datetime import datetime
class Powerball(object):
"""Summary of class here.
Longer class information. . .Longer
Attributes:
fileName: File name to load drawing from.
"""
# class Constants
_DATE_FORMAT = '%m/%d/%Y'
# class variables
fileName = 'pb.txt'
# class initialization
def __init__(self, pFileName):
"""Return a Powerball Object with a set of winning drawings.
:param pFileName:
"""
self.fileName = pFileName
# Open file and load drawing data sets.
def readFileData(self):
"""File to open and load data from. """
f = open(self.fileName, 'r')
# read the whole file into a list of lines.
lines = f.readlines()
f.close()
# For each line in the list of lines. . .
for line in lines[1:50]:
# split the string on whitespace into a list of strings
fields = line.split()
d = datetime.strptime(fields[0], self._DATE_FORMAT).date()
# Use list comprehension to create a frozenset.
wb = frozenset(int(num_str) for num_str in fields[1:6])
pb = int(fields[6])
t = tuple([wb, pb, d])
# Store t into a data structure for searching later. . .
# Not sure of best way to do this. . .
p = Powerball("pb.txt")
p.readFileData()
#
--
https://mail.python.org/mailman/listinfo/python-list
Re: Searching Sets (Lottery Results)
On Monday, February 8, 2016 at 7:05:24 PM UTC-5, Chris Angelico wrote: > On Tue, Feb 9, 2016 at 8:45 AM, MrPink wrote: > > I load the lottery drawings into memory for searching with the following > > code although, it is incomplete. I am stuck and need some guidance. > > > > The set datatype seems to be the best for searching, but how best can I > > implement it? > > > > And I want the results to highlight the numbers that were matched. For > > example, if the white balls in the drawing are: > > "42 15 06 05 29" > > > > AND the numbers on the lottery ticket are: > > "06 15 32 42 56" > > > > THEN the display might look like: > > "06* 15* 32 42* 56" > > > > WHERE * signifies a match. > > > > This suggests that there is an order to the numbers on your ticket > (you want to print them out in the same order), but not to the winning > numbers, which are simply a set. The easiest way to handle that would > be to iterate over your numbers, asking "if number in > winning_numbers:", and printing out a "match" marker if it is or a > "non-match" marker if it isn't. > > ChrisA Thanks Chris. Very good point. I was just too deep in the weeds to see that simple solution. I was overthinking it. ;-) Sincerely, -- https://mail.python.org/mailman/listinfo/python-list
Reading a file into a data structure....
This is a continuing to a post I made in August:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b072cfadf998deae/ce6d4d09911e4107?lnk=gst&q=MrPink#ce6d4d09911e4107
I got some free time to work with Python again and have some followup
questions.
For example, I have a list in a text file like this:
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
Ticket:
startdate,enddate,wb,wb,wb,wb,wb,bb
4/1/2011,8/1/2011,5,23,32,21,3,27
I am trying to determine the optimal way to organize the data
structure of the drawing list, search the drawing list, and mark the
matches in the drawing list.
f = open("C:\temp\drawinglist.txt", "r")
lines = f.readlines()
f.close()
drawing = lines[1].split()
The results in drawing is this:
drawing[0] = '4/1/2011'
drawing[1] = '5'
drawing[2] = '1'
drawing[3] = '45'
drawing[4] = '23'
drawing[5] = '27'
drawing[6] = '27'
I need to convert drawing[0] to a date datatype. This works, but I'm
sure there is a better way.
from datetime import date
month, day, year = drawing[0].split('/')
drawing[0] = date(int(year), int(month), int(day))
For searching, I need to determine if the date of the drawing is
within the date range of the ticket. If yes, then mark which numbers
in the drawing match the numbers in the ticket.
ticket[0] = '4/1/2011'
ticket[0] = '8/1/2011'
ticket[0] = '5'
ticket[0] = '23'
ticket[0] = '32'
ticket[0] = '21'
ticket[0] = '3'
ticket[0] = 27'
drawing[0] = '4/1/2011' (match)
drawing[1] = '5' (match)
drawing[2] = '1'
drawing[3] = '45'
drawing[4] = '23' (match)
drawing[5] = '27'
drawing[6] = '27' (match)
I'm debating on structuring the drawing list like this:
drawing[0] = '4/1/2011'
drawing[1][0] = '5'
drawing[1][1] = '1'
drawing[1][2] = '45'
drawing[1][3] = '23'
drawing[1][4] = '27'
drawing[2] = '27'
Sort drawing[1] from low to high
drawing[1][0] = '1'
drawing[1][1] = '5'
drawing[1][2] = '23'
drawing[1][3] = '27'
drawing[1][4] = '45'
I want to keep the drawing list in memory for reuse.
Any guidance would be most helpful and appreciated.
BTW, I want to learn, so be careful not to do too much of the work for
me.
I'm using WingIDE to do my work.
Thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Can I search a list for a range of values?
I have a list like so: a = [2,4,5,6,3,9,10,34,39,59,20,15,13,14] I would like to get a subset from the list with value between 10 and 20 (inclusive). b = [10,13,15,14,20] Is there a way to do this with a list or other data type? Thanks, -- http://mail.python.org/mailman/listinfo/python-list
How to test if object is an integer?
Is there a function in Python that can be used to test if the value in a string is an integer? I had to make one up for myself and it looks like this: def isInt(s): try: i = int(s) return True except ValueError: return False -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading a file into a data structure....
This is what I have been able to accomplish:
def isInt(s):
try:
i = int(s)
return True
except ValueError:
return False
f = open("powerball.txt", "r")
lines = f.readlines()
f.close()
dDrawings = {}
for line in lines:
if isInt(line[0]):
t = line.split()
d = t[0]
month,day,year = t[0].split("/")
i = int(year + month + day)
wb = t[1:6]
wb.sort()
pb = t[6]
r = {'d':d,'wb':wb,'pb':pb}
dDrawings[i] = r
The dictionary dDrawings contains records like this:
dDrawings[19971101]
{'pb': '20', 'd': '11/01/1997', 'wb': ['22', '25', '28', '33', '37']}
I am now able to search for ticket in a date range.
keys = dDrawings.keys()
b = [key for key in keys if 20110909 <= key <= 20111212]
How would I search for matching wb (White Balls) in the drawings?
Is there a better way to organize the data so that it will be flexible
enough for different types of searches?
Search by date range, search by pb, search by wb matches, etc.
I hope this all makes sense.
Thanks,
On Oct 13, 7:42 pm, Jon Clements wrote:
> On Oct 13, 10:59 pm,MrPink wrote:
>
>
>
>
>
>
>
>
>
> > This is a continuing to a post I made in
> > August:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> > I got some free time to work with Python again and have some followup
> > questions.
>
> > For example, I have a list in a text file like this:
> > 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
> >
>
> > Ticket:
> > startdate,enddate,wb,wb,wb,wb,wb,bb
> > 4/1/2011,8/1/2011,5,23,32,21,3,27
>
> > I am trying to determine the optimal way to organize the data
> > structure of the drawing list, search the drawing list, and mark the
> > matches in the drawing list.
>
> > f = open("C:\temp\drawinglist.txt", "r")
> > lines = f.readlines()
> > f.close()
> > drawing = lines[1].split()
>
> > The results in drawing is this:
> > drawing[0] = '4/1/2011'
> > drawing[1] = '5'
> > drawing[2] = '1'
> > drawing[3] = '45'
> > drawing[4] = '23'
> > drawing[5] = '27'
> > drawing[6] = '27'
>
> > I need to convert drawing[0] to a date datatype. This works, but I'm
> > sure there is a better way.
> > from datetime import date
> > month, day, year = drawing[0].split('/')
> > drawing[0] = date(int(year), int(month), int(day))
>
> > For searching, I need to determine if the date of the drawing is
> > within the date range of the ticket. If yes, then mark which numbers
> > in the drawing match the numbers in the ticket.
>
> > ticket[0] = '4/1/2011'
> > ticket[0] = '8/1/2011'
> > ticket[0] = '5'
> > ticket[0] = '23'
> > ticket[0] = '32'
> > ticket[0] = '21'
> > ticket[0] = '3'
> > ticket[0] = 27'
>
> > drawing[0] = '4/1/2011' (match)
> > drawing[1] = '5' (match)
> > drawing[2] = '1'
> > drawing[3] = '45'
> > drawing[4] = '23' (match)
> > drawing[5] = '27'
> > drawing[6] = '27' (match)
>
> > I'm debating on structuring the drawing list like this:
> > drawing[0] = '4/1/2011'
> > drawing[1][0] = '5'
> > drawing[1][1] = '1'
> > drawing[1][2] = '45'
> > drawing[1][3] = '23'
> > drawing[1][4] = '27'
> > drawing[2] = '27'
>
> > Sort drawing[1] from low to high
> > drawing[1][0] = '1'
> > drawing[1][1] = '5'
> > drawing[1][2] = '23'
> > drawing[1][3] = '27'
> > drawing[1][4] = '45'
>
> > I want to keep the drawing list in memory for reuse.
>
> > Any guidance would be most helpful and appreciated.
> > BTW, I want to learn, so be careful not to do too much of the work for
> > me.
> > I'm using WingIDE to do my work.
>
> > Thanks,
>
> - Use the csv module to read the file
> - Use strptime to process the date field
> - Use a set for draw numbers (you'd have to do pure equality on the
> bb)
> - Look at persisting in a sqlite3 DB (maybe with a custom convertor)
>
> hth,
>
> Jon.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Reading a file into a data structure....
I did not understand what a tuple was.
So it was very hard for me to understand what a namedtuple was and
follow the code.
Then I looked up the word at dictionary.com and got this:
http://dictionary.reference.com/browse/tuple
tuple: computing a row of values in a relational database
Now I understand a little better what a tuple is and can follow the
code better.
A namedtuple seems like a dictionary type. I'll need to read up on
the difference between the two.
Thanks again.
On Oct 15, 12:47 am, Chris Rebert wrote:
> On Fri, Oct 14, 2011 at 7:59 PM,MrPink wrote:
> > This is what I have been able to accomplish:
>
> > def isInt(s):
> > try:
> > i = int(s)
> > return True
> > except ValueError:
> > return False
>
> > f = open("powerball.txt", "r")
> > lines = f.readlines()
> > f.close()
>
> > dDrawings = {}
> > for line in lines:
> > if isInt(line[0]):
> > t = line.split()
> > d = t[0]
> > month,day,year = t[0].split("/")
> > i = int(year + month + day)
> > wb = t[1:6]
> > wb.sort()
> > pb = t[6]
> > r = {'d':d,'wb':wb,'pb':pb}
> > dDrawings[i] = r
>
> > The dictionary dDrawings contains records like this:
> > dDrawings[19971101]
> > {'pb': '20', 'd': '11/01/1997', 'wb': ['22', '25', '28', '33', '37']}
>
> > I am now able to search for ticket in a date range.
> > keys = dDrawings.keys()
> > b = [key for key in keys if 20110909 <= key <= 20111212]
>
> > How would I search for matching wb (White Balls) in the drawings?
>
> > Is there a better way to organize the data so that it will be flexible
> > enough for different types of searches?
> > Search by date range, search by pb, search by wb matches, etc.
>
> > I hope this all makes sense.
>
> from datetime import datetime
> from collections import namedtuple, defaultdict
> # for efficient searching by date: import bisect
>
> DATE_FORMAT = "%m/%d/%Y"
> Ticket = namedtuple('Ticket', "white_balls powerball date".split())
>
> powerball2ticket = defaultdict(set)
> whiteball2ticket = defaultdict(set)
> tickets_by_date = []
>
> with open("powerball.txt", "r") as f:
> for line in f:
> if not line[0].isdigit():
> # what are these other lines anyway?
> continue # skip such lines
>
> fields = line.split()
>
> date = datetime.strptime(fields[0], DATE_FORMAT).date()
> white_balls = frozenset(int(num_str) for num_str in fields[1:6])
> powerball = int(fields[6])
> ticket = Ticket(white_balls, powerball, date)
>
> powerball2ticket[powerball].add(ticket)
> for ball in white_balls:
> whiteball2ticket[ball].add(ticket)
> tickets_by_date.append(ticket)
>
> tickets_by_date.sort(key=lambda ticket: ticket.date)
>
> print(powerball2ticket[7]) # all tickets with a 7 powerball
> print(whiteball2ticket[3]) # all tickets with a non-power 3 ball
>
> Cheers,
> Chris
> --http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
