Automating a daily task in Python (BINGO)

This is a demonstrate of how you can completely eliminate small daily tasks entirely using python.

At the company I work for currently every employee is given a bingo board and every day the warehouse does without a loss time accident a bingo number is called. Each time someone wins they get a small cash prize and the numbers reset.

Now admittedly the odds of me winning are very slim in fact I have not won having been here 2 years now. But being the type of person I am I cannot just let the odds stop me from trying so i continue playing. Every day I pull out my bingo board and check it against the numbers that were called. Now lets say this takes me on average 30 seconds to 1 minute (these number lists can get big and I always recheck each number so i never miss a possible bingo). Quickly doing the math if i spend on average 3/4 a minute every day checking my bingo card. Which equates to an estimated 3.125 hours a year! Ok well maybe that is not very much but if you multiply that by every employee at AFTCO that is a more alarming number north of 300 hours.

Problem to solve: Remove the need to check my bingo card ever again.

The biggest issues I faced when taking on this challenge was checking to see if some list data was in another list. And more specifically whether every item in set1 matches existed item in set2. After some exceptionally long nested for loops I found an incredible set specific function that does exactly what I needed! If you have never heard of it before check it out its called issuperset and you can find out more here. Everything else was pretty straight forward list manipulation. I saved all possible solutions in a list of sets and used a set I derived from a for loop that iterated over every called number. Adding the correct hit when a called number hit a players game card.

So without further ado I will share my code. I should also state that I am lucky the process of sharing numbers each day is placed on a externally available web server which makes life easy.

Project name: AFTCOBingoPlayer
Programmed by: Jordon Lovik
Date: 3/12/17
import urllib.request
import urllib.parse
import re
import os

savelocation = 'C:\\Users\\localuser\\Desktop\\bingofile.txt'
bingofile = open(savelocation, 'w')
url = ''
resp = urllib.request.urlopen(url) #request html data from website
respdata = #store request data in respdata
bingodict = {
'Jordon':      ['B8,', 'I18,', 'N38,', 'G47,', 'O66,',
                'B10,','I27,', 'N44,', 'G53,', 'O71,',
                'B3,', 'I19,', 'fre,', 'G48,', 'O67,',
                'B4,', 'I25,', 'N39,', 'G56,', 'O65,',
                'B9,', 'I30,', 'N34,', 'G58,', 'O69,'],

'Rick': ['B8,', 'I24,', 'N41,', 'G52,', 'O72,',
                'B2,', 'I19,', 'N45,', 'G50,', 'O62,',
                'B11,', 'I20,', 'fre,', 'G46,', 'O70,',
                'B13,', 'I23,', 'N39,', 'G57,', 'O64,',
                'B14,', 'I26,', 'N36,', 'G48,', 'O68,'],

'Stephen': ['B3,', 'I17,', 'N41,', 'G53,', 'O61,',
                'B7,', 'I18,', 'N39,', 'G60,', 'O62,',
                'B1,', 'I21,', 'fre,', 'G47,', 'O73,',
                'B14,', 'I23,', 'N37,', 'G49,', 'O70,',
                'B15,', 'I19,', 'N43,', 'G52,', 'O68,'],

'Aleck': ['B6,', 'I29,', 'N32,', 'G51,', 'O75,',
           'B8,', 'I20,', 'N43,', 'G55,', 'O65,',
           'B14,', 'I30,', 'fre,', 'G48,', 'O66,',
           'B7,', 'I23,', 'N34,', 'G52,', 'O68,',
           'B3,', 'I19,', 'N41,', 'G59,', 'O61,'],

'Sam': ['B1,', 'I26,', 'N37,', 'G50,', 'O71,',
           'B3,', 'I28,', 'N33,', 'G53,', 'O73,',
           'B2,', 'I22,', 'fre,', 'G57,', 'O68,',
           'B9,', 'I21,', 'N34,', 'G46,', 'O70,',
           'B13,', 'I29,', 'N45,', 'G52,', 'O63,']

for key, value in bingodict.items():
    string = '<TEXTAREA ROWS="2" NAME="1 ANNOUNCEMENTS-return" COLS="50" WRAP=VIRTUAL>(.*?)</TEXTAREA>'#search within text area for bingo numbers
    search = re.findall(string, str(respdata)) #find all data between <b> and </b>
    searchstring = str(search) #convert search results to string
    cleanlist = searchstring.split() #split seachstring into a list object
    cleanlist.append('fre,') #add free space

    solutions = [(0,6,12,18,24),(4,8,12,16,20),(0,5,10,15,20),(1,6,11,16,21),

    counter = 0 #inicialize counter variable
    hitlist = set() #inicialize hitlist variable
    print('-------------')# formating
    print(key)# print the Bingo board name
    boardprint = [] #inicialize empty list which will hold any hits found in someones board

    for i in value: #index into each Bingo board
        boardprint.append('O')# append O to all spaces from 0-24
        for j in cleanlist: #Index into cleanlist i.e item called
            if i == j: #check to see if someones board item matches an item called by game master
                hitlist.add(counter)#When hit occurs add it to our hitlist which holds all items
                #print('Hit: {} '.format(i)) #debuging
                boardprint.pop()# removes the last item in list which will always be an 'O'
                boardprint.append('X')# adds 'X' in place of said 'O'
        counter += 1 #add one to counter

    for each in solutions: #for each item in solutions
        if hitlist.issuperset(each):
            #if collections.Counter(hitlist) == collections.Counter(each): #check to see if all items match
            print('!**BINGO**! {}'.format(key))
            bingofile.write('!**BINGO**! {}'.format(key))#write
            os.startfile(savelocation)#Open file for presenting a bingo winner

    for v in range(25):
        if v == 0:#print Header
            print('B ', 'I ', 'N ', 'G ', 'O')
        if v < 5:# 0 - 4 Row1
            print(boardprint[v], end='  ')
        if v == 5:#add spacing
        if v > 4 and v < 10:# 5 - 9
            print(boardprint[v], end='  ')
        if v == 10:#add spacing
        if v > 9 and v < 15: #10 - 14
            print(boardprint[v], end='  ')
        if v == 15:#add spacing
        if v > 14 and v < 20: #15 - 19
            print(boardprint[v], end='  ')
        if v == 20:#add spacing
        if v > 19: #20 - 24
            print(boardprint[v], end='  ')
        if v == 24:#add spacing



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s