Python3 Flashcards
Give an example of type-hinting
def some_func(nums: List(int), val: int) -> int:
type hinting, afaik, is only for method params and returns, not variables
1-liner to iterate over a list (actually, any iterable like list, tuple, string), with both an index and value
for idx, val in enumerate(iterable):
Fun: for idx, val in enumerate(iterable, start = 2):
THIS WILL CHOOSE THE INDEX TO START AT, NOT THE ITEM TO START AT (e.g. if you choose 1, the result will be 1-indexed)
For debugging, use this trick to see everything in an iterator
print(list(iterator))
How would you use the python repl to see if a type is an iterator?
if we want to see if enumerate(nums) is an iterator, we need to see if its type has the ‘__iter__’ and ‘__next__’ method.
hasattr(enumerate(nums), ‘__iter__’) and hasattr(enumerate(nums), ‘__next__’) == True
How do you filter a list?
filtered_list = list(filter(lambda : somefunc(), your_list))
make sure to do list(filter()) lest you wind up with a “filter object” which seems to be a one time iterable (so if you iterate over it again, you’re gonna have a problem)
python3 get the max int
import sys
sys.maxsize
turn a string into a list
list(some_str)
type hinting for a list
from typing import List
def some_func(strs: List[str]) -> List[str]:
construct a list using a list comprehension
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
For example, assume we want to create a list of squares, like:
>>> >>> squares = [] >>> for x in range(10): ... squares.append(x**2) ... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Note that this creates (or overwrites) a variable named x that still exists after the loop completes. We can calculate the list of squares without any side effects using:
squares = list(map(lambda x: x**2, range(10)))
or, equivalently:
squares = [x**2 for x in range(10)]
which is more concise and readable.
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
> > > [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:
>>> >>> combs = [] >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] Note how the order of the for and if statements is the same in both these snippets.
If the expression is a tuple (e.g. the (x, y) in the previous example), it must be parenthesized.
The best way to create the 4 directions in a matrix
TODO
the best way to create the 8 directions (including diagonals) in a matrix
TODO
TIP: BE VERY CAREFUL ABOUT _____ when nesting loops
indentation!!!
if you can , pull out functionality into helper functions to prevent issues in indentation, especially if you reset variables at the END of a loop`
How is a list implemented under the hood? What does this mean about its common operations?
todo
How can you get classic C-like array behavior from python (including allocating an array and constant time lookup once it’s allocated)
todo
What are the built in sort operations in python? (both in-place and make-a-copy)
todo