Some problems:
def distSum(lst,n):
sum==0
for current in lst:
sum=sum+abs(current-bestLoc)
return sum
sum
is a builtin function; do not use it as a variable name (it is confusing, and prevents you from calling the function).You meant
sum = 0
(set sum to 0), notsum==0
(is sum equal to 0?).What is
bestLoc
? You never define it. What isn
? You never use it. What is this function supposed to accomplish? It is not clear, and you haven't documented or commented it...... but that's irrelevant, because you never use the function either!
.
start_time = 0 #starts the time from 0 seconds
def median(lst):
start_time = time.time()
insertionSort(lst)
if len(lst)%2==1:
end_time = time.time()
You set a global variable,
start_time
, and never use it;You create a function-local variable by the same name (this is moderately confusing),
You only check how long it took to sort the list if it has an odd number of items.
.
return (lst[(len(lst)//2)-1]+lst[(len(lst)//2)//2])/2
... I simply have no words. How did you concoct this?
If you are looking for the median value, you probably want
lst_len = len(lst) halflen = lst_len // 2 if lst_len % 2: return lst[halflen] else: return (lst[halflen-1] + lst[halflen]) / 2
.
def main():
fileName = input('Enter a filename: ')
fileName = open(fileName, "r")
- You reuse the
fileName
variable; this is not illegal but is confusing, especially as for most of the program it is a file object, not a file name.
As for your original question: it looks like insertionSort
expects a list of items and sorts the list in-place. A simple implementation would be
insertion_sort = lambda lst: lst.sort()
but that's probably cheating ;-)