mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 07:58:02 +00:00
Update str diff challenge (#242)
Update constraints, test cases, and dictionary solution. Fix inconsistencies between challenge and solution notebooks.
This commit is contained in:
@@ -34,13 +34,13 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\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",
|
"* Can we assume the strings are ASCII?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Is case important?\n",
|
"* Is case important?\n",
|
||||||
" * The strings are lower case\n",
|
" * The strings are lower case\n",
|
||||||
"* Can we assume the inputs are valid?\n",
|
"* Can we assume the inputs are valid?\n",
|
||||||
" * No, check for None\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",
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
@@ -52,6 +52,8 @@
|
|||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None input -> TypeError\n",
|
"* None input -> TypeError\n",
|
||||||
|
"* 'ab', 'aab' -> 'a'\n",
|
||||||
|
"* 'aab', 'ab' -> 'a'\n",
|
||||||
"* 'abcd', 'abcde' -> 'e'\n",
|
"* 'abcd', 'abcde' -> 'e'\n",
|
||||||
"* 'aaabbcdd', 'abdbacade' -> 'e'"
|
"* 'aaabbcdd', 'abdbacade' -> 'e'"
|
||||||
]
|
]
|
||||||
@@ -82,7 +84,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"class Solution(object):\n",
|
"class Solution(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def find_diff(self, s, t):\n",
|
" def find_diff(self, str1, str2):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
@@ -117,9 +119,15 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_find_diff(self):\n",
|
" def test_find_diff(self):\n",
|
||||||
" solution = Solution()\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('abcd', 'abcde'), 'e')\n",
|
||||||
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), '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",
|
" print('Success: test_find_diff')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
" * The strings are lower case\n",
|
" * The strings are lower case\n",
|
||||||
"* Can we assume the inputs are valid?\n",
|
"* Can we assume the inputs are valid?\n",
|
||||||
" * No, check for None\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",
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
@@ -50,6 +51,8 @@
|
|||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None input -> TypeError\n",
|
"* None input -> TypeError\n",
|
||||||
|
"* 'ab', 'aab' -> 'a'\n",
|
||||||
|
"* 'aab', 'ab' -> 'a'\n",
|
||||||
"* 'abcd', 'abcde' -> 'e'\n",
|
"* 'abcd', 'abcde' -> 'e'\n",
|
||||||
"* 'aaabbcdd', 'abdbacade' -> 'e'"
|
"* 'aaabbcdd', 'abdbacade' -> 'e'"
|
||||||
]
|
]
|
||||||
@@ -65,6 +68,7 @@
|
|||||||
"* Keep a dictionary of seen values in s\n",
|
"* Keep a dictionary of seen values in s\n",
|
||||||
"* Loop through t, decrementing the seen values\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",
|
" * 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",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(m+n), where m and n are the lengths of s, t\n",
|
"* Time: O(m+n), where m and n are the lengths of s, t\n",
|
||||||
@@ -112,7 +116,8 @@
|
|||||||
" return char\n",
|
" return char\n",
|
||||||
" if seen[char] < 0:\n",
|
" if seen[char] < 0:\n",
|
||||||
" return char\n",
|
" return char\n",
|
||||||
" return None\n",
|
" for char, count in seen.items():\n",
|
||||||
|
" return char\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def find_diff_xor(self, str1, str2):\n",
|
" def find_diff_xor(self, str1, str2):\n",
|
||||||
" if str1 is None or str2 is None:\n",
|
" if str1 is None or str2 is None:\n",
|
||||||
@@ -157,8 +162,14 @@
|
|||||||
" def test_find_diff(self):\n",
|
" def test_find_diff(self):\n",
|
||||||
" solution = Solution()\n",
|
" solution = Solution()\n",
|
||||||
" assert_raises(TypeError, solution.find_diff, 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('abcd', 'abcde'), 'e')\n",
|
||||||
" assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), '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",
|
" print('Success: test_find_diff')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -6,8 +6,14 @@ class TestFindDiff(object):
|
|||||||
def test_find_diff(self):
|
def test_find_diff(self):
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
assert_raises(TypeError, solution.find_diff, None)
|
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('abcd', 'abcde'), 'e')
|
||||||
assert_equal(solution.find_diff('aaabbcdd', 'abdbacade'), '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')
|
print('Success: test_find_diff')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user