mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Add solution to challenge reverse words
This commit is contained in:
parent
f771a8b173
commit
15f8ff88f0
|
@ -92,16 +92,6 @@
|
|||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"deletable": true,
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
|
@ -110,8 +100,7 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"# %load test_reverse_string.py\n",
|
||||
"# %load test_reverse_words.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -140,9 +129,9 @@
|
|||
"metadata": {
|
||||
"celltoolbar": "Edit Metadata",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "Python 2",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
"name": "python2"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
|
@ -154,7 +143,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.9"
|
||||
"version": "2.7.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
37
arrays_strings/reverse_words/reverse_words_challenge.py
Normal file
37
arrays_strings/reverse_words/reverse_words_challenge.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
class ReverseWords:
|
||||
|
||||
def reverse(self, string):
|
||||
if string is None:
|
||||
return None
|
||||
if string == ['']:
|
||||
return ['']
|
||||
buff_w = [] # buffer for substrings
|
||||
substring = [] # substring
|
||||
new_string = [] # new string with reverses words
|
||||
for i in range(len(string)):
|
||||
if(string[i] == ' '):
|
||||
for h in range(len(buff_w) - 1, -1, -1):
|
||||
substring.append(buff_w[h])
|
||||
for j in range(len(substring)):
|
||||
new_string.append(substring[j])
|
||||
substring = []
|
||||
buff_w = []
|
||||
else:
|
||||
buff_w.append(string[i])
|
||||
if(i == (len(string) - 1)):
|
||||
new_string.append(' ')
|
||||
for h in range(len(buff_w) - 1, -1, -1):
|
||||
substring.append(buff_w[h])
|
||||
for j in range(len(substring)):
|
||||
new_string.append(substring[j])
|
||||
return new_string
|
||||
|
||||
|
||||
def main():
|
||||
R = ReverseWords()
|
||||
result = R.reverse('foo bar')
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,187 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This notebook was prepared by [eamanu](http://github.com/eamanu). 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: Implement a function to reverse words within a string. \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",
|
||||
"* Since we need to do this in-place, it seems we cannot use the slice operator or the reversed function?\n",
|
||||
" * Correct\n",
|
||||
"* Since Python string are immutable, can we use a list of characters instead?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* [''] -> ['']\n",
|
||||
"* ['f', 'o', 'o', ' ', 'b', 'a', 'r'] -> ['o', 'o', 'f', ' ', 'r', 'a', 'b']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"* First if String is None\n",
|
||||
" * Return None\n",
|
||||
"* If String is ['']\n",
|
||||
" * Return ['']\n",
|
||||
"* Go throught the string and we append in a buffer\n",
|
||||
"* IF the actual char is ' ' we reverse the buffer and put into a new string (the solution)\n",
|
||||
"* Repeat until the finished.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class ReverseWords:\n",
|
||||
"\n",
|
||||
" def reverse(self, string):\n",
|
||||
" if string is None:\n",
|
||||
" return None\n",
|
||||
" if string == ['']:\n",
|
||||
" return ['']\n",
|
||||
" buff_w = [] # buffer for substrings\n",
|
||||
" substring = [] # substring\n",
|
||||
" new_string = [] # new string with reverses words\n",
|
||||
" for i in range(len(string)):\n",
|
||||
" if(string[i] == ' '):\n",
|
||||
" for h in range(len(buff_w) - 1, -1, -1):\n",
|
||||
" substring.append(buff_w[h])\n",
|
||||
" for j in range(len(substring)):\n",
|
||||
" new_string.append(substring[j])\n",
|
||||
" substring = []\n",
|
||||
" buff_w = []\n",
|
||||
" else:\n",
|
||||
" buff_w.append(string[i])\n",
|
||||
" if(i == (len(string) - 1)):\n",
|
||||
" new_string.append(' ')\n",
|
||||
" for h in range(len(buff_w) - 1, -1, -1):\n",
|
||||
" substring.append(buff_w[h])\n",
|
||||
" for j in range(len(substring)):\n",
|
||||
" new_string.append(substring[j])\n",
|
||||
" return new_string\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_reverse\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# %load test_reverse_words.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestReverseWords(object):\n",
|
||||
"\n",
|
||||
" def test_reverse(self, func):\n",
|
||||
" assert_equal(func(None), None)\n",
|
||||
" assert_equal(func(['']), [''])\n",
|
||||
" assert_equal(func(\n",
|
||||
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
|
||||
" ['o', 'o', 'f', ' ', 'r', 'a', 'b'])\n",
|
||||
" print('Success: test_reverse')\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"def main():\n",
|
||||
" test = TestReverseWords()\n",
|
||||
" reverse_string = ReverseWords()\n",
|
||||
" test.test_reverse(reverse_string.reverse)\n",
|
||||
"\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Edit 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": 2
|
||||
}
|
22
arrays_strings/reverse_words/test_reverse_words.py
Normal file
22
arrays_strings/reverse_words/test_reverse_words.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from nose.tools import assert_equal
|
||||
|
||||
|
||||
class TestReverseWords(object):
|
||||
|
||||
def test_reverse(self, func):
|
||||
assert_equal(func(None), None)
|
||||
assert_equal(func(['']), [''])
|
||||
assert_equal(func(
|
||||
['f', 'o', 'o', ' ', 'b', 'a', 'r']),
|
||||
['o', 'o', 'f', ' ', 'r', 'a', 'b'])
|
||||
print('Success: test_reverse')
|
||||
|
||||
|
||||
def main():
|
||||
test = TestReverseWords()
|
||||
reverse_string = ReverseWords()
|
||||
test.test_reverse(reverse_string.reverse)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user