943,840 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 231
  • Python RSS
Jun 7th, 2009
0

data structures

Expand Post »
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

python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jen638 is offline Offline
1 posts
since Jun 2009
Jun 8th, 2009
0

Re: data structures

Please use code tags, even if it is quick.

What exactly is your question?
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: quick question
Next Thread in Python Forum Timeline: Web interface for server-client application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC