473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Search function not working.

Expand|Select|Wrap|Line Numbers
  1. import random
  2. import math
  3.  
  4. t = tuple()
  5.  
  6. def linsearch(key, l):
  7.     count = 0
  8.     index = 0
  9.     t = tuple()
  10.     for i in l:
  11.         count += 1
  12.         if key == i:
  13.             t = (index, count)
  14.             return t
  15.         index += 1
  16.  
  17.  
  18. def binsearch(key, l):
  19.     count = 0
  20.     low = 0
  21.     t = tuple()
  22.     high = len(l) - 1
  23.     while low < high:
  24.         count += 1
  25.         mid = (low + high) / 2  
  26.         if l[mid] < key:
  27.             low = mid + 1
  28.         elif low < len(l) and l[low] == key:
  29.             t = (low, count)
  30.         else:
  31.             high = mid
  32.         return t
  33.  
  34. def mean(l):
  35.     s = 0.0
  36.     for i in l:
  37.         s += i
  38.     return s // len(l)
  39.  
  40. def sdev(l):
  41.     s = 0.0
  42.     for i in l:
  43.         s += i**2
  44.     return math.sqrt((s // len(l)) - mean(l)**2)
  45.  
  46. def median(l):
  47.     if len(l) % 2 == 0:
  48.         return (l[len(l) // 2] + l[(len(l) // 2) - 1]) // 2.0
  49.     else:
  50.         return l[len(l) // 2]
  51.  
  52. listlen = int(raw_input("List length: "))
  53. testlen = int(raw_input("Test length: "))
  54. maxval = int(raw_input("Maximum value: "))
  55.  
  56. comlinsort = []
  57. combinsort = []
  58. comlinunsort = []
  59. combinunsort = []
  60. comlinreverse = []
  61. combinreverse = []
  62.  
  63. for i in range(listlen):
  64.     list = []
  65.     key = random.randint(1, maxval)
  66.     for j in range(listlen):
  67.         x = random.randint(1, maxval)
  68.         list.append(x)
  69.     t = linsearch(key, list)
  70.     comlinunsort.append(t[1])
  71.     list.sort()
  72.     t = linsearch(key, list)
  73.     comlinsort.append(t[1])
  74.     t = binsearch(key, list)
  75.     combinsort.append(t[1])
  76.     list.sort(reverse=True)
  77.     t = linsearch(key, list)
  78.     comlinreverse.append(t[1])
  79.     t = binsearch(key, list)
  80.     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:

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.    File "assign7.py", line 72, in <module>
  3.       comlinunsort.append(t[1])
  4. 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.
Nov 13 '09 #1
1 2003
bvdet
2,851 Expert Mod 2GB
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().
Nov 13 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
2
by: CharitiesOnline | last post by:
Hello, I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be depending on the number of results returned...
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
83
by: D. Dante Lorenso | last post by:
Trying to use the 'search' in the docs section of PostgreSQL.org is extremely SLOW. Considering this is a website for a database and databases are supposed to be good for indexing content, I'd...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
2
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to start, pause and resume a recurrsive search exactly where you left off, say in the registry programmatically? -- Michael Bragg,...
3
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework,...
6
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.