2
2
DSAN 5500: Data Structures, Objects, and Algorithms in Python
Monday, February 5, 2024
x
is an int
int
\(\implies\) thing you can do arithmetic addition withy
is a str
str
\(\implies\) thing you cannot do arithmetic addition with+
operator to represent concatenation when applied to two str
objects)Objects vs. Their Representations
\[ \begin{align*} \overbrace{\boxed{\texttt{int}\text{ object}}}^{\text{addition defined}} &\neq \overbrace{\boxed{\texttt{str}\text{ representation of }\texttt{int}\text{ object}}}^{\text{addition not defined}} \\ \underbrace{\boxed{\texttt{SwimStyle}\text{ object}}}_{\texttt{.name}\text{ defined}} &\neq \underbrace{\boxed{\texttt{str}\text{ representation of }\texttt{SwimStyle}\text{ object}}}_{\texttt{.name}\text{ not defined}} \end{align*} \]
str
s are just lists of characters…\(\leadsto\) 2. Characters are stored in Python memory as int
values (ASCII encodings)…
\(\leadsto\) 3. Each int
value is stored in computer memory as a byte (8 bits \(b_i \in \{0, 1\}\)):
['1000011', '1100101', '1100011', '1101001', '100000', '1101110', '100111', '1100101', '1110011', '1110100', '100000', '1110000', '1100001', '1110011', '100000', '1110101', '1101110', '1100101', '100000', '1110011', '1110100', '1110010', '1101001', '1101110', '1100111']
datetime.datetime
example)__str__()
and __repr__()
key
)[('Banana', 10), ('Apple', 2), ('Four Loko', 5)]
\[ T(n) = O(1 + \underbrace{\epsilon}_{\mathclap{\text{Collision rate}}} \cdot n) \]
\[ p^{✅} = [T(n) = O(1 + \epsilon n)], q^{✅} = [\epsilon \approx 0],\text{ so } p \wedge q \implies T(n) \overset{✅}{=} O(1). \]
hash(item)
= first letter of item
\[ h(\texttt{x}) = \texttt{x[0]} \]
h('Banana') = 'B'
, h('Monkey') = 'M'
item
hashes to'Banana'
), we compute the hash (B
), look in that slot, and obtain the price for bananas.h('Blueberries') = 'B'
B
slot and see that (Bananas, 10)
is already there!!! Wtf do we do here… don’t panic!LinkedList
that we can use whenever and however we want!LinkedList
? Why not just… another length-26 array, for example?B
(or, if we do, we want to be able to expand/contract our price list to handle new/discontinued items)HashTable
is an Array
that “degenerates into” a LinkedList
(when there are collisions)LinkedList
) was \(O(n)\) for everythihgLinkedList
BinarySearchTree
(BST
)LinkedList
, we’ll be able to take our HashMap
from today and “drop in” the BST
to play the role the LinkedList
is playing right nowBST
with its \(O(\log(n))\) operations, rather than a LinkedList
with its \(O(n)\) operationsHashMap
will go from [\(O(1)\) best-case / \(O(n)\) worst-case] to [\(O(1)\) best-case / \(O(\log_2(n))\) worst-case]! Stay tuned…DSAN 5500 Week 4: Heaps, Stacks, Hash Maps