Added unique chars challenge.

This commit is contained in:
Donne Martin 2015-07-03 19:57:24 -04:00
parent 73ec41e427
commit 8ac0bd3f69
3 changed files with 260 additions and 21 deletions

View File

@ -0,0 +1,24 @@
from nose.tools import assert_equal
class TestUniqueChars(object):
def test_unique_chars(self, func):
assert_equal(func(''), True)
assert_equal(func('foo'), False)
assert_equal(func('bar'), True)
print('Success: test_unique_chars')
def main():
test = TestUniqueChars()
test.test_unique_chars(unique_chars)
try:
test.test_unique_chars(unique_chars_hash)
test.test_unique_chars(unique_chars_inplace)
except NameError:
# Alternate solutions are only defined
# in the solutions file
pass
if __name__ == '__main__':
main()

View File

@ -0,0 +1,183 @@
{
"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/coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"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](#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 you assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
"* Can we assume this is case sensitive?\n",
" * Yes\n",
"* Can you use additional data structures?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"*It is important to identify and run through general and edge cases by hand.*\n",
"\n",
"* '' -> True\n",
"* 'foo' -> False\n",
"* 'bar' -> True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [solution notebook](#). 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": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def unique_chars(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": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_unique_chars\n"
]
}
],
"source": [
"# %load test_unique_chars.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestUniqueChars(object):\n",
"\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",
"def main():\n",
" test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n",
" try:\n",
" test.test_unique_chars(unique_chars_hash)\n",
" test.test_unique_chars(unique_chars_inplace)\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](#) 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
}

View File

@ -4,7 +4,14 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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"
] ]
}, },
{ {
@ -30,11 +37,13 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\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", "\n",
"* Can I assume the string is ASCII?\n", "* Can you assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
"* Can we assume this is case sensitive?\n",
" * Yes\n",
"* Can you use additional data structures? \n", "* Can you use additional data structures? \n",
" * Yes" " * Yes"
] ]
@ -185,15 +194,51 @@
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "code",
"metadata": {}, "execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_unique_chars.py\n"
]
}
],
"source": [ "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_unique_chars.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestUniqueChars(object):\n",
"\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",
"def main():\n",
" test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n",
" try:\n",
" test.test_unique_chars(unique_chars_hash)\n",
" test.test_unique_chars(unique_chars_inplace)\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", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -209,20 +254,7 @@
} }
], ],
"source": [ "source": [
"from nose.tools import assert_equal\n", "%run -i test_unique_chars.py"
"\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)"
] ]
} }
], ],