Initial commit for new challenge: Non repeating char

This commit is contained in:
betterin30days 2017-04-12 22:56:40 -04:00
parent ffd4798f23
commit 31619bcc8e
4 changed files with 387 additions and 0 deletions

View File

View File

@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Lee Costello](https://github.com/betterin30days). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the first non-repeated character in a string. A repeating char is when the same character appears more than once in a row.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
"* Is this case sensitive?\n",
" * Yes\n",
"* Is whitespace important? \n",
" * Yes\n",
"* Do we need to validate input?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* 'aabccd' -> 'b'\n",
"* 'abbccd' -> 'a'\n",
"* 'aabbadd' -> 'a'\n",
"* 'Aabccd' -> 'A'\n",
"* 'aabbccdd' -> None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/non_repeated/non_repeat_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class NonRepeat(object):\n",
"\n",
" def non_repeated_char(self, string):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load test_non_repeat.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestNonRepeat(object):\n",
"\n",
" def test_non_repeat(self, func):\n",
" assert_equal(func('aabccd'), 'b')\n",
" assert_equal(func('abbccd'), 'a')\n",
" assert_equal(func('aabbadd'), 'a')\n",
" assert_equal(func('aaab'), 'b')\n",
" assert_equal(func('Aabccd'), 'A')\n",
" assert_equal(func('aabbccdd'), None)\n",
" print('Success: test_non_repeat')\n",
"\n",
"def main():\n",
" test = TestNonRepeat()\n",
" nonrepeat = NonRepeat()\n",
" test.test_non_repeat(nonrepeat.non_repeated_char)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/non_repeated/non_repeat_solution.ipynb) for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}

View File

@ -0,0 +1,197 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Lee Costello](https://github.com/betterin30days). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the first non-repeated character in a string. A repeating char is when the same character appears more than once in a row.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
"* Is this case sensitive?\n",
" * Yes\n",
"* Is whitespace important?\n",
" * Yes\n",
"* Do we need to validate input?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* 'aabccd' -> 'b'\n",
"* 'abbccd' -> 'a'\n",
"* 'aabbadd' -> 'a'\n",
"* 'Aabccd' -> 'A'\n",
"* 'aabbccdd' -> None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We only need to know if the current character is the same as the previous character, and if the last two characters were repeats.\n",
"* Iterate through the string once:\n",
" * if char == last_char: rep = True\n",
" * elif rep is True: rep = False\n",
" * elif last_char is not None: return last_char\n",
"* Perform a final check to account for the last character\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class NonRepeat(object):\n",
"\n",
" def non_repeated_char(self, string):\n",
" assert isinstance(string, str)\n",
" last_char = None\n",
" is_repeating = False\n",
" for char in string:\n",
" if char == last_char:\n",
" is_repeating = True\n",
" elif is_repeating:\n",
" is_repeating = False\n",
" elif last_char is not None:\n",
" return last_char\n",
" last_char = char\n",
" if is_repeating is False:\n",
" return last_char"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_non_repeat.py\n"
]
}
],
"source": [
"%%writefile test_non_repeat.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestNonRepeat(object):\n",
"\n",
" def test_non_repeat(self, func):\n",
" assert_equal(func('aabccd'), 'b')\n",
" assert_equal(func('abbccd'), 'a')\n",
" assert_equal(func('aabbadd'), 'a')\n",
" assert_equal(func('aaab'), 'b')\n",
" assert_equal(func('Aabccd'), 'A')\n",
" assert_equal(func('aabbccdd'), None)\n",
" print('Success: test_non_repeat')\n",
"\n",
"def main():\n",
" test = TestNonRepeat()\n",
" nonrepeat = NonRepeat()\n",
" test.test_non_repeat(nonrepeat.non_repeated_char)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_non_repeat\n"
]
}
],
"source": [
"%run -i test_non_repeat.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}

View File

@ -0,0 +1,22 @@
from nose.tools import assert_equal
class TestNonRepeat(object):
def test_non_repeat(self, func):
assert_equal(func('aabccd'), 'b')
assert_equal(func('abbccd'), 'a')
assert_equal(func('aabbadd'), 'a')
assert_equal(func('aaab'), 'b')
assert_equal(func('Aabccd'), 'A')
assert_equal(func('aabbccdd'), None)
print('Success: test_non_repeat')
def main():
test = TestNonRepeat()
nonrepeat = NonRepeat()
test.test_non_repeat(nonrepeat.non_repeated_char)
if __name__ == '__main__':
main()