mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-03-04 14:48:45 +00:00
Added permutation challenge.
This commit is contained in:
@@ -4,7 +4,14 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Solution Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -28,9 +35,9 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"\n",
|
||||
"* Can I assume the string is ASCII?\n",
|
||||
"* Can we assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language\n",
|
||||
"* Is whitespace important?\n",
|
||||
@@ -110,7 +117,7 @@
|
||||
"Notes:\n",
|
||||
"* Since the characters are in ASCII, we could potentially use an array of size 128 (or 256 for extended ASCII), where each array index is equivalent to an ASCII value\n",
|
||||
"* Instead of using two hash maps, you could use one hash map and increment character values based on the first string and decrement based on the second string\n",
|
||||
"* You can short circuit if the lengths of each string are not equal, len() in Python is generally O(1)\n",
|
||||
"* You can short circuit if the lengths of each string are not equal, although len() in Python is generally O(1) unlike other languages like C where getting the length of a string is O(n)\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
@@ -134,6 +141,7 @@
|
||||
"source": [
|
||||
"from collections import defaultdict\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def unique_counts(string):\n",
|
||||
" dict_chars = defaultdict(int)\n",
|
||||
" for char in string:\n",
|
||||
@@ -156,15 +164,51 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting test_permutation_solution.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||
"%%writefile test_permutation_solution.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestPermutation(object):\n",
|
||||
" \n",
|
||||
" def test_permutation(self, func):\n",
|
||||
" assert_equal(func('', 'foo'), False)\n",
|
||||
" assert_equal(func('Nib', 'bin'), False)\n",
|
||||
" assert_equal(func('act', 'cat'), True)\n",
|
||||
" assert_equal(func('a ct', 'ca t'), True)\n",
|
||||
" print('Success: test_permutation')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestPermutation()\n",
|
||||
" test.test_permutation(permutations)\n",
|
||||
" try:\n",
|
||||
" test.test_permutation(permutations_alt)\n",
|
||||
" except NameError:\n",
|
||||
" # Alternate solutions are only defined\n",
|
||||
" # in the solutions file\n",
|
||||
" pass\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -179,20 +223,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" def test_permutation(self, func):\n",
|
||||
" assert_equal(func('', 'foo'), False)\n",
|
||||
" assert_equal(func('Nib', 'bin'), False)\n",
|
||||
" assert_equal(func('act', 'cat'), True)\n",
|
||||
" assert_equal(func('a ct', 'ca t'), True)\n",
|
||||
" print('Success: test_permutation')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_permutation(permutations)\n",
|
||||
" test.test_permutation(permutations_alt)"
|
||||
"run -i test_permutation_solution.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user