Nah, you can't change A
like that. Keep in mind that A
in your radixSort
function (local scope) is different with A
in your main function (global scope).
To achieve what you need, you can declare it as a global variable. However, this is not the best method since using global vars can get you to a confusing scope problem, therefore it's not recommended. But this how to do it:
def radixSort(lst): #we can refer to the passed variable as lst
global A
This doesn't sort A inplace, but it assigns the sorted lst
to A
.
Or better, assign the sorted value to A
with slice notation:
A[:] = sortByDigit(A, maxDigits) #store the sorted list in A
Or even better, return it as sorted, and then reassign A
with the sorted value:
def radixSort(A):
#get max amount of digits
A = sortByDigit(A, maxDigits) #this works
print(A) #prints A as sorted
return A
And in your main program:
if __name__ == "__main__":
A = [int(100*random.random()) for i in range(10)]
A = radixSort(A)
print(A) #prints sorted
self.assertEqual(A, [4,3,2]) #self.assertEqual(sorted, unsorted)
Also, it's not a good practice to capitalize identifiers. Capitalized words is usually reserved for classes.
So:
def radixSort(a):
#get max amount of digits
a = sortByDigit(a, maxDigits) #this works
print(a) #prints A as sorted
return a
if __name__ == "__main__":
a = [int(100*random.random()) for i in range(10)]
a = radixSort(a)
print(a) #prints sorted
self.assertEqual(A, [4,3,2]) #self.assertEqual(sorted, unsorted)
Hope this helps!