File tree 2 files changed +67
-0
lines changed
2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ Module 1.5 Big O SideBar
2
+
3
+ This module contains a few functions used to demonstrate the basic timed
4
+ performance of sort and add. It also has some small code to demonstrate O(n^2)
5
+ behavior
6
+
7
+
Original file line number Diff line number Diff line change
1
+ from time import time
2
+ import random
3
+
4
+ def performance_sum ():
5
+ """Evaluate performance of sum"""
6
+
7
+ scores = {}
8
+ trial = 1
9
+ while trial <= 20 :
10
+ numbers = [random .randint (1 ,9 ) for i in range (2 ** trial )]
11
+ now = time ()
12
+ sum = 0
13
+ for d in numbers :
14
+ sum = sum + d
15
+ done = time ()
16
+
17
+ scores [trial ] = (done - now )
18
+ trial += 1
19
+
20
+ for i in scores :
21
+ print 2 ** i , '\t ' , scores [i ]
22
+
23
+ def performance_sort ():
24
+ """Evaluate performance of sorting"""
25
+
26
+ scores = {}
27
+
28
+ trial = 1
29
+
30
+ while trial <= 16 :
31
+ numbers = [random .randint (1 ,2 ** trial ) for i in range (2 ** trial )]
32
+ now = time ()
33
+ numbers .sort ()
34
+ done = time ()
35
+
36
+ scores [trial ] = (done - now )
37
+ trial += 1
38
+
39
+ for i in scores :
40
+ print 2 ** i , '\t ' , scores [i ]
41
+
42
+ def hasDuplicates (X ):
43
+ """Determine whether X contains a duplicate value"""
44
+ for i in range (len (X )- 1 ):
45
+ for j in range (i + 1 ,len (X )):
46
+ if X [i ] == X [j ]:
47
+ return True
48
+ return False
49
+
50
+ def hasDuplicateOrNone (X ):
51
+ """Determine whether X contains None or a duplicate value"""
52
+ for i in range (len (X )):
53
+ if X [i ] == None :
54
+ return True
55
+ for i in range (len (X )- 1 ):
56
+ for j in range (i + 1 ,len (X )):
57
+ if X [i ] == X [j ]:
58
+ return True
59
+ return False
60
+
You can’t perform that action at this time.
0 commit comments