Question

Basically in here is the code that is supposed to return the median, and the time, but it shows an error on line 32, 30, and 6, "global name ' insertionSort ' is not defined, i know it is not defined but i don't know how do i implement it so that it works with this code?

import time
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()
        print("Time: " , end_time - start_time) #prints the time of the process
        return lst[len(lst)//2]
    else:
        return (lst[(len(lst)//2)-1]+lst[(len(lst)//2)//2])/2

def distSum(lst,n):
    sum==0
    for current in lst:
        sum=sum+abs(current-bestLoc)
    return sum

def main():
    fileName = input('Enter a filename: ')
    fileName = open(fileName, "r")
    lst = []
    for inputLine in fileName:
        splittext = inputLine.split()
        place = splittext[0]
        locations = splittext[1]
        lst += [locations]
    print(lst)
    print(median(lst))

main()

No correct solution

OTHER TIPS

Some problems:

def distSum(lst,n):
    sum==0
    for current in lst:
        sum=sum+abs(current-bestLoc)
    return sum
  1. sum is a builtin function; do not use it as a variable name (it is confusing, and prevents you from calling the function).

  2. You meant sum = 0 (set sum to 0), not sum==0 (is sum equal to 0?).

  3. What is bestLoc? You never define it. What is n? You never use it. What is this function supposed to accomplish? It is not clear, and you haven't documented or commented it...

  4. ... 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()
  1. You set a global variable, start_time, and never use it;

  2. You create a function-local variable by the same name (this is moderately confusing),

  3. 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
  1. ... I simply have no words. How did you concoct this?

  2. 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")
  1. 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 ;-)

Currently, insertion_sort means nothing to the program. It hasn't been defined.

If you know how insertion sort works, basically you start with an empty list and place numbers in it. You start at one end and compare the number you're inserting with the numbers already in the list until the number is between two numbers already in the list. Then you insert it in and repeat the process.

Hopefully this is enough to guide you on how to implement it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top