Created
June 27, 2018 22:51
-
-
Save ladyrassilon/d0861c256882abc37f61cd757dd1a99f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### This was the moderately optimised solution I submitted" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(1, 9), (2, 8), (3, 7)]\n", | |
"[(5, 7), (6, 6), (3, 9)]\n", | |
"[(5, 7), (3, 9)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from itertools import combinations\n", | |
"def numberOfPairs(a, k):\n", | |
" perms = combinations(a, 2)\n", | |
" unique_pairs = []\n", | |
" for x, y in perms:\n", | |
" if x + y == k:\n", | |
" if x > y:\n", | |
" valid_pair = (y, x)\n", | |
" else:\n", | |
" valid_pair = (x, y)\n", | |
" if valid_pair not in unique_pairs:\n", | |
" unique_pairs.append(valid_pair)\n", | |
" print(unique_pairs)\n", | |
" return len(unique_pairs)\n", | |
"\n", | |
"a = [1, 2, 3, 6, 7, 8, 9, 1]\n", | |
"target = 10\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,6,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,2,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This timed out so I restructured the code without the combinations method, to try to get a more optimal solution.\n", | |
"### More optimal solution" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(1, 9), (2, 8), (3, 7)]\n", | |
"[(5, 7), (3, 9)]\n", | |
"[(5, 7), (3, 9)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def numberOfPairs(a, k):\n", | |
" unique_pairs = []\n", | |
" for i in a:\n", | |
" for j in a:\n", | |
" if i is not j:\n", | |
" if i + j == k:\n", | |
" if i > j:\n", | |
" valid_pair = (j, i)\n", | |
" else:\n", | |
" valid_pair = (i, j)\n", | |
" if valid_pair not in unique_pairs:\n", | |
" unique_pairs.append(valid_pair)\n", | |
" print(unique_pairs)\n", | |
" return len(unique_pairs)\n", | |
"\n", | |
"a = [1, 2, 3, 6, 7, 8, 9, 1]\n", | |
"target = 10\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,6,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,2,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now this definitely gives the wrong result on the second test (2 rather than 3), but it doesn't complain about that, however it does complain about the third test insisting that it should have 1 result rather than 2.\n", | |
"\n", | |
"The question was slightly unclear about repeats, since it never covers repeated number pairs, but does say repeated numbers are wrong... so [6,6] should be valid for test case 2, but it doesn't complain about that not being in the count, but it does complain about having 2 pairs (which are definitely valid) rather than 1 in the second test case." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### This was the next iteration I was going for which gives the right answers, even if they were not testing right.\n", | |
"\n", | |
"This was my last attempt but I stopped debugging because I wanted to finish the other questions, but the behaviour appeared to be complaining even though it gives the exact output as the previous version (I didn't properly test to exaustion like the above example)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[(1, 9), (2, 8), (3, 7)]\n", | |
"[(5, 7), (6, 6), (3, 9)]\n", | |
"[(5, 7), (3, 9)]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def numberOfPairs(a, k):\n", | |
" unique_pairs = []\n", | |
" for idx, i in enumerate(a):\n", | |
" for idy, j in enumerate(a):\n", | |
" if idx != idy:\n", | |
" if i + j == k:\n", | |
" if i > j:\n", | |
" valid_pair = (j, i)\n", | |
" else:\n", | |
" valid_pair = (i, j)\n", | |
" if valid_pair not in unique_pairs:\n", | |
" unique_pairs.append(valid_pair)\n", | |
" print(unique_pairs)\n", | |
" return len(unique_pairs)\n", | |
"\n", | |
"a = [1, 2, 3, 6, 7, 8, 9, 1]\n", | |
"target = 10\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,6,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)\n", | |
"\n", | |
"a = [7,6,2,3,9,3,5,1]\n", | |
"target = 12\n", | |
"numberOfPairs(a, target)" | |
] | |
} | |
], | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment