mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Moved unit test to test case section. Updated unit test to take a function parameter on which to run the test.
This commit is contained in:
parent
32ae488a45
commit
83e3eedb52
|
@ -45,6 +45,27 @@
|
||||||
"* 'bar' -> True"
|
"* 'bar' -> True"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"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",
|
||||||
|
"\n",
|
||||||
|
"def run_tests(func):\n",
|
||||||
|
" test = Test()\n",
|
||||||
|
" test.test_unique_chars(func)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -79,30 +100,9 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def unique_chars(string):\n",
|
"def unique_chars(string):\n",
|
||||||
" return len(set(string)) == len(string)"
|
" return len(set(string)) == len(string)\n",
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from nose.tools import assert_equal\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"class Test(object):\n",
|
"run_tests(unique_chars)"
|
||||||
" def test_unique_chars(self):\n",
|
|
||||||
" assert_equal(unique_chars(''), True)\n",
|
|
||||||
" assert_equal(unique_chars('foo'), False)\n",
|
|
||||||
" assert_equal(unique_chars('bar'), True) \n",
|
|
||||||
"\n",
|
|
||||||
"def run_tests():\n",
|
|
||||||
" test = Test()\n",
|
|
||||||
" test.test_unique_chars()\n",
|
|
||||||
"\n",
|
|
||||||
"run_tests()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -144,7 +144,7 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def unique_chars_alt(string):\n",
|
"def unique_chars_hash(string):\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",
|
||||||
|
@ -153,7 +153,7 @@
|
||||||
" chars_set.add(char)\n",
|
" chars_set.add(char)\n",
|
||||||
" return True\n",
|
" return True\n",
|
||||||
"\n",
|
"\n",
|
||||||
"run_tests()"
|
"run_tests(unique_chars_hash)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -190,13 +190,13 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def unique_chars_alt(string):\n",
|
"def unique_chars_inplace(string):\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",
|
||||||
" return True\n",
|
" return True\n",
|
||||||
"\n",
|
"\n",
|
||||||
"run_tests()"
|
"run_tests(unique_chars_inplace)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user