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):
def test_unique_chars(self, func):
assert_equal(func(None), False)
assert_equal(func(''), True)
assert_equal(func('foo'), False)
assert_equal(func('bar'), True)

View File

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

View File

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