* Python :PROPERTIES: :ANKI_DECK: soft-eng::python :END: ** In Python, how can you push off the right-most digit? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1763087705407 :ANKI_NOTE_HASH: ba6f561ebbdbd811d9e639ea42506286 :END: =n//=10= ** What is the /nonlocal/ keyword in Python? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1763087705410 :ANKI_NOTE_HASH: 3d4534755f70eb9d40aa9781ca19399e :END: refers to the variable (non-global) outside the functions' current scope. ** what is the difference between =.sort()= and =sorted()= in Python? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1763087705413 :ANKI_NOTE_HASH: c160a694069ddeca7b9a111eac29a0bb :END: =.sort()= changes the variable in-place. =sorted()= takes in an iterable and gives a new object. ** What is the difference between list and List in Python? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1763087705417 :ANKI_NOTE_HASH: 05612bbc546a51d3005cfd507d3ef517 :END: =list= is the built-in runtime type (the actual list class). =typing.List= (capital L) is a typing alias used for type annotations (pre-Python 3.9 you wrote =List[int]=, etc.). Since Python 3.9 (PEP 585), built-in generics exist, so you can usually write: =list[int]= instead of =List[int]=. Note: =typing.List= still works, but it’s mainly kept for backward compatibility. ** What error will this code throw and why? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1764554572460 :ANKI_NOTE_HASH: 0614461b5a079e0682f0fbad88822147 :END: #+begin_src python :exports code from collections import defaultdict def myfunc(nums, target): hashmap = defaultdict() for item in nums: if target - item in hashmap: return (item, target - item) hashmap[item] += 1 myfunc([2,7,11,15], 9) #+end_src *** Back #+begin_example --------------------------------------------------------------------------- KeyError Traceback (most recent call last) KeyError: 2 #+end_example because we need to give =defaultdict()= a type! ** What are some useful options that the =sorted()= function takes in Python? Does =.sort()= take these too? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1764704883831 :ANKI_NOTE_HASH: 24321628d307158bf153e37143fbb66f :END: Useful options to =sorted(iterable, ...)=: - =key=None= (a function used to extract the sort key) - =reverse=False= (sort descending if True) Yes: =list.sort(key=None, reverse=False)= takes the same two options. Difference: =sorted(...)= returns a new list; =.sort()= sorts the list in-place (and returns None). ** How can you access all the items in a dictionary (called =mydict= )? What about the keys and values? What are their natural types? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty :ANKI_NOTE_ID: 1764704883839 :ANKI_NOTE_HASH: 2f24f41daccd4e148adfb5c51a4d345e :END: =mydict.items()= list of tuples =mydict.keys()= list of keys =mydict.values()= list of values ** In Python, where do you import =defaultdict= from? What does this function do? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1764704883840 :ANKI_NOTE_HASH: 9e4b2d0a7f66231edb14869db153b8a2 :END: =from collections import defaultdict= takes a type as an argument and makes that the default value. prevents =KeyError=​s on lookups. ** How is a set implemented in Python? What does this mean we can expect from the runtime? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1764704883842 :ANKI_NOTE_HASH: d84e957312cc74e5c21b3ce58c7479b6 :END: implemented as a hash-table. implies $\mathcal{O}(1)$ insert, lookup and deletion. ** Give an example of an exponential time algorithm. Give one for quadratic and linear too. :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1764704883843 :ANKI_NOTE_HASH: affb2a17ef319203cccc1f571429a98a :END: - finding all subsets of a set (exponential) - quadratic: bubble-sort / insertion sort - linear: finding an item in an unsorted array ** In Python, a set removes an element by its {{c1::value}} not {{c1::index}} :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765545444999 :ANKI_NOTE_HASH: 16e564e404da8ff50adff8f4314e9230 :END: ** Where do we import =deque= from? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1764730520822 :ANKI_NOTE_HASH: 87b98c618d9b20e34bad30dce1993b30 :END: What is another way to use the =deque= without importing it explicitly first? *** Back =from collections import deque= or, =q = collections.deque()= (inline)