A Housie card has 3 rows x 9 columns of numbers. The numbers that can appear under each column are 1–9, 10–19, 20–29, 30–39, 40–49, 50–59, 60–69, 70–79 & 80–90. WAPS to create 10 random Housie cards and write the same to a CSV file.
import random, csv
def generateTicket():
# 4 cols with 1 & 2 nos, 1 col with 3 nos
LL = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 1, 1],
[1, 1, 0], [1, 1, 0], [1, 1, 1]]
random.shuffle(LL)
# Range of numbers in each column
nums = [list(range(1, 10)), list(range(10, 20)), list(range(20, 30)),
list(range(30, 40)), list(range(40, 50)), list(range(50, 60)),
list(range(60, 70)), list(range(70, 80)), list(range(80, 91))]
for i in range(9):
random.shuffle(nums[i]) # Shuffle the numbers
ascending = sorted(nums[i][:3]) # Sort only the 3 nos. to be displayed
for j in range(3):
nums[i][j] = ascending[j]
# Where the numbers will be stored
bingo = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1],
[0, 1, 1], [1, 1, 0], [1, 1, 0], [1, 1, 1]]
for i in range(9):
for j in range(3):
bingo[i][j] = "" if LL[i][j] == 0 else nums[i][j]
return bingo
def transpose(L1):
L2 = []
for i in range(len(L1[0])):
row = []
for item in L1:
row.append(item[i])
L2.append(row)
return L2
def createCSV():
bingo = generateTicket()
bingo = transpose(bingo)
with open("Bingo1.csv", 'a', newline='') as out:
W = csv.writer(out)
W.writerow("")
for i in bingo:
W.writerow(i)
W.writerow("")
for i in range(10): # Create 10 tickets
createCSV()
No comments:
Post a Comment