mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
251 lines
6.5 KiB
Plaintext
251 lines
6.5 KiB
Plaintext
{
|
|
"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://bit.ly/code-notes).</i></small>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem: Implement an algorithm to determine if a string has all unique characters\n",
|
|
"\n",
|
|
"* [Constraints](#Constraints)\n",
|
|
"* [Test Cases](#Test-Cases)\n",
|
|
"* [Algorithm 1: Sets and Length Comparison](#Algorithm-1:-Sets-and-Length-Comparison)\n",
|
|
"* [Code: Sets and Length Comparison](#Code:-Sets-and-Length-Comparison)\n",
|
|
"* [Algorithm 2: Hash Map Lookup](#Algorithm-2:-Hash-Map-Lookup)\n",
|
|
"* [Code: Hash Map Lookup](#Code:-Hash-Map-Lookup)\n",
|
|
"* [Algorithm 3: In-Place](#Algorithm-3:-In-Place)\n",
|
|
"* [Code: In-Place](#Code:-In-Place)\n",
|
|
"* [Unit Test](#Unit-Test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"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",
|
|
"\n",
|
|
"* Can I assume the string is ASCII?\n",
|
|
" * Yes\n",
|
|
" * Note: Unicode strings could require special handling depending on your language\n",
|
|
"* Can you use additional data structures? \n",
|
|
" * Yes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test Cases\n",
|
|
"\n",
|
|
"* '' -> True\n",
|
|
"* 'foo' -> False\n",
|
|
"* 'bar' -> True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm 1: Sets and Length Comparison\n",
|
|
"\n",
|
|
"A set is an unordered collection of unique elements. \n",
|
|
"\n",
|
|
"* If the length of the set(string) equals the length of the string\n",
|
|
" * Return True\n",
|
|
"* Else\n",
|
|
" * Return False\n",
|
|
" \n",
|
|
"Complexity:\n",
|
|
"* Time: O(n)\n",
|
|
"* Space: Additional O(m), where m is the number of unique characters in the set"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code: Sets and Length Comparison"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def unique_chars(string):\n",
|
|
" return len(set(string)) == len(string)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm 2: Hash Map Lookup\n",
|
|
"\n",
|
|
"We'll keep a hash map (set) to keep track of unique characters we encounter. \n",
|
|
"\n",
|
|
"Steps:\n",
|
|
"* Scan each character\n",
|
|
"* For each character:\n",
|
|
" * If the character does not exist in a hash map, add the character to a hash map\n",
|
|
" * Else, return False\n",
|
|
"* Return True\n",
|
|
"\n",
|
|
"Notes:\n",
|
|
"* We could also use a dictionary, but it seems more logical to use a set as it does not contain duplicate elements\n",
|
|
"* Since the characters are in ASCII, we could potentially use an array of size 128 (or 256 for extended ASCII)\n",
|
|
"\n",
|
|
"Complexity:\n",
|
|
"* Time: O(n)\n",
|
|
"* Space: Additional O(m), where m is the number of unique characters in the hash map"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code: Hash Map Lookup"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def unique_chars_hash(string):\n",
|
|
" chars_set = set()\n",
|
|
" for char in string:\n",
|
|
" if char in chars_set:\n",
|
|
" return False\n",
|
|
" else:\n",
|
|
" chars_set.add(char)\n",
|
|
" return True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm 3: In-Place\n",
|
|
"\n",
|
|
"Assume we cannot use additional data structures, which will eliminate the fast lookup O(1) time provided by our hash map. \n",
|
|
"* Scan each character\n",
|
|
"* For each character:\n",
|
|
" * Scan all [other] characters in the array\n",
|
|
" * Exluding the current character from the scan is rather tricky in Python and results in a non-Pythonic solution\n",
|
|
" * If there is a match, return False\n",
|
|
"* Return True\n",
|
|
"\n",
|
|
"Algorithm Complexity:\n",
|
|
"* Time: O(n^2)\n",
|
|
"* Space: In-place"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code: In-Place"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def unique_chars_inplace(string):\n",
|
|
" for char in string:\n",
|
|
" if string.count(char) > 1:\n",
|
|
" return False\n",
|
|
" return True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"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.*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Success: test_unique_chars\n",
|
|
"Success: test_unique_chars\n",
|
|
"Success: test_unique_chars\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from nose.tools import assert_equal\n",
|
|
"\n",
|
|
"class Test(object):\n",
|
|
" def test_unique_chars(self, func):\n",
|
|
" assert_equal(func(''), True)\n",
|
|
" assert_equal(func('foo'), False)\n",
|
|
" assert_equal(func('bar'), True)\n",
|
|
" print('Success: test_unique_chars')\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" test = Test()\n",
|
|
" test.test_unique_chars(unique_chars)\n",
|
|
" test.test_unique_chars(unique_chars_hash)\n",
|
|
" test.test_unique_chars(unique_chars_inplace)"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|