Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahau committed May 17, 2024
1 parent 1e19175 commit d93d824
Show file tree
Hide file tree
Showing 12 changed files with 1,266 additions and 3 deletions.
6 changes: 3 additions & 3 deletions 4a. Hash table.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
"source": [
"Thus the hash table as follows after the additions:\n",
"\n",
"| Location | 00 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 |\n",
"| -------- | ---- | -- | -- | -- | -- | -- | ---- | -- | -- | -- |\n",
"| Elements | 3030 | – | – | 99 | 22 | 55 | 1818 | – | 44 | – |\n",
"| Location | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n",
"| -------- | -- | - | - | - | - | - | -- | - | - | - |\n",
"| Elements | 30 | – | – | 9 | 2 | 5 | 18 | – | 4 | – |\n",
"\n",
"When we want to check if the hash table contains an element $x$, we search for it at the location $7x \\bmod 10$. For example, we will search for the element $4$ at the location $7 \\cdot 4 \\bmod 10 = 8$."
]
Expand Down
217 changes: 217 additions & 0 deletions Exercises/Week 4/25 - Robot route.ipynb

Large diffs are not rendered by default.

279 changes: 279 additions & 0 deletions Exercises/Week 4/26 - No pair.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# No pair"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You are given a list containing $n$ integers. Every number has exactly two occurrences on the list, except one number that occurs only once. Your task is to find this number.\n",
"\n",
"The time complexity of the algorithm should be $O(n)$.\n",
"\n",
"In a file `nopair.py`, implement a function `find` that returns the desired number."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def find(t):\n",
" # TODO\n",
"\n",
"if __name__ == \"__main__\":\n",
" print(find([2,1,3,2,3])) # 1\n",
" print(find([5,5,9])) # 9\n",
" print(find([1,2,3,4,1,3,4])) # 2\n",
" print(find([8])) # 8\n",
" print(find([7,1,7,4,4,5,1])) # 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{2: 2, 1: 1, 3: 2}\n",
"1\n",
"{5: 2, 9: 1}\n",
"9\n",
"{1: 2, 2: 1, 3: 2, 4: 2}\n",
"2\n",
"{8: 1}\n",
"8\n",
"{7: 2, 1: 2, 4: 2, 5: 1}\n",
"5\n"
]
}
],
"source": [
"def find(t):\n",
" d = {}\n",
" \n",
" for i in t:\n",
" if i not in d: d[i] = 0 # noqa: E701\n",
" d[i] += 1\n",
" \n",
" keys = list(d.keys())\n",
" counts = list(d.values())\n",
" \n",
" print(d)\n",
" return keys[counts.index(1)]\n",
" \n",
"\n",
"if __name__ == \"__main__\":\n",
" print(find([2,1,3,2,3])) # 1\n",
" print(find([5,5,9])) # 9\n",
" print(find([1,2,3,4,1,3,4])) # 2\n",
" print(find([8])) # 8\n",
" print(find([7,1,7,4,4,5,1])) # 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attempt 2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"9\n",
"2\n",
"8\n",
"5\n"
]
}
],
"source": [
"def find(t):\n",
" d = set()\n",
" \n",
" for i in t:\n",
" if i not in d: \n",
" d.add(i) \n",
" else:\n",
" d.remove(i)\n",
" \n",
" return next(iter(d))\n",
" \n",
"\n",
"if __name__ == \"__main__\":\n",
" print(find([2,1,3,2,3])) # 1\n",
" print(find([5,5,9])) # 9\n",
" print(find([1,2,3,4,1,3,4])) # 2\n",
" print(find([8])) # 8\n",
" print(find([7,1,7,4,4,5,1])) # 5"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"9\n",
"2\n",
"8\n",
"5\n"
]
}
],
"source": [
"def find(t):\n",
" d = set()\n",
" \n",
" for i in t:\n",
" d.add(i) if i not in d else d.remove(i)\n",
" \n",
" return next(iter(d))\n",
" \n",
"\n",
"if __name__ == \"__main__\":\n",
" print(find([2,1,3,2,3])) # 1\n",
" print(find([5,5,9])) # 9\n",
" print(find([1,2,3,4,1,3,4])) # 2\n",
" print(find([8])) # 8\n",
" print(find([7,1,7,4,4,5,1])) # 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following solution counts the occurrences of each number using a dictionary `count`, and then finds the number with count one."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def find(t):\n",
" count = {}\n",
" \n",
" for x in t:\n",
" if x not in count:\n",
" count[x] = 0\n",
" count[x] += 1\n",
" \n",
" for x in t:\n",
" if count[x] == 1:\n",
" return x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is another solution based on the xor operator `^` that operates on the binary representations of the numbers. When all the numbers are xored together, the result has one bit at positions that had an odd number of one bits in the collection of numbers. Thus the result is exactly the number that has no pair."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def find(t):\n",
" result = 0\n",
" for x in t:\n",
" result ^= x\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"0\n",
"2\n",
"6\n",
"2\n",
"7\n"
]
}
],
"source": [
"result = 0\n",
"result ^= 10\n",
"print(result)\n",
"result ^= 10\n",
"print(result)\n",
"result ^= 2\n",
"print(result)\n",
"result ^= 4\n",
"print(result)\n",
"result ^= 4\n",
"print(result)\n",
"result ^= 5\n",
"print(result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit d93d824

Please sign in to comment.