Merge pull request #57 from donnemartin/develop

Polish unique chars challenge and solution
This commit is contained in:
Donne Martin 2016-06-12 23:19:38 -04:00 committed by GitHub
commit 2dd8f4ac55
3 changed files with 29 additions and 14 deletions

View File

@ -4,6 +4,7 @@ from nose.tools import assert_equal
class TestUniqueChars(object): class TestUniqueChars(object):
def test_unique_chars(self, func): def test_unique_chars(self, func):
assert_equal(func(None), False)
assert_equal(func(''), True) assert_equal(func(''), True)
assert_equal(func('foo'), False) assert_equal(func('foo'), False)
assert_equal(func('bar'), True) assert_equal(func('bar'), True)

View File

@ -34,12 +34,14 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can you assume the string is ASCII?\n", "* Can we 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", "* Can we assume this is case sensitive?\n",
" * Yes\n", " * Yes\n",
"* Can you use additional data structures?\n", "* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -49,6 +51,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> False\n",
"* '' -> True\n", "* '' -> True\n",
"* 'foo' -> False\n", "* 'foo' -> False\n",
"* 'bar' -> True" "* 'bar' -> True"
@ -112,6 +115,7 @@
"class TestUniqueChars(object):\n", "class TestUniqueChars(object):\n",
"\n", "\n",
" def test_unique_chars(self, func):\n", " def test_unique_chars(self, func):\n",
" assert_equal(func(None), False)\n",
" assert_equal(func(''), True)\n", " assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n", " assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n", " assert_equal(func('bar'), True)\n",
@ -146,21 +150,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -37,12 +37,14 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can you assume the string is ASCII?\n", "* Can we 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", "* Can we assume this is case sensitive?\n",
" * Yes\n", " * Yes\n",
"* Can you use additional data structures? \n", "* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -52,6 +54,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* None -> False\n",
"* '' -> True\n", "* '' -> True\n",
"* 'foo' -> False\n", "* 'foo' -> False\n",
"* 'bar' -> True" "* 'bar' -> True"
@ -91,6 +94,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "def unique_chars(string):\n",
" if string is None:\n",
" return False\n",
" return len(set(string)) == len(string)" " return len(set(string)) == len(string)"
] ]
}, },
@ -134,6 +139,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_hash(string):\n", "def unique_chars_hash(string):\n",
" if string is None:\n",
" return False\n",
" chars_set = set()\n", " chars_set = set()\n",
" for char in string:\n", " for char in string:\n",
" if char in chars_set:\n", " if char in chars_set:\n",
@ -178,6 +185,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_inplace(string):\n", "def unique_chars_inplace(string):\n",
" if string is None:\n",
" return False\n",
" for char in string:\n", " for char in string:\n",
" if string.count(char) > 1:\n", " if string.count(char) > 1:\n",
" return False\n", " return False\n",
@ -214,6 +223,7 @@
"class TestUniqueChars(object):\n", "class TestUniqueChars(object):\n",
"\n", "\n",
" def test_unique_chars(self, func):\n", " def test_unique_chars(self, func):\n",
" assert_equal(func(None), False)\n",
" assert_equal(func(''), True)\n", " assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n", " assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n", " assert_equal(func('bar'), True)\n",
@ -260,21 +270,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,