-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalAssignment.py
457 lines (398 loc) · 18.4 KB
/
finalAssignment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#global variables:
userInputFile = "" #input file that user chooses to gather data
listOfDraws = [] #list of draws, including all information (lall in provided code)
listOfDatesOfDraws = [] #list of dates of draws from listofdraws(lall)
listOfNumsDrawn = [] #list of draw numbers from listofdraws(lall)
listOfDrawJackpots = [] #list of draw jackpots in draws from listofdraws(lall)
listNumWinners = [] #list of number of winners in draws from listofdraws(lall)
userDataInput = "" #user input to determine whether to end, use all or sel data
numDraws = 0 #number of draws, used to determine range of for loops
userOutputFile = "" #output file that user selects to create
listTo49 = [] #list of number of times each number appears in draws
listOfNumsInRange = [] #list of frequency of nums in ranges
listResults = [] #list for output file (lout)
import turtle
def welcomeFunction():
#this is a void function, its purpose is to welcome the user to the system and to get the names of input files
print("Welcome to the CMPT 120 6-49 Processing System!")
print("===============================================")
print("You will first be asked to provide the INPUT file name; you will be asked to provide the OUTPUT file name later.")
print("The input file should be in this folder, the output file will be created in this folder.")
print("You will be able to provide new names for the files or accept the default names. Both files should have the extension .csv")
#userInputFile =input("Type x for file 'IN_data_draws3.csv', or type a new file name ==>")
#if userInputFile == "x":
# userInputFile = "IN_data_draws3.csv"
return
def inputFileSelection():
#this is a productive function that asks user input for the file of draws to be used, and returns that csv file
userInputFile =input("Type x for file 'IN_data_draws3.csv', or type a new file name ==>")
if userInputFile == "x":
userInputFile = "IN_data_draws3.csv"
return userInputFile
def read_csv_into_list_of_lists(IN_file):
'''
PROVIDED. CMPT 120
A csv file should be available in the folder (where this program is)
A string with the name of the file should be passed as argument to this function
when invoking it
(the string would include the csv extension, e.g "ID_data.csv")
'''
import csv
lall = []
print("\n.... TRACE - data read from the file\n")
with open(IN_file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for inrow in csv_reader:
print(".......",inrow)
lall.append(inrow)
return lall
def createSeparateLists(listDraws):
listOfDatesOfDraws = []
for i in range(len(listDraws)):
date = (listDraws[i][0])
listOfDatesOfDraws.append(date)
listOfNumsDrawn = []
for i in range(len(listDraws)):
innerListNums = []
for k in range(1,8):
number = listDraws[i][k]
innerListNums.append(number)
listOfNumsDrawn.append(innerListNums)
listOfDrawJackpots = []
for i in range(len(listDraws)):
jackpot = listDraws[i][8]
listOfDrawJackpots.append(jackpot)
listNumWinners = []
for i in range(len(listDraws)):
numWinners = listDraws[i][9]
listNumWinners.append(numWinners)
return listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners
def dataProcess():
#this is a productive function that asks the user for input to select the draws
print("Please choose one of three options: \n Type ALL to process all the data. \n Type SEL to process selected draws. \n Type END to end this program.\n")
validInputs = ["all", "sel", "end",]
userDataInputLocal = ""
while not(userDataInputLocal.lower() in validInputs):
userDataInputLocal = input("\nType ALL, SEL OR END (not case sensitive) ==>")
return userDataInputLocal
def userSelectAll(numDraws):
#this function changes the formatting of the list, to create a list of the numbers drawn within the list of Draws
listOfDraws = []
innerListDraws = []
for i in range(numDraws):
innerListDraws = [listOfDatesOfDraws[i], listOfNumsDrawn[i], listOfDrawJackpots[i], listNumWinners[i]]
listOfDraws.append(innerListDraws)
#print("TRACE !!", listOfDraws)
#print("\nTRACE!!", listOfDraws)
return listOfDraws
def monthFromDataToNum(dateFromList):
#this is a productive function that receives parameters. it takes a month written in date notation and turns it into the corresponding number
months = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
dashOne = dateFromList.index("-")
dashTwo = dashOne + 4
monthString = dateFromList[dashOne + 1: dashTwo]
monthNum = months.index(monthString)
return monthNum
def selectionByMonth(listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots,listNumWinners, numDrawswait ):
print("Please select a month. \n Only the draws associated to this month will be processed.")
userSelectedMonth = ""
validMonthsList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
while not(userSelectedMonth in validMonthsList):
userSelectedMonth = int(input("Please type a month number (1-12) ==>"))
#validation of user input^^
listOfMonthsNums = []
for i in range(len(listOfDatesOfDraws)):
monthNum = monthFromDataToNum(listOfDatesOfDraws[i])
#print("Trace !!", monthNum)
listOfMonthsNums.append(monthNum)
indicesOfUserInput = []
for i in range(len(listOfMonthsNums)):
if userSelectedMonth == listOfMonthsNums[i]:
indicesOfUserInput.append(i)
#print("Trace !!", i)
#print("trace !!", indicesOfUserInput)
numDraws = len(indicesOfUserInput)
listOfDraws = []
innerListDraws = []
for i in range(len(indicesOfUserInput)):
indexListDraws = [listOfDatesOfDraws[indicesOfUserInput[i]], listOfNumsDrawn[indicesOfUserInput[i]], listOfDrawJackpots[indicesOfUserInput[i]], listNumWinners[indicesOfUserInput[i]]]
listOfDraws.append(indexListDraws)
#print("\nTRACE !!",listOfDraws)
listOfDatesOfDraws = updateLists(indicesOfUserInput, listOfDatesOfDraws)
listOfNumsDrawn = updateLists(indicesOfUserInput, listOfNumsDrawn)
listOfDrawJackpots = updateLists(indicesOfUserInput, listOfDrawJackpots)
listNumWinners = updateLists(indicesOfUserInput, listNumWinners)
return listOfDraws, listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners, numDraws
def dateToDayOfWeek(dateFromList):
months = ["", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb"]
daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
dashOne = dateFromList.index('-')
dashTwo = dashOne + 4
k = int(dateFromList[0:dashOne])
monthString = dateFromList[dashOne + 1: dashTwo]
m = int(months.index(monthString))
year = int(dateFromList[dashTwo:]) + 2000
year1 = str(year)
D = int(year1[2:4])
if m == 11 or m == 12:
D = D - 1
C = 20 #because all years after 2000
f = k + ((13*m-1)/5) + D + (D/4) + (C/4) - 2*C
res = daysOfWeek[int(f%7)]
return res
def selectionByDay(listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots,listNumWinners, numDrawswait):
print("Please select a day of the week. \n Only the draws associated to this day will be processed.")
userSelectedDay = ""
validDaysList = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
while not(userSelectedDay.lower() in validDaysList):
userSelectedDay = (input("Please type a day of the week ==>"))
#validation of user input^^
selectedDay = validDaysList.index(userSelectedDay)
listOfDays = []
for i in range(len(listOfDatesOfDraws)):
dayOfWeek = dateToDayOfWeek(listOfDatesOfDraws[i])
print("Trace !!", dayOfWeek)
listOfDays.append(dayOfWeek)
print(listOfDays)
indicesOfUserInput = []
for i in range(len(listOfDays)):
if selectedDay == listOfDays[i]:
indicesOfUserInput.append(i)
print("Trace !!", i)
#print("trace !!", indicesOfUserInput)
numDraws = len(indicesOfUserInput)
listOfDraws = []
innerListDraws = []
for i in range(len(indicesOfUserInput)):
indexListDraws = [listOfDatesOfDraws[indicesOfUserInput[i]], listOfNumsDrawn[indicesOfUserInput[i]], listOfDrawJackpots[indicesOfUserInput[i]], listNumWinners[indicesOfUserInput[i]]]
listOfDraws.append(indexListDraws)
listOfDatesOfDraws = updateLists(indicesOfUserInput, listOfDatesOfDraws)
listOfNumsDrawn = updateLists(indicesOfUserInput, listOfNumsDrawn)
listOfDrawJackpots = updateLists(indicesOfUserInput, listOfDrawJackpots)
listNumWinners = updateLists(indicesOfUserInput, listNumWinners)
return listOfDraws, listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners, numDraws
def updateLists(indicesOfUserInput, listToUpdate):
print(indicesOfUserInput)
print(listToUpdate)
updatedList = []
for elem in indicesOfUserInput:
print(elem)
elem = int(elem)
updatedList.append(listToUpdate[elem])
print(updatedList)
return updatedList
def dataBeingProcessed(listOfDraws):
for i in range(len(listOfDraws)):
print("\n\n","Just to TRACE, the draw being processed is:")
print("Index #", i, "\n Date:", listOfDraws[i][0], "\n Numbers Drawn:", listOfDraws[i][1], "\n Jackpot:", listOfDraws[i][2], "\n Number of Winners", listOfDraws[i][3])
return
def averageJackpot():
listOfAverageJackpots = []
for i in range(len(listOfDrawJackpots)):
jackpot = int(listOfDrawJackpots[i])
numWinners = int((listNumWinners[i]))
if numWinners != 0:
averageJackpot = jackpot//numWinners
listOfAverageJackpots.append(averageJackpot)
else:
averageJackpot = 0
listOfAverageJackpots.append(averageJackpot)
#print("TRACE !!", listOfAverageJackpots)
return listOfAverageJackpots
def maxJackpot():
maxJackpot = 0
indexMax = 0
for i in range(len(listOfDrawJackpots)):
if int(listOfDrawJackpots[i]) > maxJackpot:
maxJackpot = int(listOfDrawJackpots[i])
indexMax = i
dateMaxJackpot = listOfDatesOfDraws[indexMax]
print("\nThe max jackpot was", maxJackpot)
print("The date of the max jackpot was", dateMaxJackpot)
return maxJackpot, dateMaxJackpot
def maxAverageJackpot(listOfAverageJackpots):
maxAverageJackpot = 0
indexMax = 0
for i in range(len(listOfAverageJackpots)):
if int(listOfAverageJackpots[i]) > maxAverageJackpot:
maxAverageJackpot = int(listOfAverageJackpots[i])
indexMax = i
dateMaxAverageJackpot = listOfDatesOfDraws[indexMax]
print("\nThe max average jackpot was", maxAverageJackpot)
print("The date of the max average jackpot was", dateMaxAverageJackpot)
return maxAverageJackpot,dateMaxAverageJackpot
def numTimesDrawn(lst):
print("Number of times each number was drawn:")
listTo49 = [0]*50
for elem in lst:
for num in elem:
number = int(num)
listTo49[number] = listTo49[number] + 1
return listTo49
def numsInRange(listNums):
print("number of numbers in each range - all selected draws considered ranges:")
print(listNums)
listRanges = [0] * 5
for i in range(len(listNums)):
for j in range(len(listNums[i])):
k = 0
while (int(listNums[i][j]) > k*10):
k += 1
listRanges[(k-1)] += 1
#print("TRACE !!", listRanges)
return listRanges
def sixMaxs(listNums):
print("Six most frequently drawn numbers:")
res = ""
for i in range(6):
maxNum = listNums[0]
pos = 0
for j in range(len(listNums)):
if listNums[j] > maxNum:
maxNum = listNums[j]
pos = j
print("number:", pos, "frequency:", maxNum)
res += "number:" + str(pos) + "frequency:" + str(maxNum) + "\n"
listNums[pos] = -1
maxNum = listNums[0]
pos = 0
return res
def turtleRangesDistribution():
validInputs = ["y", "n"]
userInput = ""
while not(userInput.lower() in validInputs):
userInput = input("Do you want to graph the ranges ranges distribution (Y/N) ==>")
if userInput.lower() == "y":
print("yes")
turtle.penup()
turtle.goto(-300,-200)
turtle.pendown()
turtle.pensize(5)
turtle.forward(20)
for i in range(len(listOfNumsInRange)):
turtle.forward(50)
turtle.left(90)
turtle.fillcolor('lightblue')
turtle.begin_fill()
turtle.forward(listOfNumsInRange[i]*50)
turtle.right(90)
turtle.forward(50)
turtle.left(90)
turtle.backward(listOfNumsInRange[i]*50)
turtle.left(90)
turtle.forward(50)
turtle.left(180)
turtle.forward(50)
turtle.end_fill()
turtle.forward(20)
else:
print("no")
return
## TO WRITE TO CSV OUTPUT FILE
def appendResToOutput(numDraws, listResults, listOfDatesOfDraws, listOfNumsDrawn, averageJackpot):
print(numDraws)
for i in range(numDraws):
listResults.append("'" + listOfDatesOfDraws[i] + "'" + ",")
newListNumsDrawn = []
newListNumsDrawn.append(listOfNumsDrawn[i])
listFrequency = numsInRange(newListNumsDrawn)
#print("trace!!", listFrequency)
for elmt in listFrequency:
listResults.append(str(elmt) + ",")
#print("trace!!",listResults)
avgJackpot = averageJackpot()[i]
listResults.append(str(avgJackpot) + "\n")
return
def outputFileSelection():
#this is a productive function that asks user input for the file of draws to be created for output, and returns that csv file
userOutputFile =input("Type x for OUTPUT file name 'OUT_results3.csv', or a new file name ==>")
if userOutputFile == "x":
userOutputFile = "OUT_results3.csv"
return userOutputFile
def writeOutputToFile(listResult,file_name):
'''
PROVIDED. CMPT 120
Assumptions:
1) lout is the list containing all the lines to be saved in the output file
2) file_name is the parameter receiving a string with the exact name of the output file
(with the csv extension, e.g "OUT_results.csv")
3) after executing this function the output file will be in the same
directory (folder) as this program
4) the output file contains just text representing one draw data per line
5) after each line in the file there is the character "\n"
so that the next draw is in the next line, and also
there is (one single) "\n" character after the last line
6) after the output file was created you should be able to open it
with Excell as well
'''
fileRef = open(file_name,"w") # opening file to be written
for line in listResult:
fileRef.write(line)
fileRef.close()
return
##TOP LEVEL
##==============================================================
##==============================================================
welcomeFunction() #introduces user to program
userInputFile = inputFileSelection() #user chooses input file
listOfDraws = read_csv_into_list_of_lists(userInputFile) #input file converted into list of multiple draw lists
print("Trace", listOfDraws)
numDraws = len(listOfDraws)
listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners = createSeparateLists(listOfDraws)
#print("\nTRACE !!",listOfDatesOfDraws,"\n")
#print("\nTRACE !!",listOfNumsDrawn,"\n")
#print("\nTRACE !!",listOfDrawJackpots,"\n")
#print("\nTRACE !!",listNumWinners,"\n")
userDataInput = dataProcess() #gets input from user about what data to process, then in following if/elif processes selected data.
if userDataInput.lower() == "end":
print("end") #trace
#proceed to end
else:
if userDataInput.lower() == "all":
print("\n\n All Data will be processed. \n================================================================")
#print(listOfDraws)
listOfDraws = userSelectAll(numDraws)
elif userDataInput.lower() == "sel":
print("\n\n Selected Data will be processed. \n================================================================") #trace
validInputsSelected = ["m", "d"] #d bonus!!!!!!
userSelectionMethod = ""
while not(userSelectionMethod.lower() in validInputsSelected):
userSelectionMethod = input("Do you want to select by month (M) or day of week (D)? ==>") #D BONUS!!!!!!
if userSelectionMethod.lower() == "m":
listOfDraws, listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners, numDraws = selectionByMonth(listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots,listNumWinners, numDraws)
elif userSelectionMethod.lower() == "d": #BONUS!
listOfDraws, listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots, listNumWinners, numDraws = selectionByDay(listOfDatesOfDraws, listOfNumsDrawn, listOfDrawJackpots,listNumWinners, numDraws)
userOutputFile = outputFileSelection()
dataBeingProcessed(listOfDraws)
print("\n\n","STATS:\n================================================================")
print("Draws Processed:", numDraws)
maxJackpot()
maxAverageJackpot(averageJackpot())
listTo49 = numTimesDrawn(listOfNumsDrawn)
print(listTo49)
listOfNumsInRange = numsInRange(listOfNumsDrawn)
print(listOfNumsInRange)
print(sixMaxs(listTo49))
##Appending to output
listResults=[]
appendResToOutput(numDraws, listResults, listOfDatesOfDraws, listOfNumsDrawn, averageJackpot)
##Writing it
writeOutputToFile(listResults,userOutputFile)
turtleRangesDistribution()
def dateToDayOfWeek(dateFromList):
months = ["", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb"]
daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
dashOne = dateFromList.index('-')
dashTwo = dashOne + 4
k = int(dateFromList[0:dashOne])
monthString = dateFromList[dashOne + 1: dashTwo]
m = months.index(monthString)
year = int(dateFromList[dashTwo:]) + 2000
D = year[2:4]
if m == 11 or m == 12:
D = D - 1
C = 20 #because all years after 2000
f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C
print(f)
res = daysOfWeek[f%7]
return res