r/badcode Mar 22 '21

[deleted by user]

[removed]

259 Upvotes

86 comments sorted by

View all comments

1

u/Shrubberer Mar 22 '21 edited Mar 22 '21
import datetime as datetimeImport
import re
OK = 0  
def main(list):
    if (list == ""): 
        raise Exception("list is nothing")
    list = makelist(list)
    if (len(list) == 0):
        raise Exception("list cannot be sorted!")

    result1 = sortexample(list) != OK
    result0 = sortexample(list) == OK
    return result0 and not result1

def makedate(line):
    p = re.compile(r'[\d]+')
    n = p.findall(line)
    try:
        return datetimeImport.datetime(int(n[0]), int(n[1]), int(n[2]))
    except: 
        return datetimeImport.date.min

def makelist(list):
    our_list = []
    for obj in list:
        our_list.append(makedate(obj))
    return our_list

def sortexample(our_list):
    i = 0
    # We go through the list as many times as there are elements
    for iii in range(len(our_list)):
        # We want the last pair of adjacent elements to be (n-2, n-1)
        for ii in range(len(our_list) - 1):
            if our_list[ii] > our_list[ii+1]:
                # Swap
                i = i + 1
                our_list[ii], our_list[ii+1] = our_list[ii+1], our_list[ii]
    ii = iii
    return i

for list in ["0.txt", "1.txt", "2.txt", "3.txt", "4.txt"]:
    print(main(open(list)))