|
18 | 18 | > Many common behaviors of an OS such as copying a directory or deleting a directory, are implemented using recursive algorithms.
|
19 | 19 |
|
20 | 20 | > ### Python OS module
|
21 |
| - > * *os module* -> provides robust tools for interacting with the operating system during execution of a program. |
| 21 | + >* *os module* -> provides robust tools for interacting with the operating system during execution of a program. |
22 | 22 | >> os.path.getsize(path) - return imemediate disk usage (measure in bytes) for the file or directory that is identified by the string *path* (eg. /user/rt/courses).
|
23 | 23 |
|
24 | 24 | >> os.path.isdir(path) - return True if entry designated by string path is a directory; False otherwise.
|
|
99 | 99 |
|
100 | 100 | > *Nonmuttating* behaviors of the list class are precisely those that are supported by the tuple class, since tuples are immutable (non changeable)
|
101 | 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. |
| 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. |
| 103 | +
|
| 104 | +### Lexicographic Comparisons |
| 105 | + |
| 106 | +> Comparisons between 2 sequences are defined lexicographically. |
| 107 | +
|
| 108 | +> In worst case, evaluating such a condition requires an iteration taking time proportional to the length of the shorter of the 2 arrays, because when 1 sequences ends, the lexicographic result can be determined |
| 109 | +>> Example: [7,3,...] < [7,5,...] This returns true because the second element of the second array is bigger than the second elements of the first array; Therefore not needing to go through the rest of the list |
| 110 | +
|
| 111 | +### Removing elements from a list |
| 112 | + |
| 113 | +> list class offers several ways to remove an element from a list: |
| 114 | +>> pop() removes the last element from a list. This is most efficient because all other elements remain in their original location. This is efficiently an O(1) operation, but the bound is amortized because Python will occasionally shrink the underlying dynamic array to conserve memory. |
| 115 | +
|
| 116 | +>> pop(k) removes the element k that is at index k < n of a list (k index). Shifting all remaining elements after to the left to fill the gap that resulted from the pop of the element at position k. Efficiency of this operation is O(n-k) still constant. The most expensive call using pop() would be pop(0) since we have to shift every element in the list to the left to fill in the gaps from the removal of the 0 index. |
| 117 | +
|
| 118 | +>> remove() -> which removes the specified value that should be removed rather than pop() which removes the element at index k by using the index instead of the value. |
| 119 | +
|
| 120 | +### Extending a list |
| 121 | + |
| 122 | +> Python provides a method called extend() that is used tp add all elements of 1 list to the end of a 2nd list. |
| 123 | +
|
| 124 | +### Constructing New Lists |
| 125 | + |
| 126 | +> *list comprehension* example: squares=[k*k for k in range(1, n+1)] which is the same as: |
| 127 | +
|
| 128 | + squares=[] |
| 129 | + for k in range(1, n+1): |
| 130 | + squares.append(k*k) |
| 131 | + |
| 132 | +## Python's String class |
| 133 | + |
| 134 | +> The analysis for many behaviors is quite intuitive. Methods that produce a new string require time that is linear in the length of the string that is produced, since a string is an array of characters. |
| 135 | +
|
| 136 | +> Many of the behaviours that test boolean conditions of a string take O(n) time, examining all n characters in the worst case, but short circuiting as soon as the answer becomes evident. Such as checking if string is lower character and the first character is upper cased which gives automatically the reuslt False. |
0 commit comments