Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1685a79

Browse files
committed
update on learning
1 parent 1127b17 commit 1685a79

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from time import time
2+
3+
def compute_average(n):
4+
"""Perform n appends to an empty list and return average time elapsed"""
5+
6+
data=[]
7+
start=time()
8+
9+
for k in range(n):
10+
data.append(None)
11+
12+
end= time()
13+
14+
return (end-start) /n
15+
16+
print(compute_average(1000000))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def countOccurrences(array, k):
2+
count=0
3+
4+
for i in range(len(array)):
5+
if k == array[i]:
6+
count= count+1
7+
8+
return count
9+
10+
data=[2,3,4,5,2,4,2,4,1,2,4,5,7,8,2,7,9,10]
11+
12+
k=2
13+
print(countOccurrences(data, k))

PythonDataStructsAndAlgo/Notes.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,21 @@
8282
8383
#### Implementing a dynamic array
8484
> The key is to provide means to grow the Array A that stores all the elements of a list. When an array is full and we append an element to the list we have to create a new array and copy the elements of previous array to the new array
85-
>> A common used rule is for the new array to have twice the capacity of the existing array.
85+
>> A common used rule is for the new array to have twice the capacity of the existing array.
86+
87+
### Amortize Analysis of dynamic arrays
88+
89+
> Amortization -> we can show that performing a sequence of such append operations on a dynamic array is actually efficient. To perform an *amortized analysis* we use an accounting technique where we view the computer as a coin operated appliance that requires the payment of 1 cyber-dollar for a constant amount of computing time.
90+
91+
> Using a *fixed* increment for each resize of the array, and thus an arithmatic progression of intermidiate array sizes, results in an overall time that is quadratic in the number of operations.
92+
93+
### Memory Usage and Shrinking the an array
94+
95+
> Another consequence of the rule of a geometric increase in capacity when appending to a dynamic array is that the final array size is guranteed to be proportional to the overall number of elements. That is, the data structure uses O(n) memory which is very desirable property for a data structure.
96+
97+
## Efficiency of Python's Sequence types
98+
### Python's List and Tuple class
99+
100+
> *Nonmuttating* behaviors of the list class are precisely those that are supported by the tuple class, since tuples are immutable (non changeable)
101+
102+
> Tuples are more memory efficient than lists because they are immutable; thus, there is no need for an underlying dynamic array with surplus capacity.

0 commit comments

Comments
 (0)