0

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.

2
  • 1
    are you trying to implement bubblesort? or insertion sort? or merge sort? you really need to understand your problem better i think ... pythons default sort is plenty fast if your goal is just a sorted list (although i dont think thats your goal, this seems a bit like a homework assignment) Commented Jun 27, 2018 at 18:56
  • @JoranBeasley From the description, it sounds like he's trying to implement a really inefficient selection sort. Commented Jun 28, 2018 at 3:01

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.