scubasteve7
scubasteve7

Reputation: 67

Sorting algorithm help in python

I've been playing around with a program that will take in information from two files and then write the information out to a single file in sorted order.

So what i did was store each line of the file as an element in a list. I create another function that splits each element into a 2d array where i can easily access the name variables. From there i want to create a nested for loop that as it iterates it checks for the highest value in the array, removes the value from the list and appending it to a new list until there's a sorted list.

I think I am like 90% of the way there, but I am having trouble wrapping my head around the logic of sorting algorithms. It seems like the problem just keeps getting more complex and i keep wanting to use pointers. If someone could help shine some light on the subject I would greatly appreciate it.

import os
from http.cookiejar import DAYS
from macpath import split

# This program reads a given input file and finds its longest line.
class Employee: 
    def __init__(self, EmployeeID, name, wage, days):
        self.EmployeeID = EmployeeID
        self.name = name
        self.wage = wage
        self.days = days

def Extraction(file,file2):
    employList = [] 
    while True:
        line1 = file.readline().strip()
        line2 = file2.readline().strip()
        #print(type(line1))
        employList.append(line1)
        #print(line1)
        employList.append(line2)
        #print(line2)
        if line1 == '' or line2 == '':
            break
    return employList

def Sort(mylist):
    splitlist = []
    sortedlist = []
    print(len(mylist))
    for items in range(len(mylist)):

        #print(mylist[items].split())
        splitlist.append(mylist[items].split())
        print(splitlist)
    #print(splitlist[1][1])
    #print(splitlist[1][2])
    highest = "z"
    print(highest)
    sortingLength = len(splitlist)
    for i in range(10):
        for items in range(len(splitlist)-2):
            if highest > splitlist[items][2]:
                istrue = highest < splitlist[items][2]
                highest = splitlist[items][1]
                print(items)
                print(istrue)
                print('marker')
                print(splitlist[items][2])
            if items == (len(splitlist)-2):
                print("End of list",splitlist[items][2])

        print(highest)
        print(splitlist.index(highest))
    print(splitlist[len(splitlist)-1][2])
    print(sortingLength)

fPath = 'C:/Temp'

fileName = 'payroll1.txt'
fullFileName = os.path.join(fPath,fileName)
fileName2 = 'payroll2.txt'
fullFileName2 = os.path.join(fPath,fileName2)

f = open(fullFileName,'r')
f2 = open(fullFileName2, 'r')

employeeList = Extraction(f,f2)#pulling out each line in the file and placing into a list
Sort(employeeList)

ReportName= "List of Employees:"
marker = '-'* len(ReportName)
print (ReportName + ' \n' + marker)
total = 0

f.close()

I am having trouble with once having the higest value trying to append that value to a sortedlist, removing the value from the splitlist, and re running the code.

Upvotes: 0

Views: 97

Answers (1)

C.Nivs
C.Nivs

Reputation: 13106

Using the sorted method is much easier and already built-in, per Joran's suggestion. I've edited your reading method so that it builds two lists of tuples, representing the line and the length of the line. The sorted method will return a list sorted according to the key (line length) and descending order (reverse=True)

from operator import itemgetter

class Employee: 
    def __init__(self, EmployeeID, name, wage, days):
        self.EmployeeID = EmployeeID
        self.name = name
        self.wage = wage
        self.days = days

def Extraction(file,file2):
    employList = [] 
    mylines = [(i, len(l.strip()), 'file1') for i,l in enumerate(file.readlines())]
    mylines2 = [(i, len(l.strip()), 'file2') for i,l in enumerate(file2.readlines())]

    employList = [*mylines, *mylines2]

    return employList

fPath = 'C:/Temp'

fileName = 'payroll1.txt'
fullFileName = os.path.join(fPath,fileName)
fileName2 = 'payroll2.txt'
fullFileName2 = os.path.join(fPath,fileName2)

f = open(fullFileName,'r')
f2 = open(fullFileName2, 'r')

employeeList = Extraction(f,f2)#pulling out each line in the file and placing the line_number and length into a list

f.close()
f2.close()

# Itemgetter will sort on the second element of the tuple, len(line)
# and reverse will put it in descending order
ReportName = sorted(employeeList, key=itemgetter(1), reverse=True)

EDIT: I've added markers in the tuples so that you can keep track of what lines came from what file. Might be a bit confusing without them

Upvotes: 1

Related Questions