| | |
data structures
![]() |
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
I am trying to write a program that generates a list and a random number on 3 seperate occations, then checks the list to see if the number is in the list, all the while, timing the procedure( Python code)
1st list is 1-100, 2nd list is 1-1000, third list is 1-10000, Print the results along with the time elapsed, thanks
1st list is 1-100, 2nd list is 1-1000, third list is 1-10000, Print the results along with the time elapsed, thanks
python Syntax (Toggle Plain Text)
import random import time def orderedSequentialSearch(alist, item): pos = 0 found = False stop = False while pos < len(alist) and not found and not stop: if alist[pos] == item: found = True else: if alist[pos] > item: stop = True else: pos = pos+1 return found def main(): start = time.clock() for count in range(1,100): print random.randint count = orderedSequentialSearch(alist) random.randint = orderedSequentialSearch(item) elapsed = (time.clock() - start) if found == False: print item, ("was not found in the list . Time : "),elapsed else: print item, ("was found in the the list. Time : "), elapsed start = time.clock() for count in range(1,1000): print random.randint count = orderedSequentialSearch(alist) random.randint = orderedSequentialSearch(item) elapsed = (time.clock() - start) if found == False: print item, ("was not found in the list . Time : "),elapsed else: print item, ("was found in the the list. Time : "), elapsed start = time.clock() for count in range(1,10000): print random.randint elapsed = (time.clock() - start) count = orderedSequentialSearch(alist) random.randint = orderedSequentialSearch(item) if found == False: print item, ("was not found in the list . Time : "),elapsed else: print item, ("was found in the the list. Time : "),elapsed main()
Last edited by Tekmaven; Jun 8th, 2009 at 4:33 pm. Reason: Code Tags
![]() |
Similar Threads
- data structures in c++ (C++)
- Data Structures??? (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the Python Forum
- Previous Thread: quick question
- Next Thread: Web interface for server-client application
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





