Commit c715603b authored by Franck Thollard's avatar Franck Thollard
Browse files

adding different ways of solving the smat completion DIY

parent 2f31e2af
......@@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"metadata": {},
"outputs": [
{
......@@ -162,7 +162,7 @@
"# \"pasting\" two lists can be done using zip\n",
"l1 = [1, 2, 3]\n",
"s = 'abc'\n",
"print(list(zip(l1, l2)))\n",
"print(list(zip(l1, s)))\n",
"print(list(zip('abc', 'defg')))"
]
},
......@@ -563,15 +563,15 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"integer remove from the list: 5\n",
"shuffled list: [4, 19, 14, 1, 8, 17, 15, 2, 3, 12, 0, 6, 16, 9, 11, 10, 7, 13, 18]\n"
"integer remove from the list: 3\n",
"shuffled list: [4, 2, 5, 16, 15, 6, 9, 18, 8, 7, 13, 11, 17, 14, 12, 0, 19, 1, 10]\n"
]
}
],
......@@ -610,16 +610,16 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{5}"
"3"
]
},
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
......@@ -627,7 +627,8 @@
"source": [
"full_set = set(range(n))\n",
"changed_set = set(l)\n",
"full_set - changed_set"
"ns = full_set - changed_set\n",
"ns.pop()"
]
},
{
......@@ -642,7 +643,8 @@
" \n",
" -> Complixity of the whole algorithm : O(n)\n",
" \n",
"# Note "
"# Complexity of the \"sum\" solution : \n",
" - One traversal for the computation of the sum O(n) with sum at each step O(1) -> O(n) "
]
},
{
......@@ -1010,6 +1012,96 @@
"- Given a query pattern of size 2, propose the pattern of size 3 with the same prefix that has the highest frequency. Filter the keys of the previous dictionary so that they starts with the query pattern."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"len(s) = 7160000, nbkeys 33 base, count, count_count, except, colection.counter\n",
"1 loop, best of 5: 550 ms per loop\n",
"1 loop, best of 5: 495 ms per loop\n",
"10 loops, best of 5: 146 ms per loop\n",
"1 loop, best of 5: 488 ms per loop\n",
"1 loop, best of 5: 265 ms per loop\n",
"1 loop, best of 5: 450 ms per loop\n",
"with split\n",
"len(s) = 1100000, nbkeys 90 base, count, count_count, except, colection.counter\n",
"10 loops, best of 5: 123 ms per loop\n",
"10 loops, best of 5: 113 ms per loop\n",
"1 loop, best of 5: 992 ms per loop\n",
"10 loops, best of 5: 101 ms per loop\n",
"10 loops, best of 5: 62.3 ms per loop\n",
"1 loop, best of 5: 449 ms per loop\n"
]
}
],
"source": [
"def build_count_base(t): \n",
" d = {} \n",
" for s in t:\n",
" if s in d:\n",
" d[s] += 1\n",
" else: \n",
" d[s] = 1\n",
" return d\n",
"\n",
"def build_count_set(t): \n",
" d = {k:0 for k in set(t)}\n",
" for s in t:\n",
" d[s] += 1\n",
" return d\n",
"\n",
"def build_count_count(t):\n",
" d = {k:t.count(k) for k in set(t)}\n",
" return d\n",
"\n",
"def build_count_excpt(t): \n",
" d = {} \n",
" for s in t:\n",
" try: \n",
" d[s] += 1\n",
" except:\n",
" d[s] = 1\n",
" return d\n",
"\n",
"import collections\n",
"\n",
"def build_count_counter(t):\n",
" return collections.Counter(t)\n",
"\n",
"def build_count_defaultdict(t):\n",
" d = collections.defaultdict(int)\n",
" for k in s:\n",
" d[k] += 1\n",
" return d\n",
"\n",
"s = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique at velit in varius. Cras ut ultricies orci. Fusce vel consequat ante, vitae luctus tortor. Sed condimentum faucibus enim, sit amet pulvinar ligula feugiat ac. Sed interdum id risus id rhoncus. Nullam nisi justo, ultrices eu est nec, hendrerit maximus lorem. Nam urna eros, accumsan nec magna eu, elementum semper diam. Nulla tempus, nibh id elementum dapibus, ex diam lacinia est, sit amet suscipit nulla nibh eu sapien. Aliquam orci enim, malesuada in facilisis vitae, pharetra sit amet mi. Pellentesque mi tortor, sagittis quis odio quis, fermentum faucibus ex. Aenean sagittis nisl orci. Maecenas tristique velit sed leo facilisis porttitor. \"\n",
"s = s*10000\n",
"len(s)\n",
"print(f\"len(s) = {len(s)}, nbkeys {len(set(s))} base, count, count_count, except, colection.counter\")\n",
"%timeit build_count_base(s)\n",
"%timeit build_count_set(s)\n",
"%timeit build_count_count(s)\n",
"%timeit build_count_excpt(s)\n",
"%timeit build_count_counter(s)\n",
"%timeit build_count_defaultdict(s)\n",
"\n",
"print(\"with split\")\n",
"s2 = s.split()\n",
"print(f\"len(s) = {len(s2)}, nbkeys {len(set(s2))} base, count, count_count, except, colection.counter\")\n",
"%timeit build_count_base(s2)\n",
"%timeit build_count_set(s2)\n",
"%timeit build_count_count(s2)\n",
"%timeit build_count_excpt(s2)\n",
"%timeit build_count_counter(s2)\n",
"%timeit build_count_defaultdict(s2)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -1158,18 +1250,6 @@
"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.7.2+"
}
},
"nbformat": 4,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment