hashtables & updates
This commit is contained in:
parent
ce4e78cabf
commit
3a8136e22c
@ -35,7 +35,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -54,17 +54,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<__main__.StaticArray object at 0x1079a50f0>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa = StaticArray([1, 2, 3])\n",
|
||||
"print(sa)"
|
||||
@ -72,19 +64,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<__main__.StaticArray object at 0x107b550f0>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa = StaticArray(0, 5)\n",
|
||||
"print(sa)"
|
||||
@ -92,17 +76,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 64,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"StaticArray([1, 2, 3])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# we can fix that by defining a __repr__ method\n",
|
||||
"\n",
|
||||
@ -126,19 +102,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 65,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"StaticArray([0, 0, 0, 0, 0])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa = StaticArray(0, 5)\n",
|
||||
"print(sa)"
|
||||
@ -173,7 +141,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 67,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -214,20 +182,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 68,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4"
|
||||
]
|
||||
},
|
||||
"execution_count": 68,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa = StaticArray([1, \"hi\", 3.14, True])\n",
|
||||
"len(sa)"
|
||||
@ -235,20 +192,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 70,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 70,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"42 in sa\n",
|
||||
"\"hi\" in sa"
|
||||
@ -256,42 +202,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 72,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 72,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa[3]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 73,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "IndexError",
|
||||
"evalue": "Index out of range",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[73], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43msa\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
|
||||
"Cell \u001b[0;32mIn[67], line 29\u001b[0m, in \u001b[0;36mStaticArray.__setitem__\u001b[0;34m(self, index, val)\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__setitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, index, val):\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity \u001b[38;5;129;01mor\u001b[39;00m index \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcapacity:\n\u001b[0;32m---> 29\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mIndexError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndex out of range\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mitems[index] \u001b[38;5;241m=\u001b[39m val\n",
|
||||
"\u001b[0;31mIndexError\u001b[0m: Index out of range"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sa[42] = \"hello\""
|
||||
]
|
||||
@ -351,20 +273,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"iterator 1 next() 1\n",
|
||||
"iterator 1 next() 2\n",
|
||||
"iterator 2 next() 1\n",
|
||||
"iterator 1 next() 3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ll = [1, 2, 3, 4]\n",
|
||||
"iterator = iter(ll)\n",
|
||||
@ -409,7 +320,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -419,13 +330,15 @@
|
||||
" self.n = n\n",
|
||||
"\n",
|
||||
" def __iter__(self):\n",
|
||||
" print(\"iter has been called\")\n",
|
||||
" return self\n",
|
||||
"\n",
|
||||
" def __next__(self):\n",
|
||||
" print(f\"next was called, moving {self.current} to {self.current+1}\")\n",
|
||||
" if self.current >= self.n:\n",
|
||||
" print(\"at the end\")\n",
|
||||
" raise StopIteration\n",
|
||||
" else:\n",
|
||||
" print(f\"next was called, moving {self.current} to {self.current+1}\")\n",
|
||||
" self.current += 1\n",
|
||||
" return self.current - 1\n",
|
||||
"\n",
|
||||
@ -435,27 +348,27 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0 0\n",
|
||||
"iter has been called\n",
|
||||
"next was called, moving 0 to 1\n",
|
||||
"iter has been called\n",
|
||||
"next was called, moving 1 to 2\n",
|
||||
"0 1\n",
|
||||
"next was called, moving 2 to 3\n",
|
||||
"0 2\n",
|
||||
"1 0\n",
|
||||
"1 1\n",
|
||||
"1 2\n",
|
||||
"2 0\n",
|
||||
"2 1\n",
|
||||
"2 2\n"
|
||||
"at the end\n",
|
||||
"at the end\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sr = range(3)\n",
|
||||
"sr = SimpleRange(3)\n",
|
||||
"for i in sr:\n",
|
||||
" for j in sr:\n",
|
||||
" print(i, j)"
|
||||
@ -463,19 +376,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SimpleRange(5, current=0)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sr = SimpleRange(5)\n",
|
||||
"siter = iter(sr)\n",
|
||||
@ -484,41 +389,22 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"siter is sr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SimpleRange(5, current=1)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"next(siter)\n",
|
||||
"print(siter)"
|
||||
@ -539,12 +425,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Adding __iter__ to StaticArray\n",
|
||||
"eclass StaticArrayIterator:\n",
|
||||
"class StaticArrayIterator:\n",
|
||||
" def __init__(self, values):\n",
|
||||
" self.values = values\n",
|
||||
" self.position = 0\n",
|
||||
@ -610,6 +496,57 @@
|
||||
" return StaticArrayIterator(self._items.copy())\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1, 2, 3, 4, 5]\n",
|
||||
"1 1\n",
|
||||
"1 2\n",
|
||||
"1 3\n",
|
||||
"1 4\n",
|
||||
"1 5\n",
|
||||
"2 1\n",
|
||||
"2 2\n",
|
||||
"2 3\n",
|
||||
"2 4\n",
|
||||
"2 5\n",
|
||||
"3 1\n",
|
||||
"3 2\n",
|
||||
"3 3\n",
|
||||
"3 4\n",
|
||||
"3 5\n",
|
||||
"4 1\n",
|
||||
"4 2\n",
|
||||
"4 3\n",
|
||||
"4 4\n",
|
||||
"4 5\n",
|
||||
"5 1\n",
|
||||
"5 2\n",
|
||||
"5 3\n",
|
||||
"5 4\n",
|
||||
"5 5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sa = StaticArray(5, 2)\n",
|
||||
"sa[0] = 1\n",
|
||||
"sa[1] = 2\n",
|
||||
"sa[2] = 3\n",
|
||||
"sa[3] = 4\n",
|
||||
"sa[4] = 5\n",
|
||||
"print(sa)\n",
|
||||
"for x in sa:\n",
|
||||
" for y in sa:\n",
|
||||
" print(x, y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@ -633,7 +570,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -664,28 +601,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ran query SELECT * FROM users;\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "ZeroDivisionError",
|
||||
"evalue": "division by zero",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[19], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m db\u001b[38;5;241m.\u001b[39mquery(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSELECT * FROM users;\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# do something dangerous\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n",
|
||||
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"db = DatabaseConnection(\"hello\", \"world\")\n",
|
||||
"db.query(\"SELECT * FROM users;\")\n",
|
||||
@ -695,17 +613,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Connection connected=True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# our connection is possibly left in a broken state\n",
|
||||
"print(db)"
|
||||
@ -713,31 +623,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"__enter__\n",
|
||||
"ran query SELECT * from users;\n",
|
||||
"__exit__\n",
|
||||
"rolling back changes\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "ZeroDivisionError",
|
||||
"evalue": "division by zero",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[21], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m DatabaseConnection(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mworld\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m db:\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# __enter__\u001b[39;00m\n\u001b[1;32m 3\u001b[0m db\u001b[38;5;241m.\u001b[39mquery(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSELECT * from users;\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# __exit__\u001b[39;00m\n",
|
||||
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with DatabaseConnection(\"hello\", \"world\") as db:\n",
|
||||
" # __enter__\n",
|
||||
@ -748,20 +636,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"False"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# changes were rolled back, and our connection is safe\n",
|
||||
"db.connected\n"
|
||||
@ -778,7 +655,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -804,29 +681,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'add'"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"x.__name__"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 48,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
@ -835,15 +701,19 @@
|
||||
"class Example:\n",
|
||||
" def __init__(self, name):\n",
|
||||
" self.name = name\n",
|
||||
" def __call__(self, *args):\n",
|
||||
" self.num_calls = 0\n",
|
||||
" def __call__(self, **kwargs):\n",
|
||||
" print(self.num_calls)\n",
|
||||
" self.num_calls += 1\n",
|
||||
" print(self.name, \"got\", args)\n",
|
||||
"\n",
|
||||
"example = Example(\"one\")"
|
||||
"example = Example(\"one\")\n",
|
||||
"two = Example(\"two\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 47,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
@ -852,12 +722,31 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"one got ()\n"
|
||||
"7\n",
|
||||
"one got (1, 2, 3)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"example()"
|
||||
"example(1, 2, 3)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2\n",
|
||||
"two got ()\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"two()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -869,7 +758,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 54,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -886,7 +775,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 55,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -907,7 +796,6 @@
|
||||
"\n",
|
||||
"#expensive_func = Memoized(expensive_func)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"print(expensive_func(1, 2, 3))\n",
|
||||
"print(expensive_func(1, 2, 3))\n"
|
||||
]
|
||||
@ -923,7 +811,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -951,19 +839,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"15\n",
|
||||
"add(args=(5,) kwargs={})\n",
|
||||
"Adds two numbers\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def add(x, y):\n",
|
||||
" \"\"\"Adds two numbers\"\"\"\n",
|
||||
|
@ -24,7 +24,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 26,
|
||||
"id": "aea2b16e-3012-4ce5-a806-7a2f0f712c08",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -37,12 +37,14 @@
|
||||
" # class attribute\n",
|
||||
" wheels = 4\n",
|
||||
" registrations = []\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def __init__(self, make, model, year):\n",
|
||||
" self.make = make \n",
|
||||
" self.model = model \n",
|
||||
" self.year = year\n",
|
||||
" self.registrations = []\n",
|
||||
" #self.wheels = 0\n",
|
||||
" Car.registrations.append(self)\n",
|
||||
" #self.registrations = []\n",
|
||||
" \n",
|
||||
" def compute_age(self):\n",
|
||||
" return datetime.date.today().year - self.year \n",
|
||||
@ -54,7 +56,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 27,
|
||||
"id": "a3aee627-9404-4c08-9571-6c1206ed9790",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -80,7 +82,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 28,
|
||||
"id": "61f681e4-0105-4b20-8d72-dfd5a347df8a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -90,7 +92,7 @@
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -128,7 +130,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 31,
|
||||
"id": "9997fc8a-6562-4e0a-8be9-9dc34446f5bd",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -139,8 +141,8 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"False\n",
|
||||
"3\n",
|
||||
"3\n"
|
||||
"4\n",
|
||||
"4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -172,7 +174,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 32,
|
||||
"id": "95bcb4f6-d11e-4074-90d3-63dc6a0f19eb",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -194,6 +196,7 @@
|
||||
" self.year = year\n",
|
||||
" \n",
|
||||
" def compute_age(self):\n",
|
||||
" print(self)\n",
|
||||
" current_year = int(date.today().year)\n",
|
||||
" return current_year - self.year \n",
|
||||
" \n",
|
||||
@ -208,7 +211,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 35,
|
||||
"id": "0805ad8c-4b7a-48e2-9ff9-be57a03067dc",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -220,14 +223,15 @@
|
||||
"text": [
|
||||
"<class '__main__.Car'>\n",
|
||||
"Car has 4 wheels with a tire pressure of 35\n",
|
||||
"<class '__main__.Car'>\n",
|
||||
"Car has 4 wheels with a tire pressure of 35\n"
|
||||
"<__main__.Car object at 0x1034b7e50>\n",
|
||||
"5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(Car.tire_description())\n",
|
||||
"print(car1.tire_description())"
|
||||
"#print(car1.tire_description())\n",
|
||||
"print(car1.compute_age())"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -308,21 +312,25 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 40,
|
||||
"id": "a1c858ea-8222-4776-a3b7-9f7443bd918f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"car1str = \"Pontiac|Grand Am|1997\"\n",
|
||||
"car2str = \"Ford|Mustang|1970\"\n",
|
||||
"car3str = \"Hyundai|Sonata|2007\""
|
||||
"car1str = \"Pontiac|Grand Am|1997|4892\"\n",
|
||||
"car2str = \"Ford|Mustang|1970|800\"\n",
|
||||
"car3str = \"Hyundai|Sonata|2007|0\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def make_car_from_string(s: str) -> Car:\n",
|
||||
" ..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 41,
|
||||
"id": "5c10ac8b-deb5-4a23-9beb-d0d32306552d",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -339,12 +347,15 @@
|
||||
" self.make = make \n",
|
||||
" self.model = model \n",
|
||||
" self.year = year\n",
|
||||
" self.mileage = 0\n",
|
||||
" \n",
|
||||
" @classmethod\n",
|
||||
" def from_string(cls, string):\n",
|
||||
" make, model, year = string.split(\"|\")\n",
|
||||
" make, model, year, mileage = string.split(\"|\")\n",
|
||||
" # invoke Car's constructor\n",
|
||||
" return cls(make, model, year)\n",
|
||||
" new_instance = cls(make, model, year)\n",
|
||||
" new_instance.mileage = mileage\n",
|
||||
" return new_instance\n",
|
||||
" \n",
|
||||
" def compute_age(self):\n",
|
||||
" current_year = int(date.today().year)\n",
|
||||
@ -362,7 +373,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 42,
|
||||
"id": "39b783df-a6a6-4a6e-b33e-a20ec18f4671",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -376,7 +387,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 43,
|
||||
"id": "b5cde4b1-168f-48bd-ac95-b0ca87c66c5a",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -411,6 +422,101 @@
|
||||
" - ``itertools.chain.from_iterable()``\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"id": "d673a408-4e12-49ae-b402-5179820e35fa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "map() must have at least two arguments.",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[45], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28;43mmap\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 2\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mdict\u001b[39m(\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m)\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: map() must have at least two arguments."
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = list(map(...))\n",
|
||||
"y = dict(...)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "3a5ed2dd-732f-4fb2-b776-475c60f2f0ec",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"datetime.date(2024, 11, 11)"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import datetime\n",
|
||||
"datetime.date(2024, 11, 11)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "dccd79cc-4f80-4609-bc11-d136beb1b9b9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"datetime.date(2009, 2, 13)"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"datetime.date.fromtimestamp(1234567890)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 46,
|
||||
"id": "6d9f0442-429c-4ed4-8dc2-3bd6f5064f09",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"2\n",
|
||||
"3\n",
|
||||
"4\n",
|
||||
"5\n",
|
||||
"6\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import itertools\n",
|
||||
"for x in itertools.chain.from_iterable([(1,2,3), (4,5,6)]):\n",
|
||||
" print(x)\n",
|
||||
"#for x in (1,2,3):\n",
|
||||
"# print(x)\n",
|
||||
"#for x in (4,5,6):\n",
|
||||
"# print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c4d1ad9f-b280-4bcf-af74-bf8dc931dbab",
|
||||
@ -645,7 +751,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 2,
|
||||
"id": "b5d16735-9280-4002-a4ba-bc267fd52ff2",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -657,8 +763,11 @@
|
||||
" self.__name = name # Assume it has getter/setters \n",
|
||||
" self.set_age(age)\n",
|
||||
"\n",
|
||||
" def _calculate_age_from_birthday():\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def get_age(self):\n",
|
||||
" return self.__age\n",
|
||||
" return self._calculate_age_from_birthday()\n",
|
||||
"\n",
|
||||
" def set_age(self, age):\n",
|
||||
" if age < 0:\n",
|
||||
@ -673,19 +782,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 3,
|
||||
"id": "1923f424-f9a4-4bb1-bd30-5847ecbc3064",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p = Person(\"Joe Biden\", 79)"
|
||||
"p = Person(\"C. Montgomery Burns\", 100)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 4,
|
||||
"id": "74f206d8-3ecd-464d-a3ca-8a17f390f6ac",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -694,10 +803,10 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"79"
|
||||
"100"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -708,19 +817,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 6,
|
||||
"id": "75b8d93c-c2f7-4712-9934-8ccf0ad22439",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"p.set_age(80)"
|
||||
"p.set_age(101)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 7,
|
||||
"id": "c89dd611-adec-405f-9ac8-0fb65a68e35d",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -729,10 +838,10 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"80"
|
||||
"101"
|
||||
]
|
||||
},
|
||||
"execution_count": 28,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -743,7 +852,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"execution_count": 8,
|
||||
"id": "af8b33ec-388a-4d9a-98c0-d5a824eede66",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -756,8 +865,8 @@
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_age\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
||||
"Cell \u001b[0;32mIn[24], line 11\u001b[0m, in \u001b[0;36mPerson.set_age\u001b[0;34m(self, age)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset_age\u001b[39m(\u001b[38;5;28mself\u001b[39m, age):\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPerson can\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt have a negative age!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__age \u001b[38;5;241m=\u001b[39m age\n",
|
||||
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_age\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
||||
"Cell \u001b[0;32mIn[2], line 11\u001b[0m, in \u001b[0;36mPerson.set_age\u001b[0;34m(self, age)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset_age\u001b[39m(\u001b[38;5;28mself\u001b[39m, age):\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPerson can\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt have a negative age!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__age \u001b[38;5;241m=\u001b[39m age\n",
|
||||
"\u001b[0;31mValueError\u001b[0m: Person can't have a negative age!"
|
||||
]
|
||||
}
|
||||
@ -768,17 +877,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"execution_count": 9,
|
||||
"id": "9705d447-8568-476e-97ab-541b61f05002",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"80"
|
||||
"101"
|
||||
]
|
||||
},
|
||||
"execution_count": 30,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -818,7 +927,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"execution_count": 16,
|
||||
"id": "b92490b9-867a-461b-b922-a54235ca9463",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -826,15 +935,16 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Person:\n",
|
||||
" \n",
|
||||
" def __init__(self, name, age):\n",
|
||||
" self.name = name # Assume it has getter/setters \n",
|
||||
" self.age = age\n",
|
||||
"\n",
|
||||
" def get_age(self):\n",
|
||||
" def _get_age(self):\n",
|
||||
" print(\"inside get age\")\n",
|
||||
" return self.__age\n",
|
||||
"\n",
|
||||
" def set_age(self, age):\n",
|
||||
" def _set_age(self, age):\n",
|
||||
" if age < 0:\n",
|
||||
" raise ValueError(\"Person can't have a negative age!\")\n",
|
||||
" self.__age = age\n",
|
||||
@ -842,66 +952,51 @@
|
||||
" def __repr__(self):\n",
|
||||
" return f\"Person({self.__name!r}, {self.__age})\"\n",
|
||||
" \n",
|
||||
" age = property(get_age, set_age, doc=\"age of the person\")"
|
||||
" age = property(_get_age, _set_age, doc=\"age of the person\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"execution_count": 17,
|
||||
"id": "c32cd2dd-cb18-4858-b662-19790b74571e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"inside get age\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['_Person__age',\n",
|
||||
" '__class__',\n",
|
||||
" '__delattr__',\n",
|
||||
" '__dict__',\n",
|
||||
" '__dir__',\n",
|
||||
" '__doc__',\n",
|
||||
" '__eq__',\n",
|
||||
" '__format__',\n",
|
||||
" '__ge__',\n",
|
||||
" '__getattribute__',\n",
|
||||
" '__gt__',\n",
|
||||
" '__hash__',\n",
|
||||
" '__init__',\n",
|
||||
" '__init_subclass__',\n",
|
||||
" '__le__',\n",
|
||||
" '__lt__',\n",
|
||||
" '__module__',\n",
|
||||
" '__ne__',\n",
|
||||
" '__new__',\n",
|
||||
" '__reduce__',\n",
|
||||
" '__reduce_ex__',\n",
|
||||
" '__repr__',\n",
|
||||
" '__setattr__',\n",
|
||||
" '__sizeof__',\n",
|
||||
" '__str__',\n",
|
||||
" '__subclasshook__',\n",
|
||||
" '__weakref__',\n",
|
||||
" 'age',\n",
|
||||
" 'get_age',\n",
|
||||
" 'name',\n",
|
||||
" 'set_age']"
|
||||
"1000"
|
||||
]
|
||||
},
|
||||
"execution_count": 32,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"p = Person(\"Wayne\", 30)\n",
|
||||
"dir(p)"
|
||||
"p.age"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"execution_count": null,
|
||||
"id": "6c52d816-af8d-46bf-8e23-cc1417743868",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "30497dd2-ebf6-44f8-a4f9-d37b4384b06a",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -914,8 +1009,8 @@
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mage\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m\n",
|
||||
"Cell \u001b[0;32mIn[31], line 12\u001b[0m, in \u001b[0;36mPerson.set_age\u001b[0;34m(self, age)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset_age\u001b[39m(\u001b[38;5;28mself\u001b[39m, age):\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPerson can\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt have a negative age!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__age \u001b[38;5;241m=\u001b[39m age\n",
|
||||
"Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mage\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m\n",
|
||||
"Cell \u001b[0;32mIn[10], line 12\u001b[0m, in \u001b[0;36mPerson.set_age\u001b[0;34m(self, age)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset_age\u001b[39m(\u001b[38;5;28mself\u001b[39m, age):\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPerson can\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt have a negative age!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__age \u001b[38;5;241m=\u001b[39m age\n",
|
||||
"\u001b[0;31mValueError\u001b[0m: Person can't have a negative age!"
|
||||
]
|
||||
}
|
||||
@ -926,7 +1021,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"execution_count": 13,
|
||||
"id": "06a9c879-8c84-4b57-b585-b162e6c489fa",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -963,7 +1058,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"execution_count": 59,
|
||||
"id": "abe093f5-b911-434b-8071-00c9d177e5bf",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -972,16 +1067,19 @@
|
||||
" def __init__(self, name, age):\n",
|
||||
" self.__name = name # Assume it has getter/setters \n",
|
||||
" # invokes setter\n",
|
||||
" self.age = age\n",
|
||||
" self.age = age #self.set_age(age)\n",
|
||||
" self.birth_date = ...\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" def age(self):\n",
|
||||
" \"\"\" returns the age property \"\"\"\n",
|
||||
" print('getter called')\n",
|
||||
" return self.__age\n",
|
||||
" #age = property(age)\n",
|
||||
" \n",
|
||||
" @age.setter\n",
|
||||
" def age(self, age):\n",
|
||||
" print('setter called')\n",
|
||||
" if age < 0:\n",
|
||||
" raise ValueError(\"Person can't have a negative age!\")\n",
|
||||
" self.__age = age\n",
|
||||
@ -992,14 +1090,24 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"execution_count": 61,
|
||||
"id": "3bc6ff3e-9dc3-4b48-876b-3a7a98911da4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"setter called\n",
|
||||
"getter called\n",
|
||||
"28\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"p2 = Person(\"Emma\", 28)\n",
|
||||
"#p2.age = -1\n",
|
||||
"p2.age = 4"
|
||||
"print(p2.age)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -1043,7 +1151,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"execution_count": 26,
|
||||
"id": "0c9fc2ed-9a6e-41d3-a1bb-a7c13641d617",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -1055,28 +1163,29 @@
|
||||
" def __init__(self,width,height):\n",
|
||||
" self.width = width \n",
|
||||
" self.height = height \n",
|
||||
" self.area = width*height\n",
|
||||
" \n",
|
||||
" # read-only calculated property\n",
|
||||
" @property \n",
|
||||
" def area(self):\n",
|
||||
" return self.width * self.height "
|
||||
" #@property \n",
|
||||
" #def area(self):\n",
|
||||
" # return self.width * self.height "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"execution_count": 29,
|
||||
"id": "2aa20ea3-1292-4224-9d99-b40eab748021",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"r = Rectangle(3, 9)\n"
|
||||
"r = Rectangle(3, 9)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 41,
|
||||
"execution_count": 30,
|
||||
"id": "a03f9038-7b97-4e0b-a46e-b8d558f12882",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@ -1096,7 +1205,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"execution_count": 31,
|
||||
"id": "7d9ce39e-bfc3-4389-9315-a9bb16dcdec0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -1104,7 +1213,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"54\n"
|
||||
"27\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -1116,7 +1225,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"execution_count": 78,
|
||||
"id": "489244f1-9568-4c73-93c8-86cb6ffecfe4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -1127,7 +1236,7 @@
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[48], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# but can't be set\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marea\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m4\u001b[39m\n",
|
||||
"Cell \u001b[0;32mIn[78], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# but can't be set\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marea\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m4\u001b[39m\n",
|
||||
"\u001b[0;31mAttributeError\u001b[0m: can't set attribute 'area'"
|
||||
]
|
||||
}
|
||||
@ -1139,9 +1248,31 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 25,
|
||||
"id": "a5ffcdd9-5c89-4cdb-84cc-81b255c2e28f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "AttributeError",
|
||||
"evalue": "can't delete attribute 'area'",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[25], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m r\u001b[38;5;241m.\u001b[39marea\n",
|
||||
"\u001b[0;31mAttributeError\u001b[0m: can't delete attribute 'area'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"del r.area"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "952bb276-725e-43ea-835d-3ab4116696b6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
|
@ -79,7 +79,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 1,
|
||||
"id": "cfb1a2af-b065-43cf-b7b0-3bfb046d9d85",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -88,7 +88,7 @@
|
||||
" def __init__(self, value, _next=None):\n",
|
||||
" self.value = value\n",
|
||||
" self.next = _next\n",
|
||||
"\n",
|
||||
" self.prev = ..\n",
|
||||
"\n",
|
||||
"class LinkedList:\n",
|
||||
" def __init__(self):\n",
|
||||
@ -118,7 +118,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"id": "2a796778-c06e-4585-b201-07061d25e561",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -218,7 +218,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 3,
|
||||
"id": "6c73e9f9-17e4-4057-b5fb-d169d0db9b96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -245,36 +245,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 6,
|
||||
"id": "1db244c8-9168-4c19-a73c-53a20c48485a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"called h()\n",
|
||||
" TOP -> [ h() ]\n",
|
||||
"\n",
|
||||
"h called g()\n",
|
||||
" TOP -> [ g() ]\n",
|
||||
" [ h() ]\n",
|
||||
"\n",
|
||||
"g called f()\n",
|
||||
" TOP -> [ f() ]\n",
|
||||
" [ g() ]\n",
|
||||
" [ h() ]\n",
|
||||
"\n",
|
||||
"exited f()\n",
|
||||
" TOP -> [ g() ]\n",
|
||||
" [ h() ]\n",
|
||||
"\n",
|
||||
"exited g()\n",
|
||||
" TOP -> [ h() ]\n",
|
||||
"\n",
|
||||
"exited h()\n",
|
||||
" TOP -> \n"
|
||||
"ename": "NameError",
|
||||
"evalue": "name 'Stack' is not defined",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s \u001b[38;5;241m=\u001b[39m \u001b[43mStack\u001b[49m()\n\u001b[1;32m 2\u001b[0m s\u001b[38;5;241m.\u001b[39mpush(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mh()\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mcalled h()\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
|
||||
"\u001b[0;31mNameError\u001b[0m: name 'Stack' is not defined"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -311,7 +294,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 4,
|
||||
"id": "4525594e-070b-4bcb-b4c7-a3641f514b2d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -344,7 +327,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 5,
|
||||
"id": "20c843b5-9c64-45cb-af77-514b680cbd9c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -398,7 +381,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 6,
|
||||
"id": "a414509d-84bb-4ae7-a105-2c6b9dcb7406",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -417,7 +400,7 @@
|
||||
"@print_elapsed\n",
|
||||
"def testfunc(QueueCls):\n",
|
||||
" queue = QueueCls()\n",
|
||||
" for item in range(100):\n",
|
||||
" for item in range(1000):\n",
|
||||
" queue.push(item)\n",
|
||||
" while queue:\n",
|
||||
" queue.pop()"
|
||||
@ -425,7 +408,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 7,
|
||||
"id": "f1842168-b12d-4d1c-a004-10614904f3f2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -433,17 +416,17 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Took 7.605552673339844e-05 sec to run testfunc\n"
|
||||
"Took 0.0007648468017578125 sec to run testfunc\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"testfunc(ArrayQueue)\n"
|
||||
"testfunc(ArrayQueue)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 8,
|
||||
"id": "19a3b107-da59-40f3-bafb-70160747fb53",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -451,7 +434,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Took 7.700920104980469e-05 sec to run testfunc\n"
|
||||
"Took 0.0006389617919921875 sec to run testfunc\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -479,7 +462,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 9,
|
||||
"id": "93010cc5-55c9-4308-b0d0-8fac623f2c55",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -487,9 +470,9 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1000000x ArrayQueue.push, took 0.10346049995860085\n",
|
||||
"1000000x DequeQueue.push, took 0.06602491601370275\n",
|
||||
"DequeQueue is 36.183% faster\n"
|
||||
"1000000x ArrayQueue.push, took 0.10360287500009235\n",
|
||||
"1000000x DequeQueue.push, took 0.06678124999996271\n",
|
||||
"DequeQueue is 35.541% less time\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -512,12 +495,12 @@
|
||||
")\n",
|
||||
"print(f\"{number}x ArrayQueue.push, took\", elapsed)\n",
|
||||
"print(f\"{number}x DequeQueue.push, took\", elapsed2)\n",
|
||||
"print(f\"DequeQueue is {(elapsed-elapsed2) / elapsed * 100:.3f}% faster\")\n"
|
||||
"print(f\"DequeQueue is {(elapsed-elapsed2) / elapsed * 100:.3f}% less time\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 10,
|
||||
"id": "f40f7691-599c-49f9-8f08-30aa14c1e90a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -525,9 +508,9 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"10000x ArrayQueue.pop, took 1.7424908330431208\n",
|
||||
"10000x DequeQueue.pop, took 0.0005924999713897705\n",
|
||||
"DequeQueue is 99.966% faster\n"
|
||||
"10000x ArrayQueue.pop, took 17.357110499999976\n",
|
||||
"10000x DequeQueue.pop, took 0.0005909170000677477\n",
|
||||
"DequeQueue is 99.997% less time\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -536,19 +519,19 @@
|
||||
"\n",
|
||||
"elapsed = timeit.timeit(\n",
|
||||
" \"queue.pop()\",\n",
|
||||
" setup=\"queue = QueueCls([0] * 1000000)\",\n",
|
||||
" setup=\"queue = QueueCls([0] * 10000000)\",\n",
|
||||
" globals={\"QueueCls\": ArrayQueue},\n",
|
||||
" number=number,\n",
|
||||
")\n",
|
||||
"elapsed2 = timeit.timeit(\n",
|
||||
" \"queue.pop()\",\n",
|
||||
" setup=\"queue = QueueCls([0] * 1000000)\",\n",
|
||||
" setup=\"queue = QueueCls([0] * 10000000)\",\n",
|
||||
" globals={\"QueueCls\": DequeQueue},\n",
|
||||
" number=number,\n",
|
||||
")\n",
|
||||
"print(f\"{number}x ArrayQueue.pop, took\", elapsed)\n",
|
||||
"print(f\"{number}x DequeQueue.pop, took\", elapsed2)\n",
|
||||
"print(f\"DequeQueue is {(elapsed-elapsed2) / elapsed * 100:.3f}% faster\")"
|
||||
"print(f\"DequeQueue is {(elapsed-elapsed2) / elapsed * 100:.3f}% less time\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -78,7 +78,8 @@
|
||||
" print_infix(node.left)\n",
|
||||
" print(node.value)\n",
|
||||
" if node.right:\n",
|
||||
" print_infix(node.right)"
|
||||
" print_infix(node.right)\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
|
401
hashtables.ipynb
Normal file
401
hashtables.ipynb
Normal file
@ -0,0 +1,401 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6c3f443-273b-4f49-9339-902e3251e0e4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Hash Tables\n",
|
||||
"\n",
|
||||
"A hash table is a collection that maps keys to values. Python's `dict` is an implementation of a hash table.\n",
|
||||
"\n",
|
||||
"For the course project, you will be implementing a hash table without using `dict`.\n",
|
||||
"\n",
|
||||
"### Hash Table Performance\n",
|
||||
"\n",
|
||||
"| Operation | Average | Worst Case | \n",
|
||||
"| --------- | ---- | ---------- |\n",
|
||||
"| lookup | O(1) | O(n) |\n",
|
||||
"| insert | O(1) | O(n) |\n",
|
||||
"| delete | O(1) | O(n) |\n",
|
||||
"\n",
|
||||
"Note: These are average case, as we'll see, depending on implementation, worst case can be much worse.\n",
|
||||
"\n",
|
||||
"A key property for hash tables is that we **do not need to linearly search through them for our data**.\n",
|
||||
"\n",
|
||||
"If you find yourself scanning every element in a hash table, you're doing something wrong.\n",
|
||||
"\n",
|
||||
"### Example\n",
|
||||
"\n",
|
||||
"Let's first model a simple hashtable with fixed capacity of 10. For simplicity we'll stick to string keys.\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"When we get a key-value pair, we need to assign it a bucket.\n",
|
||||
"\n",
|
||||
"How can we write a function that takes a string and assigns it to a bucket?\n",
|
||||
"\n",
|
||||
"1. Turn string into a number. **Hash Function**\n",
|
||||
"2. Take (number % capacity)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "450367cc-fbd5-4b97-89d6-37f69cba5d91",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def strhash(key):\n",
|
||||
" # ord converts a character to it's numeric representation\n",
|
||||
" # ord(\"A\") == 65\n",
|
||||
" # ord(\"z\") == 122\n",
|
||||
" # etc.\n",
|
||||
" return sum(ord(letter) for letter in key)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "e689c2aa-a3a5-4400-b7bb-557944a725da",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"410"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"strhash(\"bear\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "bb545dc8-7db6-46a0-bbc3-716f6bf3639b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"333"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"strhash(\"fox\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "0ef489a6-7f2c-46aa-aa2e-3ff10241eb46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Hashtable:\n",
|
||||
" def __init__(self, capacity=10):\n",
|
||||
" self._table = [None] * capacity\n",
|
||||
" self.capacity = capacity\n",
|
||||
"\n",
|
||||
" def __setitem__(self, key, value):\n",
|
||||
" index = strhash(key) % self.capacity\n",
|
||||
" self._table[index] = value\n",
|
||||
"\n",
|
||||
" def __getitem__(self, key):\n",
|
||||
" index = strhash(key) % self.capacity\n",
|
||||
" value = self._table[index]\n",
|
||||
" if value:\n",
|
||||
" return value\n",
|
||||
" else:\n",
|
||||
" raise KeyError(key)\n",
|
||||
"\n",
|
||||
" def display(self):\n",
|
||||
" print(f\"Capacity = {self.capacity}\")\n",
|
||||
" for idx, elem in enumerate(self._table):\n",
|
||||
" if elem:\n",
|
||||
" print(f\"{idx}: {elem}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "8f73d5b3-1fbe-4439-a19f-3c1c73cf1e04",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Capacity = 10\n",
|
||||
"0: 3\n",
|
||||
"3: 12\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"h = Hashtable()\n",
|
||||
"h[\"bear\"] = 3\n",
|
||||
"h[\"fox\"] = 12\n",
|
||||
"h.display()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "1e68e2ee-9feb-4623-88a3-03b16d7fedd4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(h[\"bear\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "1535a473-c2e5-474f-8a80-e89ff3bd00bd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"\n",
|
||||
"strhash(\"bear\") == strhash(\"been\") # different word, same hash!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "3458faf2-cc40-4e31-a792-7a1515f718e7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Capacity = 10\n",
|
||||
"0: 12\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"h = Hashtable()\n",
|
||||
"h[\"bear\"] = 33\n",
|
||||
"h[\"been\"] = 12\n",
|
||||
"h.display()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b23a0b1f-1e59-4d83-aa4c-2c57d5706298",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Linear Probing\n",
|
||||
"\n",
|
||||
"One solution is to just walk forward in the storage list, until we find an empty space.\n",
|
||||
"\n",
|
||||
"Either way, we'll need to start storing the key as well. Let's revise our class:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "6a5dd148-07ce-4e02-998a-75b9edce31f7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Hashtable:\n",
|
||||
" def __init__(self, capacity=100):\n",
|
||||
" self._table = [None] * 100\n",
|
||||
" self.capacity = capacity\n",
|
||||
"\n",
|
||||
" def __setitem__(self, key, value):\n",
|
||||
" index = strhash(key) % self.capacity\n",
|
||||
" while self._table[index] is not None:\n",
|
||||
" index += 1\n",
|
||||
" # Handling wrap-around omitted for brevity\n",
|
||||
"\n",
|
||||
" # we now store the key and value\n",
|
||||
" self._table[index] = (key, value)\n",
|
||||
"\n",
|
||||
" def __getitem__(self, key):\n",
|
||||
" index = strhash(key) % self.capacity\n",
|
||||
"\n",
|
||||
" # walk forward until we either reach the item or an empty space\n",
|
||||
" while self._table[index] is not None:\n",
|
||||
" if self._table[index][0] == key:\n",
|
||||
" return self._table[index][1]\n",
|
||||
" index += 1\n",
|
||||
" # Handling wrap-around omitted for brevity\n",
|
||||
"\n",
|
||||
" # if code got here, the item wasn't in the list\n",
|
||||
" raise KeyError(key)\n",
|
||||
"\n",
|
||||
" def display(self):\n",
|
||||
" print(f\"Capacity = {self.capacity}\")\n",
|
||||
" for idx, elem in enumerate(self._table):\n",
|
||||
" if elem:\n",
|
||||
" print(f\"{idx}: {elem}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "3251b310-dfae-4aae-9d04-603a5e20dbea",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Capacity = 10\n",
|
||||
"0: 12\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"h = Hashtable()\n",
|
||||
"h[\"bear\"] = 33\n",
|
||||
"h[\"been\"] = 12\n",
|
||||
"h.display()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "781024b5-a81d-46be-8c87-84f3860395c6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"12\n",
|
||||
"12\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(h[\"bear\"])\n",
|
||||
"print(h[\"been\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f90d7b38-2f69-465d-8da1-59f8efd82909",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Separate Chaining\n",
|
||||
"\n",
|
||||
"Another solution is to use a linked list to store multiple items in the same bucket. One could imagine each bucket being the head of a linked list where items that hash to that value are stored linearly.\n",
|
||||
"\n",
|
||||
"This is the approach we're asking you to use for the course project.\n",
|
||||
"\n",
|
||||
"**What happens if we have a lot of items in the same bucket?**\n",
|
||||
"\n",
|
||||
"### Better Hash Function\n",
|
||||
"\n",
|
||||
"Ideally a hash function will evenly distribute values across the collection.\n",
|
||||
"\n",
|
||||
"A common pattern is to use a polynomial hash function.\n",
|
||||
"\n",
|
||||
"$$h(x_0, ..., x_n) = (\\sum_{i=0}^{k-1}{c_ip^i})\\mod{m}$$\n",
|
||||
"\n",
|
||||
"Where:\n",
|
||||
"- $x_0...x_i $ is the sequence\n",
|
||||
"- $k = len(x)$\n",
|
||||
"- $c_i$ is the numeric value of the character $x_i$ ($ord(x$ in Python)\n",
|
||||
"- $p$ is an arbitrary constant.\n",
|
||||
"- and $m$ is the size of the collection.\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
" def _hash(self, key):\n",
|
||||
" \"\"\"\n",
|
||||
" This method takes in a string and returns \n",
|
||||
" an integer value between 0 and self.capacity.\n",
|
||||
"\n",
|
||||
" This particular hash function uses \n",
|
||||
" Horner's rule to compute a large polynomial.\n",
|
||||
" \"\"\"\n",
|
||||
" val = 0\n",
|
||||
" for letter in key:\n",
|
||||
" val = self.P_CONSTANT * val + ord(letter)\n",
|
||||
" return val % self.capacity\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"### Questions\n",
|
||||
"\n",
|
||||
"* Linear probing vs. separate chaining? Other approaches?\n",
|
||||
"* What if our hash function wasn't reliable?\n",
|
||||
"\n",
|
||||
"### Rehashing\n",
|
||||
"\n",
|
||||
"As we add more items to our hash table, we'll eventually run out of space. We can increase the capacity of our table, but we'll need to rehash all of our existing items.\n",
|
||||
"\n",
|
||||
"Because which bucket we choose depends on `hash(item) % capacity` items would end up in different buckets if we change capacity.\n",
|
||||
"\n",
|
||||
"This means when we resize, we need to rehash all of our items.\n",
|
||||
"\n",
|
||||
"A common pattern is to double capacity when the table is ~50% full."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c1d5023d-25db-4885-a303-2c34d1a453de",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue
Block a user