Skip to content

Commit

Permalink
mcq reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorhuang1 committed Mar 15, 2024
1 parent 09bbd63 commit 534f0fa
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 35 deletions.
272 changes: 256 additions & 16 deletions _notebooks/2024-03-12-CSP-MCQ-2020-missed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -76,9 +76,18 @@
"print(f\"Overflow float to infinity: {overflow_float}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 1\n",
"- Binary overflow because it is confined to 8 bits but exceeds 255\n",
"- Underflow because the binary bits cannot represent negative numbers"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -190,9 +199,28 @@
" print(A, B, C, D, \"-->\", circuit(A, B, C, D))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 2\n",
"- New truth table using NOT and NOR gates\n",
"- There are only 8 combinations since the not gate only takes 1 argument\n",
"- Scenario\n",
" - Light Switch where A is if there is motion\n",
" - B is if the light is on\n",
" - C is time of day\n",
" - EX: There is motion --> A is True (NOT gate returns false)\n",
" - The light is on --> B is true\n",
" - It is 10 PM --> B is true\n",
" - NOR gate returns true\n",
" - Second NOR gate between false and true returns true\n",
" - The light should remain on"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -223,6 +251,7 @@
"# Print truth table for circuit\n",
"print('A', 'B', 'C', \"-->\", 'Output')\n",
"\n",
"# Hypothetical situation of turning on/off a light\n",
"def circuit(A, B, C):\n",
" return NOR_gate(NOT_gate(A), NOR_gate(B, C))\n",
"\n",
Expand Down Expand Up @@ -264,7 +293,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -371,9 +400,16 @@
"plot_colors(rgb_primary)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 44,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -409,14 +445,17 @@
"proportion 0.0 0.0 0.0\n",
"binary: 11111111 11111111 11111111\n",
"decimal 255 255 255\n",
"proportion 1.0 1.0 1.0\n"
"proportion 1.0 1.0 1.0\n",
"binary: 00000000 11111111 11111111\n",
"decimal 0 255 255\n",
"proportion 0.0 1.0 1.0\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAAB7CAYAAADXClMoAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAB/0lEQVR4nO3aQWrEMBAAQU3Y/3958gPnYtomW3W1EYJBfRCa3T0ANH6e3gDANxFdgJDoAoREFyAkugAh0QUIfa4+zoz3ZC+xu3PXWub6HnfO9RyzfYuruV5G95xz9pjh0+bcei7557y9f9bM9Xl1vQAQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkB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ACQCCYABAIJgAEggkAgWACQPAJnacd/N5/5A0AAAAASUVORK5CYII=",
"text/plain": [
"<Figure size 432x144 with 3 Axes>"
"<Figure size 576x144 with 4 Axes>"
]
},
"metadata": {
Expand All @@ -432,7 +471,8 @@
"# Standard Color Examples\n",
"standard_colors = [('11111111', '00000000', '11111111'), \n",
" ('00000000', '00000000', '00000000'),\n",
" ('11111111', '11111111', '11111111')]\n",
" ('11111111', '11111111', '11111111'),\n",
" ('00000000', '11111111', '11111111'),]\n",
"plot_colors(standard_colors)"
]
},
Expand Down Expand Up @@ -465,16 +505,16 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Algorithm 2 * N took 0.29 milliseconds\n",
"Algorithm N^2 took 786.55 milliseconds\n",
"Algorithm 10 times took 0.07 milliseconds\n"
"Algorithm 2 * N took 0.06 milliseconds\n",
"Algorithm N^2 took 7.62 milliseconds\n",
"Algorithm 10 times took 0.03 milliseconds\n"
]
}
],
Expand Down Expand Up @@ -507,7 +547,7 @@
" pass\n",
"\n",
"# Create a large list\n",
"n = 10000\n",
"n = 1000\n",
"lst = list(range(n))\n",
"\n",
"# Measure the time taken by algorithm1\n",
Expand All @@ -533,6 +573,65 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 4\n",
"- Recursion\n",
" - O(2^n) time complexity because it calls itself within 2 times\n",
"- O(n^4)\n",
" - 4 nested loops"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Recursion times took 180.93 milliseconds\n",
"O(n^4) times took 922.20 milliseconds\n"
]
}
],
"source": [
"def recursion(num):\n",
" if num <= 1:\n",
" return num\n",
" return recursion(num - 1) - recursion(num - 2) \n",
"\n",
"start = time.time()\n",
"recursion(30)\n",
"end = time.time()\n",
"\n",
"print(f\"Recursion times took {(end - start)*1000:.2f} milliseconds\")\n",
"\n",
"def powerOfFour(lst):\n",
" n = len(lst)\n",
" for i in range(n):\n",
" pass\n",
" for j in range(n):\n",
" pass\n",
" for k in range(n):\n",
" pass\n",
" for l in range(n):\n",
" pass\n",
"\n",
"\n",
"n = 100\n",
"lst = list(range(n))\n",
"start = time.time()\n",
"powerOfFour(lst)\n",
"end = time.time()\n",
"\n",
"print(f\"O(n^4) times took {(end - start)*1000:.2f} milliseconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 56. Compare execution times of tow version (1D analysis) - Kayden Le\n",
"\n",
"An online game collects data about each player’s performance in the game. A program is used to analyze the data to make predictions about how players will perform in a new version of the game.\n",
Expand Down Expand Up @@ -611,6 +710,60 @@
"version_II(idList)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 5\n",
"- Slow recursion uses recursion to get the answer\n",
"- Fast recursion uses memoization\n",
" - Stores the values so it is very quick\n",
" - O(n) run time vs O(2^n) of slow fibonacci\n",
" - <a href=\"https://www.youtube.com/watch?v=P8Xa2BitN3I\">Youtube Video</a>"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slow Fibonacci with a small number took 0.00 seconds\n",
"Fast Fibonacci with a large number took 0.01 seconds\n"
]
}
],
"source": [
"def slowFibonacci(num):\n",
" if num <= 1:\n",
" return num\n",
" else:\n",
" return slowFibonacci(num-1) + slowFibonacci(num-2)\n",
"\n",
"start = time.time()\n",
"smallSlow = slowFibonacci(5)\n",
"end = time.time()\n",
"print(f\"Slow Fibonacci with a small number took {end - start:.2f} seconds\")\n",
"\n",
"#start = time.time()\n",
"#smallSlow = slowFibonacci(40)\n",
"#end = time.time()\n",
"#print(f\"Slow Fibonacci with a large number took {end - start:.2f} seconds\")\n",
"\n",
"def fastFibonacci(n, computed = {0: 0, 1: 1}):\n",
" if n not in computed:\n",
" computed[n] = fastFibonacci(n-1, computed) + fastFibonacci(n-2, computed)\n",
" return computed[n]\n",
"\n",
"start = time.time()\n",
"fast = fastFibonacci(1000)\n",
"end = time.time()\n",
"print(f\"Fast Fibonacci with a large number took {end - start:.2f} seconds\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -664,9 +817,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 70,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"0\n",
"-10\n",
"0\n"
]
}
],
"source": [
"# flawed multiply function\n",
"# as a popcorn hack (coding challenge), fix the multiply function to work with negative numbers\n",
Expand All @@ -691,6 +855,45 @@
"print(multiply(-2, -5)) # Expected output: 10, Actual output: 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 6\n",
"- Fixed using abs and multiplying by the sign at the end"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10.0\n",
"-10.0\n",
"-10.0\n",
"10.0\n"
]
}
],
"source": [
"def multiply(x, y):\n",
" count = 0 \n",
" result = 0\n",
" while count < abs(y):\n",
" result += x\n",
" count += 1\n",
" return result * y/abs(y)\n",
"\n",
"print(multiply(2, 5)) # Expected output: 10\n",
"print(multiply(2, -5)) # Expected output: -10, Actual output: 0\n",
"print(multiply(-2, 5)) # Expected output: -10\n",
"print(multiply(-2, -5)) # Expected output: 10, Actual output: 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -745,6 +948,43 @@
"animal = \"antelope\"[4:8] + animal # Concat(Substring(\"antelope\", 5, 4), animal)\n",
"print(animal) # Outputs: lopejacka"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack 7"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"jackalope\n",
"jackalope\n"
]
}
],
"source": [
"# A is already done above\n",
"\n",
"# B --> this prints jackalope as intended\n",
"animal = \"antelope\"[4:8]\n",
"animal = \"a\" + animal\n",
"animal = \"jackrabbit\"[0:4] + animal\n",
"print(animal)\n",
"\n",
"# C --> this prints jackalope as well\n",
"animal = \"jackrabbit\"[0:4]\n",
"animal = animal + \"a\"\n",
"animal = animal + \"antelope\"[4:8]\n",
"print(animal)\n"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 534f0fa

Please sign in to comment.