-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e19175
commit d93d824
Showing
12 changed files
with
1,266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.