mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
249 lines
6.3 KiB
Python
249 lines
6.3 KiB
Python
{
|
|
"cells": [
|
|
{
|
|
"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://github.com/donnemartin/interactive-coding-challenges).</i></small>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Solution 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: Compare Sorted Strings](#Algorithm:-Compare-Sorted-Strings)\n",
|
|
"* [Code: Compare Sorted Strings](#Code:-Compare-Sorted-Strings)\n",
|
|
"* [Algorithm: Hashmap Lookup](#Algorithm:-Hash-Map-Lookup)\n",
|
|
"* [Code: Hashmap Lookup](#Code:-Hash-Map-Lookup)\n",
|
|
"* [Unit Test](#Unit-Test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Constraints\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: Compare Sorted Strings\n",
|
|
"\n",
|
|
"Permutations contain the same strings but in different orders. This approach could be slow for large strings due to sorting.\n",
|
|
"\n",
|
|
"* Sort both strings\n",
|
|
"* If both sorted strings are equal\n",
|
|
" * return True\n",
|
|
"* Else\n",
|
|
" * return False\n",
|
|
"\n",
|
|
"Complexity:\n",
|
|
"* Time: O(n log n) from the sort, in general\n",
|
|
"* Space: O(n)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code: Compare Sorted Strings"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def permutations(str1, str2):\n",
|
|
" return sorted(str1) == sorted(str2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm: Hash Map Lookup\n",
|
|
"\n",
|
|
"We'll keep a hash map (dict) to keep track of characters we encounter. \n",
|
|
"\n",
|
|
"Steps:\n",
|
|
"* Scan each character\n",
|
|
"* For each character in each string:\n",
|
|
" * If the character does not exist in a hash map, add the character to a hash map\n",
|
|
" * Else, increment the character's count\n",
|
|
"* If the hash maps for each string are equal\n",
|
|
" * Return True\n",
|
|
"* Else\n",
|
|
" * Return False\n",
|
|
"\n",
|
|
"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, 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",
|
|
"* Space: O(n)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code: Hash Map Lookup"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from collections import defaultdict\n",
|
|
"\n",
|
|
"\n",
|
|
"def permutations_alt(str1, str2):\n",
|
|
" if len(str1) != len(str2):\n",
|
|
" return False\n",
|
|
" unique_counts2 = defaultdict(int)\n",
|
|
" unique_counts2 = defaultdict(int)\n",
|
|
" for char in str1:\n",
|
|
" unique_counts2[char] += 1\n",
|
|
" for char in str2:\n",
|
|
" unique_counts2[char] += 1\n",
|
|
" return unique_counts1 == unique_counts2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Overwriting test_permutation_solution.py\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%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",
|
|
"\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",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Success: test_permutation\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"run -i test_permutation_solution.py"
|
|
]
|
|
}
|
|
],
|
|
"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.4.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|