Reputation: 13
This is the code I have right now. I'm trying to apply a radix sort to a list of numbers read from a file and write the sorted list to a different file.
from math import log
b = open("radix.in.txt").readlines()
a = [int(i) for i in b]
f = open("radix.out.txt", "w")
def getIndex(num, base, digit_num):
return (num // base ** digit_num) % base
def newLists(size):
return [ [] for i in range(size) ]
def order(a, base, digit_num):
placehold = newLists(base)
for num in a:
placehold[getIndex(num, base, digit_num)].append(num)
return placehold
def radixSort(a, base):
passes = 3
new_list = a
for digit_num in range(passes):
new_list = order(new_list, base, digit_num)
list_c = [str(i) for i in new_list]
print list_c
radixSort(a, 10)
The input file is just a list of all the numbers to sort with one number on each line
The traceback:
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 23, in radixSort new_list = order(new_list, base, digit_num)
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 16, in order placehold[getIndex(num, base, digit_num)].append(num)
File "C:\Users\Nolan Caldwell\Desktop\HW5.py", line 8, in getIndex return (num // base ** digit_num) % base
TypeError: unsupported operand type(s) for //: 'list' and 'int'
Upvotes: 1
Views: 581
Reputation: 34312
Your function order()
expects a list of integers as a first argument a
, and that's what passed in the first iteration.
However, order()
returns a list of lists, and that new list is passed instead of a
in the second iteration in radixSort()
, that's why it fails.
Upvotes: 4