Search code examples
pythonradix-sort

radix sort of python between tow files


OK here! i just finished this program. it works well, but the result is not what i wanted. here is my whole code:

#----------class of queue----------
class queue:
    def __init__(self):
        self._queue = []

    def __str__(self):
        sstr = ""
        for i in self._queue:
            sstr += str(i)
            sstr += ", "
        return sstr

    def __len__(self):
        return len(self._queue)

    def after(self):
        return self._queue.pop(0)

    def add(self, i):
        self._queue.append(i)

#----------radix sort----------
def set_output():
    output_list = []
    for i in range (10):
        output_list.append(queue())
    return output_list

def set_radix(lists):
    output_list = set_output()
    for number in lists:
        a = number[2]
        output_list[int(a)].add(number)
    return output_list

def sec_radix(input_list, i):
    output_list = set_output()
    for queue in input_list:
        while len(queue) > 0:
            num = queue.after()
            a = num[i]
            output_list[int(a)].add(num)
    return output_list

def done_radix(num_list):
    return sec_radix(sec_radix(set_radix(num_list), 1), 0)

#----------read and write file----------        
def readfile(infile):
    print "Reading files, please wait..."
    f = open(infile, "r")
    lineList = f.readlines()
    f.close()
    outputList = []
    for line in lineList:
        line = line.strip()
        outputList.append(line.split(" "))
        print "Reading finished!"
    return outputList

def writefile(outfile, nums):
    print "Writing files, please wait..."
    output = new_output(nums)
    f = open(outfile, "w")
    f.write(output)
    print "Writing finished!"

def new_output(three_list):
    output = ""
    for queue_list in three_list:
        for numqueue in queue_list:
            while len(numqueue) > 0:
                output += numqueue.after()
                output += " "
                output += "\n"
    return output

#----------main function----------
def main():
    infile = readfile("radix.in")
    final_list = []
    for num_list in infile:
        print "Sorting..."
        final_list.append(done_radix(num_list))
    print "Sorting finished!"
    writefile("radix.out", final_list)

main()

here is what input and output should be three lines:

for radix.in:

line 1: 467 119 635 231 234 858 line 2: 786 463 715 745 729 574 856 806 339 106 487 line 3: 798 791 392 916 177 115 948 871 525

for radix.out:

line 1: 119 231 234 467 635 858 line 2: 106 339 463 487 574 715 729 745 786 806 856 line 3: 115 177 392 525 791 798 871 916 948

when you run this program you need have two files in same path(same folder). when you finished running, the result should be looks like above which is radix.out, which is really wired... it should be just create three line, but there are 26 lines, which is mean it start every new line with one number when sorting.

how can it just print out three lines like above.


Solution

  • Change this:

    def new_output(three_list):
        output = ""
        for queue_list in three_list:
            for numqueue in queue_list:
                while len(numqueue) > 0:
                    output += numqueue.after()
                    output += " "
                    output += "\n"
        return output
    

    To this (mind the indentation in last but one line!):

    def new_output(three_list):
        output = ""
        for queue_list in three_list:
            for numqueue in queue_list:
                while len(numqueue) > 0:
                    output += numqueue.after()
                    output += " "
            output += "\n"
        return output