Python Mixed Flashcards
(52 cards)
NumPy Array
- A multi-dimensional array provided by the Numerical Python library.
- Homogeneous: All elements are of the same type (lists are heterogenous).
- Efficient in terms of memory and speed.
- Supports a wide range of mathematical operations natively.
- Fixed in size once created.
””””
More than one dimension:
np.array([[1, 2], [3, 4]])
array([[1, 2],
[3, 4]])
“”””
Minimum dimensions 2:
np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])
“”””
Type provided:
np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])
zip function
- Combines two or more iterables into a sequence of tuples.
- Each tuple contains elements from the input iterables paired together.
- Stops when the shortest iterable is exhausted.
- Example: zip([1, 2, 3], [‘a’, ‘b’, ‘c’]) -> [(1, ‘a’), (2, ‘b’), (3, ‘c’)]
Iterables
- Objects that can be looped over or traversed.
- Returns an iterator to loop over its elements.
- Examples include lists, dictionaries, strings, and sets.
Tuples
- Ordered and immutable collections.
- Used to represent data where order is significant and modification is not permitted.
- Defined using parentheses, e.g., (1, ‘a’, 3.4) –> Can be unpacked: a, b, c = (1, ‘a’, 3.4)
Binary tree
Each node has at MOST 2 children
Binary search tree
a binary tree in which all the left children of a node <= n (node in question) < all right descendants
Static memory allocation
E.g., stacks or arrays: will manage memory for you and assume a fixed amount of memory upon instantiation with a cap on how much data may be added.
dynamic memory allocation
e.g., heaps or linked lists: allow you to allocate and reallocate memory within the life of the program.
Nodes
An individual node contains data and links to other nodes. Each data structure adds additional constraints or behavior to these features to create the desired structure.
Pointers
The link or links within the node are sometimes referred to as pointers. This is because they “point” to another node
What is the time complexity of a linear search algorithm?
O(N)
N/2 depicted as O(N) in Big O connotation
What is the time complexity for binary search?
O(log N)
How many search comparisons will a binary algorithm make in a sorted 1000-element list?
log(1000)
list of integers 1 to n, with one missing, find it
use n*(n+1)/2 formula to find sum of full list, then subtract sum of missing list
Display the first five rows of the DataFrame, including column titles
Df.head()
further commands:
Df.columns
Df.info()
Df.describe()
for a Dataframe df, create a rolling window for the “return” column
df[‘return’].rolling(window=…)
What does .apply(lambda x: (x + 1).prod() - 1) accomplish in the context of rolling windows?
- Applies a custom calculation to each rolling window.
- lambda x: Defines an anonymous function for the calculation.
- (x + 1): Adds 1 to each element (useful for compounding returns)
- .prod(): Computes the product of elements in the window, compounding the returns.
- 1: Adjusts the final compounded product back to a % format.
iterate over dict_x
for key, value in dict_x.items():
What are the three main operations for a stack?
- Push: adds data to the top of the stack
- Pop: removes and provides data from the top of the stack
- Peek: reveals data on the top of the stack
hash function will first compute…
a value using some scoring metric –> this is the hash value, hash code, or just the hash.
Our hash map implementation then takes that hash value …. the size of the array (missing word…)
mod
mod = modulo operation
a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.
defining feature of all hash functions: they greatly reduce any possible inputs into a much smaller range of potential outputs –> thus known as…
compression functions.
Is hashing a reversible process?
No