Update str diff challenge (#242)

Update constraints, test cases, and dictionary solution. Fix inconsistencies between challenge and solution notebooks.
This commit is contained in:
Donne Martin 2018-08-21 23:02:58 -04:00 committed by GitHub
parent 95d0d26a2c
commit 8e035f1dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -34,13 +34,13 @@
"source": [
"## Constraints\n",
"\n",
"* Assume two strings str1, str2, where str2 contains the same set of characters in str1 with one additional character.\n",
"* Can we assume the strings are ASCII?\n",
" * Yes\n",
"* Is case important?\n",
" * The strings are lower case\n",
"* Can we assume the inputs are valid?\n",
" * No, check for None\n",
" * Otherwise, assume there is only a single different char between the two strings\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
@ -52,6 +52,8 @@
"## Test Cases\n",
"\n",
"* None input -> TypeError\n",
"* 'ab', 'aab' -> 'a'\n",
"* 'aab', 'ab' -> 'a'\n",
"* 'abcd', 'abcde' -> 'e'\n",
"* 'aaabbcdd', 'abdbacade' -> 'e'"
]
@ -82,7 +84,7 @@
"source": [
"class Solution(object):\n",
"\n",
" def find_diff(self, s, t):\n",
" def find_diff(self, str1, str2):\n",
" # TODO: Implement me\n",
" pass"
]
@ -117,9 +119,15 @@
"\n",
" def test_find_diff(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.find_diff, None, None)\n",
" assert_raises(TypeError, solution.find_diff, None)\n",
" assert_equal(solution.find_diff('ab', 'aab'), 'a')\n",
" assert_equal(solution.find_diff('aab', 'ab'), 'a')\n",
" assert_equal(solution.find_diff('abcd', 'abcde'), 'e')\n",
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')\n",
" assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')\n",
" assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')\n",
" assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')\n",
" assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')\n",
" print('Success: test_find_diff')\n",
"\n",
"\n",

View File

@ -39,6 +39,7 @@
" * The strings are lower case\n",
"* Can we assume the inputs are valid?\n",
" * No, check for None\n",
" * Otherwise, assume there is only a single different char between the two strings\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
@ -50,6 +51,8 @@
"## Test Cases\n",
"\n",
"* None input -> TypeError\n",
"* 'ab', 'aab' -> 'a'\n",
"* 'aab', 'ab' -> 'a'\n",
"* 'abcd', 'abcde' -> 'e'\n",
"* 'aaabbcdd', 'abdbacade' -> 'e'"
]
@ -65,6 +68,7 @@
"* Keep a dictionary of seen values in s\n",
"* Loop through t, decrementing the seen values\n",
" * If the char is not there or if the decrement results in a negative value, return the char\n",
"* Return the differing char from the dictionary\n",
"\n",
"Complexity:\n",
"* Time: O(m+n), where m and n are the lengths of s, t\n",
@ -112,7 +116,8 @@
" return char\n",
" if seen[char] < 0:\n",
" return char\n",
" return None\n",
" for char, count in seen.items():\n",
" return char\n",
"\n",
" def find_diff_xor(self, str1, str2):\n",
" if str1 is None or str2 is None:\n",
@ -157,8 +162,14 @@
" def test_find_diff(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.find_diff, None)\n",
" assert_equal(solution.find_diff('ab', 'aab'), 'a')\n",
" assert_equal(solution.find_diff('aab', 'ab'), 'a')\n",
" assert_equal(solution.find_diff('abcd', 'abcde'), 'e')\n",
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')\n",
" assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')\n",
" assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')\n",
" assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')\n",
" assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')\n",
" print('Success: test_find_diff')\n",
"\n",
"\n",

View File

@ -6,8 +6,14 @@ class TestFindDiff(object):
def test_find_diff(self):
solution = Solution()
assert_raises(TypeError, solution.find_diff, None)
assert_equal(solution.find_diff('ab', 'aab'), 'a')
assert_equal(solution.find_diff('aab', 'ab'), 'a')
assert_equal(solution.find_diff('abcd', 'abcde'), 'e')
assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), 'e')
assert_equal(solution.find_diff_xor('ab', 'aab'), 'a')
assert_equal(solution.find_diff_xor('aab', 'ab'), 'a')
assert_equal(solution.find_diff_xor('abcd', 'abcde'), 'e')
assert_equal(solution.find_diff_xor('aaabbcdd', 'abdbacade'), 'e')
print('Success: test_find_diff')