ASPP 2024 material

This commit is contained in:
Pietro Berkes 2024-08-27 15:27:53 +03:00
commit 1f6bc07c51
90 changed files with 91689 additions and 0 deletions

View file

@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "373ed1db",
"metadata": {},
"source": [
"Exercise: given 2 decks of tarod cards, `deck1` and `deck2`, find all the matching pairs. The output should be a set of tuples `(idx1, idx2)` for every matching pair in `deck1`, `deck2`.\n",
"\n",
"For examples:\n",
"```\n",
"deck1 = ['C', 'B', 'A']\n",
"deck2 = ['A', 'C', 'B']\n",
"```\n",
"\n",
"should return (in no particular order):\n",
"\n",
"```\n",
"{(0, 1), (1, 2), (2, 0)}\n",
"```\n",
"\n",
"Compute the Big-O complexity of your algorithm.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cf05b9c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deck 1: ['The Fool', 'Death', 'The Lovers', 'Judgement', 'Temperance', 'The Sun', 'The Hermit', 'The Chariot', 'The Tower', 'Wheel of Fortune', 'The Devil', 'The Emperor', 'The Empress', 'The World', 'The Hierophant', 'The High Priestess', 'The Star', 'The Hanged Man', 'Strength', 'Justice', 'The Moon', 'The Magician']\n",
"Deck 2: ['The Hermit', 'The Sun', 'The Tower', 'The Empress', 'The Star', 'The Emperor', 'The Magician', 'The Chariot', 'The Devil', 'The Moon', 'Judgement', 'Death', 'The Fool', 'Strength', 'Temperance', 'The Hierophant', 'The Lovers', 'Justice', 'Wheel of Fortune', 'The High Priestess', 'The Hanged Man', 'The World']\n"
]
}
],
"source": [
"import random\n",
"\n",
"# List of tarot card names (Major Arcana)\n",
"tarot_cards = [\n",
" \"The Fool\", \"The Magician\", \"The High Priestess\", \"The Empress\", \"The Emperor\",\n",
" \"The Hierophant\", \"The Lovers\", \"The Chariot\", \"Strength\", \"The Hermit\",\n",
" \"Wheel of Fortune\", \"Justice\", \"The Hanged Man\", \"Death\", \"Temperance\",\n",
" \"The Devil\", \"The Tower\", \"The Star\", \"The Moon\", \"The Sun\", \"Judgement\",\n",
" \"The World\"\n",
"]\n",
"\n",
"# Copy the list to create two separate decks\n",
"deck1 = tarot_cards.copy()\n",
"deck2 = tarot_cards.copy()\n",
"\n",
"# Shuffle both decks\n",
"random.shuffle(deck1)\n",
"random.shuffle(deck2)\n",
"\n",
"# Print the shuffled decks\n",
"print(\"Deck 1:\", deck1)\n",
"print(\"Deck 2:\", deck2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7685740c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 12],\n",
" [1, 11],\n",
" [2, 16],\n",
" [3, 10],\n",
" [4, 14],\n",
" [5, 1],\n",
" [6, 0],\n",
" [7, 7],\n",
" [8, 2],\n",
" [9, 18],\n",
" [10, 8],\n",
" [11, 5],\n",
" [12, 3],\n",
" [13, 21],\n",
" [14, 15],\n",
" [15, 19],\n",
" [16, 4],\n",
" [17, 20],\n",
" [18, 13],\n",
" [19, 17],\n",
" [20, 9],\n",
" [21, 6]]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card_to_matches = {}\n",
"for card in deck1: # O(N)\n",
" card_to_matches[card] = [None, None] # O(1)\n",
"\n",
"for idx1, card in enumerate(deck1): # O(N)\n",
" card_to_matches[card][0] = idx1 # O(1)\n",
" \n",
"for idx2, card in enumerate(deck2): # O(N)\n",
" card_to_matches[card][1] = idx2 # O(1)\n",
" \n",
"list(card_to_matches.values()) # O(N)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2b33252c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The World'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "509dda71",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "af52b0cd",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "198afa0e",
"metadata": {},
"outputs": [],
"source": [
"def get_slice_and_perform_something(x, axis):\n",
" idx = 75\n",
" if axis == 0:\n",
" slice_ = x[idx, :]\n",
" else:\n",
" slice_ = x[:, idx]\n",
" # Here I divide by two but any other operation will do, \n",
" # we just want to simulate the fact that we actually need to read the memory\n",
" return slice_ // 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "15900a3c",
"metadata": {},
"outputs": [],
"source": [
"page_size = 4096\n",
"n = 100\n",
"x = np.empty((page_size * n, page_size * n), dtype='int8')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c182d3d3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(409600, 409600)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "86a3e63f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"28.2 µs ± 1.22 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
]
}
],
"source": [
"%timeit get_slice_and_perform_something(x, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8b2a96c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.29 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"886 ms ± 337 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit get_slice_and_perform_something(x, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a8f67b13",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32218.18181818182"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"886000 / 27.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17a65d38",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,462 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 107,
"id": "4ccf18f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def is_view(a):\n",
" return a.base is not None\n",
"\n",
"\n",
"def print_info(a):\n",
" txt = f\"\"\"\n",
"Is it a view? {is_view(a)}\n",
"\n",
"dtype\\t{a.dtype}\n",
"ndim\\t{a.ndim}\n",
"shape\\t{a.shape}\n",
"strides\\t{a.strides}\n",
" \"\"\"\n",
" print(a)\n",
" print(txt)\n",
"\n",
"\n",
"x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "b68308e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 4)\n",
"strides\t(64, 8)\n",
" \n"
]
}
],
"source": [
"y = x[::2, :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "85feedb0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(4,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[1, :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "dbbb9a7f",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(4,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[1]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "fc63ad8c",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 4 11]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(2,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[[1, 2], [0, 3]]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "aa8effeb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"# Get the first and third row\n",
"y = x[[0, 2], :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "4474f8cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2 3 4 5]\n",
" [ 6 7 8 9]\n",
" [10 11 12 13]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"y = x + 2\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "4957469c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 9 2]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(3,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[[1, 2, 0], [1, 1, 2]]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "d1649515",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1]\n",
" [ 2 3]\n",
" [ 4 5]\n",
" [ 6 7]\n",
" [ 8 9]\n",
" [10 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(6, 2)\n",
"strides\t(16, 8)\n",
" \n"
]
}
],
"source": [
"y = x.reshape((6, 2))\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "c5caeb74",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 4]\n",
" [ 8 1]\n",
" [ 5 9]\n",
" [ 2 6]\n",
" [10 3]\n",
" [ 7 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(6, 2)\n",
"strides\t(16, 8)\n",
" \n"
]
}
],
"source": [
"y = x.T.reshape((6, 2))\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "15b0d7ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(12,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x.ravel()\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "38cc1ef3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 1 5 9 2 6 10 3 7 11]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(12,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x.T.ravel()\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "b7d0cc63",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 2 4 6 8 10]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(6,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[(x % 2) == 0]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "866e842a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"y = np.sort(x, axis=1)\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a074c89b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "320cfb50",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}