Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
14 kB
2
Indexable
Never
def read_csv_into_list_of_lists(IN_file):
    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 convert_lall_to_separate_lists(lall):
    for index in range(len(lall)):
        inner_list = lall[index]
        print("JUST TO TRACE, the draw being processed is:")
        print("index #", index)
        print("date", inner_list[0])
        print("numbers drawn", inner_list[1:8])
        print("jackpot", inner_list[8])
        print("number of winners", inner_list[-1])

def convert_lall2_to_separate_lists(lall2):
    for index in range(len(lall2)):
        inner_list = lall2[index]
        print("JUST TO TRACE, the draw being processed is:")
        print("index #", index)
        print("date", inner_list[0])
        print("numbers drawn", inner_list[1:8])
        print("jackpot", inner_list[8])
        print("number of winners", inner_list[-1])

def filtrate(func: callable, data: list) -> list:
    filtrated = []
    for i in data:
        if func(i):
            filtrated.append(i)
    return filtrated


def num_ranges(numbers: list):
    num_range = [0] * 5

    def first_num_range(x: int) -> bool:
        return 0 < int(x) <= 10

    def second_num_range(x: int) -> bool:
        return 10 < int(x) <= 20

    def third_num_range(x: int) -> bool:
        return 20 < int(x) <= 30

    def fourth_num_range(x: int) -> bool:
        return 30 < int(x) <= 40

    def five_num_range(x: int) -> bool:
        return 40 < int(x) <= 50

    num_range[0] = len(list(filtrate(first_num_range, numbers)))
    num_range[1] = len(list(filtrate(second_num_range, numbers)))
    num_range[2] = len(list(filtrate(third_num_range, numbers)))
    num_range[3] = len(list(filtrate(fourth_num_range, numbers)))
    num_range[4] = len(list(filtrate(five_num_range, numbers)))

    return num_range


def get_average_paid(jackpot_size: int, winners_number: int) -> float:
    return int(jackpot_size) / int(winners_number) if int(winners_number) > 0 else 0


def tail(number: int, iterable: list) -> list:
    tailed = []
    length = len(iterable)
    for index in range(length - number, length):
        tailed.append(iterable[index])
    return tailed


def get_numbers(iterable: list) -> list:
    return [iterable[index] for index in range(1, len(iterable) - 2)]


def show_output(data: list) -> list:
    lout = []
    for inner_list in data:
        jackpot = tail(2, inner_list)[0]#find two last digits in lists
        winners = tail(2, inner_list)[1]
        date = inner_list[0]
        data = num_ranges(get_numbers(inner_list))
        average_paid = get_average_paid(jackpot, winners)
        lout.append("'" + date + "'" + ",")
        lout.append(str(data) + ",")
        lout.append(str(average_paid) + "\n")
        print(date, data, average_paid)
    return lout


def write_list_of_output_lines_to_file(lout, file_name):
    fileRef = open(file_name, "w")
    for line in lout:
        fileRef.write(line)

    fileRef.close()
    return

def countALLlist(lst):
    res = [0]*50
    for st in lst:
        for ch in st[1:8]:
            ndig = int(ch)
            res[ndig] +=1
    return res
def oneNumRange(lst):
    lres=[0]*5
    for st in lst:
     for ch in st[1:8]:
       ndig=int(ch)
       k=0
       while (ndig>k*10):
         k=k+1
       lres[(k-1)]+=1
    return lres
#TOPLEVEL
from collections import Callable
mon = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11,
       'Dec': 12}
print("You will be asked to provide the output file name later")
print("The input file should be in this  folder")
print("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")
datafile= input("Type a file name you wish to input==>")
lall = read_csv_into_list_of_lists(datafile)
print("Please choose one of three options:")
print("Type ALL to process all the data")
print("Type SEL to process selected draws")
print("Type END to end this program")
while True:
        selcrit = input("Type ALL, SEL or END ==>")
        if selcrit == "ALL" or selcrit == 'all':
            file_name = input("Type a new file name for OUTPUT file ==>")
            print("============= ALL the data will be processed ============")
            convert_lall_to_separate_lists(lall)
            print("TRACING: Here is the output saved to the file!")
            lout = show_output(lall)
            write_list_of_output_lines_to_file(lout, file_name)
            print("=========== STATS: ===========")
            print("draw processed", len(lall))
            print()
            jackpots = []
            for st in lall:
                dig = int(st[8])
                jackpots.append(dig)
            dates = []
            for st in lall:
                dates.append(st[0])
            print("max jackpot", max(jackpots))
            ind = jackpots.index(max(jackpots))
            print("date max jackpot", dates[ind])
            print()
            averages = []
            for innerlist in lall:
                jackpot = tail(2, innerlist)[0]
                winners = tail(2, innerlist)[1]
                average_paid = get_average_paid(jackpot, winners)
                averages.append(average_paid)
            print("max average won", max(averages))
            ind1 = averages.index(max(averages))
            print("date max average won", dates[ind1])
            print()
            print("number of times each number was drawn")
            pen = countALLlist(lall)
            print(countALLlist(lall))
            print()
            print(
                "number of numbers in each range - all selected draws considered ranges: (0,10], (10,20], (20,30], (30,40], (40,50)")
            print(oneNumRange(lall))
            print()
            print("Six most frequently drawn numbers")
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            pen[pos] = -1
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            pen[pos] = -1
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            pen[pos] = -1
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            pen[pos] = -1
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            pen[pos] = -1
            maxi = pen[0]
            pos = 0
            for k in range(len(pen)):
                if (pen[k] > maxi):
                    maxi = pen[k]
                    pos = k
            print("number", str(pos), "was drawn", str(maxi), "times")
            continue

        if selcrit == "SEL" or selcrit == "sel":
            print("============= SELECTED the data will be processed ============")

            print("Please select a month")
            print("Only the draws associated to this month will be processed")
            while True:
                try:
                    a = []
                    monlist = []
                    coun = 0
                    ind = []
                    indx = 0
                    lall2 = []
                    selchoice = int(input('Please type a month number (1 to 12)'))
                except ValueError:#if not int it reurns valuerror
                    print('That was not an integer. Please retype')
                    continue
                else:
                    if 0 < selchoice > 12:
                        print("The month number is out of range. Please retype")
                        continue
                    else:
                        lst2 = [item[0] for item in lall]
                        for item in lst2:
                            word = item.split('-')
                            monlist.append(word[1])

                            # for iteml in a:
                            #     for i in iteml:
                            #         c.append(i)

                        for item in monlist:
                            indx = indx + 1
                            if selchoice == mon.get(item):
                                ind.append((indx - 1))
                                coun = monlist.count(item)
                                print(lall[indx - 1])
                                lall2.append(lall[indx - 1])
                        if not lall2:
                            print("No data will be processed for this month")
                            continue

                        else:

                            file_name = input("Type a new file name for OUTPUT file ==>")

                            convert_lall2_to_separate_lists(lall2)
                            print("TRACING: Here is the output saved to the file!")
                            lout = show_output(lall2)
                            write_list_of_output_lines_to_file(lout, file_name)
                            print("=========== STATS: ===========")
                            print("draw processed", len(lall2))
                            print()
                            jackpots = []
                            for st in lall2:
                                dig = int(st[8])
                                jackpots.append(dig)
                            dates = []
                            for st in lall2:
                                dates.append(st[0])
                            print("max jackpot", max(jackpots))
                            ind = jackpots.index(max(jackpots))
                            print("date max jackpot", dates[ind])
                            print()
                            averages = []
                            for innerlist1 in lall2:
                                jackpot = tail(2, innerlist1)[0]
                                winners = tail(2, innerlist1)[1]
                                average_paid = get_average_paid(jackpot, winners)
                                averages.append(average_paid)
                            print("max average won", max(averages))
                            ind1 = averages.index(max(averages))
                            print("date max average won", dates[ind1])
                            print()
                            print("number of times each number was drawn")
                            pen = countALLlist(lall2)
                            print(countALLlist(lall2))
                            print()
                            print(
                                "number of numbers in each range - all selected draws considered ranges: (0,10], (10,20], (20,30], (30,40], (40,50)")
                            print(oneNumRange(lall2))
                            print()
                            print("Six most frequently drawn numbers")
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            pen[pos] = -1
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            pen[pos] = -1
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            pen[pos] = -1
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            pen[pos] = -1
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            pen[pos] = -1
                            maxi = pen[0]
                            pos = 0
                            for k in range(len(pen)):
                                if (pen[k] > maxi):
                                    maxi = pen[k]
                                    pos = k
                            print("number", str(pos), "was drawn", str(maxi), "times")
                            break
                continue
        if selcrit == "END" or selcrit == 'end':
            print("You wish to end the program!")
            quit()
            break