Question

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.

Was it helpful?

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

OTHER TIPS

Perhaps write to outfile with something like:

with open('radix.out', 'w') as outfile:
    for sublist in final_list:
        list1 = (str(element).strip() for element in sublist)
        list2 = (element for element in list1 if element)
        list3 = (element.rstrip(',') for element in list2)
        string = ', '.join(list3)
        outfile.write('{}\n'.format(string))

BTW, you're probably better off using list_.sort() than writing your own radix sort. This is for two reasons:

  1. Writing a sort in Python tends to be slower than sorting in C, and Python's list_.sort() is in C. Also, Python's list_.sort() is a very good algorithm: Timsort.
  2. Radix sort is sometimes said to be O(n), but really it's O(nlogn). Why? Because it varies with the number of digits in your numbers, and the number of digits is logn.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top