I write a program for radix sort some numbers which is in a file. here is my code:
rsort(a):
if a:
bins = [ [],[],[],[],[],[],[],[],[],[] ]
m = max(a)
r = 1
while m > r:
for e in a:
bins[(e/r)%10].append(e)
r = r * 10
a = []
for i in range(10):
a.extend(bins[i])
bins[i] = []
return a
def readfile(infile):
grid = []
f = open(infile, 'r')
lines = f.readlines()
f.close()
return grid
def writefile(outfile):
grid = []
f = open(outfile, 'w')
f.write()
f.close
return grid
def main():
infile = readfile("radix.in")
outfile = writefile("radix.out")
sortedvar = rsort(infile)
main()
So, i tried plug in rsort() and rsort(a) in the f.write(), it keeps telling me wrong. and if i leave it empty, it tells me function need takes exactly 1 argument. what i should do to print the radix sort results in file radix.out?
I didn't look at the rsort function at all as your question seems to be about I/O and function calling in general. Here's a possibility (assuming the numbers are all on the first line).
def readfile(infile):
nums = []
f = open(infile, "r")
line = f.readline()
f.close()
return [int(x) for x in line.split()]
def writefile(outfile, nums):
f = open(outfile, 'w')
f.write(' '.join([str(x) for x in nums]))
f.write('\n')
f.close()
def main():
nums = readfile("radix.in")
# nums = rsort(nums)
writefile("radix.out", nums)
main()