diff --git a/arrays_strings/permutation/permutation_challenge.ipynb b/arrays_strings/permutation/permutation_challenge.ipynb
new file mode 100644
index 0000000..9b6d371
--- /dev/null
+++ b/arrays_strings/permutation/permutation_challenge.ipynb
@@ -0,0 +1,171 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Challenge Notebook"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Problem: Determine if a string is a permutation of another string\n",
+ "\n",
+ "* [Constraints](#Constraints)\n",
+ "* [Test Cases](#Test-Cases)\n",
+ "* [Algorithm](#Algorithm)\n",
+ "* [Code](#Code)\n",
+ "* [Unit Test](#Unit-Test)\n",
+ "* [Solution Notebook](#Solution-Notebook)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Constraints\n",
+ "\n",
+ "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
+ "\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",
+ " * Yes\n",
+ "* Is this case sensitive? 'Nib', 'bin' is not a match?\n",
+ " * Yes"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Test Cases\n",
+ "\n",
+ "* One or more empty strings -> False\n",
+ "* 'Nib', 'bin' -> False\n",
+ "* 'act', 'cat' -> True\n",
+ "* 'a ct', 'ca t' -> True"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Algorithm\n",
+ "\n",
+ "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/arrays_strings/permutation/permutation_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "def permutations(str1, str2):\n",
+ " # TODO: Implement me\n",
+ " pass"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Unit Test"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\n",
+ "\n",
+ "**The following unit test is expected to fail until you solve the challenge.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "# %load 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": "markdown",
+ "metadata": {},
+ "source": [
+ "## Solution Notebook\n",
+ "\n",
+ "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/arrays_strings/permutation/permutation_solution.ipynb) for a discussion on algorithms and code solutions."
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.10"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/arrays_strings/permutation/permutation_solution.ipynb b/arrays_strings/permutation/permutation_solution.ipynb
index 57a25f3..e60608b 100644
--- a/arrays_strings/permutation/permutation_solution.ipynb
+++ b/arrays_strings/permutation/permutation_solution.ipynb
@@ -4,7 +4,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes)."
+ "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)."
+ ]
+ },
+ {
+ "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"
]
}
],
diff --git a/arrays_strings/permutation/test_permutation_solution.py b/arrays_strings/permutation/test_permutation_solution.py
new file mode 100644
index 0000000..430fa81
--- /dev/null
+++ b/arrays_strings/permutation/test_permutation_solution.py
@@ -0,0 +1,24 @@
+from nose.tools import assert_equal
+
+
+class TestPermutation(object):
+
+ def test_permutation(self, func):
+ assert_equal(func('', 'foo'), False)
+ assert_equal(func('Nib', 'bin'), False)
+ assert_equal(func('act', 'cat'), True)
+ assert_equal(func('a ct', 'ca t'), True)
+ print('Success: test_permutation')
+
+def main():
+ test = TestPermutation()
+ test.test_permutation(permutations)
+ try:
+ test.test_permutation(permutations_alt)
+ except NameError:
+ # Alternate solutions are only defined
+ # in the solutions file
+ pass
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file