File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
com/d3achannel/algorithms Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ .idea /*
Original file line number Diff line number Diff line change 1+ import time ;
2+ from functools import reduce ;
3+ import numpy ;
4+ X = [1 ,2 ,3 ,4 ,5 ];
5+
6+
7+ """
8+
9+ initial implementation with O(n^2)
10+
11+ """
12+ def calculateNewArray (x ):
13+ start = time .time ();
14+
15+ n = [1 for _ in range (len (X ))];
16+ for i in range (len (x )):
17+ for j in range (len (x )):
18+ if i != j :
19+ n [i ] *= x [j ];
20+ end = time .time ()
21+ print ("time for calculateNewArray in milliseconds " , (end - start )* 1000 );
22+ return n ;
23+
24+ print (calculateNewArray (X ));
25+
26+ """
27+
28+ Optimized with Divition and using numpy
29+ complexity is O(n)
30+
31+ """
32+ def calculateNewArray2 (x ):
33+
34+ start = time .time ();
35+
36+ n = [1 for _ in range (len (x ))];
37+ sumvalue = numpy .prod (x );
38+
39+ for i in range (len (x )):
40+ n [i ] = sumvalue / x [i ];
41+
42+ end = time .time ()
43+ print ("time for calculateNewArray2 in milliseconds " , (end - start ) * 1000 );
44+
45+ return n ;
46+
47+ print (calculateNewArray2 (X ));
48+
49+ """
50+
51+ Optimized with Divition and using reduce((lambda x, y: x * y), list1)
52+
53+ complexity is O(n)
54+
55+ """
56+
57+ def calculateNewArray3 (x ):
58+
59+ start = time .time ();
60+
61+ n = [1 for _ in range (len (x ))];
62+ sumvalue = reduce ((lambda x , y : x * y ), x )
63+
64+ for i in range (len (x )):
65+ n [i ] = sumvalue / x [i ];
66+
67+ end = time .time ()
68+ print ("time for calculateNewArray3 in milliseconds " , (end - start ) * 1000 );
69+
70+ return n ;
71+
72+ print (calculateNewArray3 (X ));
You can’t perform that action at this time.
0 commit comments