Search function not working.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesinik
    New Member
    • Nov 2009
    • 1

    Search function not working.

    Code:
    import random
    import math
    
    t = tuple()
    
    def linsearch(key, l):
        count = 0
        index = 0
        t = tuple()
        for i in l:
            count += 1
            if key == i:
                t = (index, count)
                return t
            index += 1
        
    
    def binsearch(key, l):
        count = 0
        low = 0
        t = tuple()
        high = len(l) - 1
        while low < high:
            count += 1
            mid = (low + high) / 2  
            if l[mid] < key:
                low = mid + 1
            elif low < len(l) and l[low] == key:
                t = (low, count)
            else:
                high = mid
            return t
    
    def mean(l):
        s = 0.0
        for i in l:
            s += i
        return s // len(l)
    
    def sdev(l):
        s = 0.0
        for i in l:
            s += i**2
        return math.sqrt((s // len(l)) - mean(l)**2)
    
    def median(l):
        if len(l) % 2 == 0:
            return (l[len(l) // 2] + l[(len(l) // 2) - 1]) // 2.0
        else:
            return l[len(l) // 2]
    
    listlen = int(raw_input("List length: "))
    testlen = int(raw_input("Test length: "))
    maxval = int(raw_input("Maximum value: "))
    
    comlinsort = []
    combinsort = []
    comlinunsort = []
    combinunsort = []
    comlinreverse = []
    combinreverse = []
    
    for i in range(listlen):
        list = []
        key = random.randint(1, maxval)
        for j in range(listlen):
            x = random.randint(1, maxval)
            list.append(x)
        t = linsearch(key, list)
        comlinunsort.append(t[1])
        list.sort()
        t = linsearch(key, list)
        comlinsort.append(t[1])
        t = binsearch(key, list)
        combinsort.append(t[1])
        list.sort(reverse=True)
        t = linsearch(key, list)
        comlinreverse.append(t[1])
        t = binsearch(key, list)
        combinreverse.append(t[1])
    ..is my code. I want to read the input for the list length, the number of items to search and the max value. Then sort a random list, reverse it, and do a linear search (store it in a tuple) and a binary search (also store in a tuple). Then I want statistics (the reason for mean, standard deviation, median definitions) for the comparison counts.

    I keep getting this error:

    Code:
    Traceback (most recent call last):
       File "assign7.py", line 72, in <module>
          comlinunsort.append(t[1])
    TypeError: 'NoneType' object is unsubscriptable
    My code is nearly complete, but I think the issue is with the placement of the returns. I've been messing around with it but I can't figure it out. If I move the returns out an indent, I get an error saying the tuple is out of range. Help would be greatly appreciated :D thanks.
    Last edited by thesinik; Nov 13 '09, 06:04 PM. Reason: fixed indents
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    binsearch() returns an empty tuple if the right condition is not met. linsearch() returns None if key is not in the list. This is causing your errors. Try debugging the functions and modify them to return what you need.

    I suggest that you avoid using built-in Python function names for identifiers. Assigning an object to list masks built-in function list().

    Comment

    Working...