This commit is contained in:
James Turk 2024-11-19 18:07:13 -06:00
parent 987d203eba
commit dea7029598
4 changed files with 82 additions and 29 deletions

View File

@ -294,7 +294,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"id": "4525594e-070b-4bcb-b4c7-a3641f514b2d",
"metadata": {},
"outputs": [],
@ -327,7 +327,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"id": "20c843b5-9c64-45cb-af77-514b680cbd9c",
"metadata": {},
"outputs": [],
@ -381,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"id": "a414509d-84bb-4ae7-a105-2c6b9dcb7406",
"metadata": {},
"outputs": [],
@ -408,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"id": "f1842168-b12d-4d1c-a004-10614904f3f2",
"metadata": {},
"outputs": [
@ -416,7 +416,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Took 0.0007648468017578125 sec to run testfunc\n"
"Took 0.0012509822845458984 sec to run testfunc\n"
]
}
],
@ -426,7 +426,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"id": "19a3b107-da59-40f3-bafb-70160747fb53",
"metadata": {},
"outputs": [
@ -434,7 +434,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Took 0.0006389617919921875 sec to run testfunc\n"
"Took 0.001255035400390625 sec to run testfunc\n"
]
}
],
@ -462,7 +462,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"id": "93010cc5-55c9-4308-b0d0-8fac623f2c55",
"metadata": {},
"outputs": [
@ -470,9 +470,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"1000000x ArrayQueue.push, took 0.10360287500009235\n",
"1000000x DequeQueue.push, took 0.06678124999996271\n",
"DequeQueue is 35.541% less time\n"
"1000000x ArrayQueue.push, took 0.11357445899921004\n",
"1000000x DequeQueue.push, took 0.06381245900047361\n",
"DequeQueue is 43.814% less time\n"
]
}
],
@ -500,7 +500,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"id": "f40f7691-599c-49f9-8f08-30aa14c1e90a",
"metadata": {},
"outputs": [
@ -508,8 +508,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"10000x ArrayQueue.pop, took 17.357110499999976\n",
"10000x DequeQueue.pop, took 0.0005909170000677477\n",
"10000x ArrayQueue.pop, took 16.82198095900094\n",
"10000x DequeQueue.pop, took 0.0005862910002178978\n",
"DequeQueue is 99.997% less time\n"
]
}
@ -534,6 +534,48 @@
"print(f\"DequeQueue is {(elapsed-elapsed2) / elapsed * 100:.3f}% less time\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "4333f0dd-846e-44fa-8aa0-9c3d883c165e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"58.76708083400081"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"timeit.timeit(\"''.join(['a', 'b', 'c', 'd'])\", number=1000000000)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5a4a6e0d-0e02-42bf-8d53-06b09ce1a5dd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.945709666997573"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"timeit.timeit(\"d = ('apple'*5) + 'banana' + 'c' + 'd'\", number=1000000000)"
]
},
{
"cell_type": "markdown",
"id": "b20fc917-73d2-4378-ae0a-a2119883efa3",

View File

@ -139,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"id": "a06805c2-75ae-46e3-9916-f4ee8cca6080",
"metadata": {},
"outputs": [
@ -147,8 +147,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n",
"defaultdict(<class 'set'>, {'newkey': set()})\n"
"{1, 2, 3}\n",
"defaultdict(<function <lambda> at 0x111069120>, {'newkey': {1, 2, 3}})\n",
"defaultdict(<function <lambda> at 0x111069120>, {'newkey': {1, 2, 3, 4}})\n"
]
}
],
@ -156,12 +157,13 @@
"from collections import defaultdict\n",
"\n",
"# give defaultdict a function that it will use to generate missing keys\n",
"dd = defaultdict(set)\n",
"dd = defaultdict(lambda: {1, 2, 3})\n",
"\n",
"print(dd[\"newkey\"])\n",
"print(dd)\n",
"\n",
"dd[\"newset\"].add(1) # can add to set without ensuring it exists"
"dd[\"newkey\"].add(4) # can add to set without ensuring it exists\n",
"print(dd)"
]
},
{
@ -176,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 13,
"id": "323a6e33-76b7-41b7-97b9-e4dfa04d6bb4",
"metadata": {},
"outputs": [],
@ -207,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 14,
"id": "867f5186-e308-4d2f-9766-713ecb352f99",
"metadata": {},
"outputs": [
@ -217,7 +219,7 @@
"('A', 'D', 'B')"
]
},
"execution_count": 11,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@ -231,17 +233,17 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 17,
"id": "a9acaa28-8a24-41c5-86c0-4342365533f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('A', 'B', 'C', 'D', 'E')"
"('A', 'B', 'A', 'D', 'E')"
]
},
"execution_count": 13,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@ -252,6 +254,7 @@
"g.add_edge(\"B\", \"C\")\n",
"g.add_edge(\"C\", \"D\")\n",
"g.add_edge(\"D\", \"E\")\n",
"g.add_edge(\"A\", \"D\")\n",
"g.find_path(\"A\", \"E\")"
]
},
@ -294,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"id": "1ed75b91-54fe-4650-965d-ef26507788b9",
"metadata": {},
"outputs": [],
@ -303,7 +306,7 @@
"class TrieNode:\n",
" def __init__(self, value=None):\n",
" self.value = value\n",
" self.children = (None, None, None, None)\n"
" self.children = [None, None, None, None]\n"
]
},
{
@ -382,6 +385,8 @@
" If color is set to None, the background will be transparent.\n",
" If color is not set, the background will default to the user's choice.\n",
" \"\"\"\n",
" if color is DefaultColor:\n",
" ...\n",
"```\n"
]
},

View File

@ -84,7 +84,13 @@
"If in doubt:\n",
"- look for signs of activity on GitHub/etc.: a popular library with dozens of tutorials is one thing -- an obscure library only one person used may also be fine, but worth a bit of vetting\n",
"- look at the source code!\n",
"- ask someone! (James, TAs, etc.)"
"- ask someone! (James, TAs, etc.)\n",
"\n",
"### Licensing\n",
"\n",
"Code that is published & open source comes with a license, a set of rules saying what you may and may not do with it. Typically this prohibits redistribution of the work without the license, but in some cases may mean that your own work needs to be open source to use it. **Using open source code without following the license is plagarism/theft and can come with serious consequences here and in any workplace since your employer would carry the legal burden.**\n",
"\n",
"Make sure that the code that you are using is under a license that allows you to use it in the environment that you are in. That isn't much of an issue here in class, but in companies you may not be able to use certain licenses. "
]
},
{

View File

@ -267,6 +267,7 @@
" assert sum([1, 2, 3]) == 6\n",
"\n",
"def test_sum_type_error():\n",
" ...\n",
" with pytest.raises(TypeError):\n",
" sum([1, \"hello\"])\n",
"```\n",
@ -282,7 +283,6 @@
"For example:\n",
"\n",
"```python\n",
"\n",
"def test_create_user():\n",
" db = Database(\"test.db\")\n",
" db.create_user(username=\"alice\")\n",