data structures

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 1
Reputation: jen638 is an unknown quantity at this point 
Solved Threads: 0
jen638 jen638 is offline Offline
Newbie Poster

data structures

 
0
  #1
Jun 7th, 2009
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

  1. import random
  2.  
  3. import time
  4.  
  5. def orderedSequentialSearch(alist, item):
  6. pos = 0
  7. found = False
  8. stop = False
  9. while pos < len(alist) and not found and not stop:
  10. if alist[pos] == item:
  11. found = True
  12. else:
  13. if alist[pos] > item:
  14. stop = True
  15. else:
  16. pos = pos+1
  17.  
  18. return found
  19.  
  20. def main():
  21. start = time.clock()
  22. for count in range(1,100):
  23.  
  24. print random.randint
  25.  
  26. count = orderedSequentialSearch(alist)
  27. random.randint = orderedSequentialSearch(item)
  28. elapsed = (time.clock() - start)
  29. if found == False:
  30. print item, ("was not found in the list . Time : "),elapsed
  31. else:
  32. print item, ("was found in the the list. Time : "), elapsed
  33.  
  34. start = time.clock()
  35.  
  36. for count in range(1,1000):
  37. print random.randint
  38.  
  39. count = orderedSequentialSearch(alist)
  40. random.randint = orderedSequentialSearch(item)
  41. elapsed = (time.clock() - start)
  42. if found == False:
  43. print item, ("was not found in the list . Time : "),elapsed
  44. else:
  45. print item, ("was found in the the list. Time : "), elapsed
  46.  
  47. start = time.clock()
  48. for count in range(1,10000):
  49.  
  50. print random.randint
  51. elapsed = (time.clock() - start)
  52. count = orderedSequentialSearch(alist)
  53. random.randint = orderedSequentialSearch(item)
  54.  
  55. if found == False:
  56. print item, ("was not found in the list . Time : "),elapsed
  57. else:
  58. print item, ("was found in the the list. Time : "),elapsed
  59.  
  60. main()
Last edited by Tekmaven; Jun 8th, 2009 at 4:33 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 128
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: data structures

 
0
  #2
Jun 8th, 2009
Please use code tags, even if it is quick.

What exactly is your question?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 164 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC