mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
new challenge: reverse_words
This commit is contained in:
parent
502c4d77e4
commit
a567898dab
0
arrays_strings/reverse_words/__init__.py
Normal file
0
arrays_strings/reverse_words/__init__.py
Normal file
155
arrays_strings/reverse_words/reverse_words_challenge.ipynb
Normal file
155
arrays_strings/reverse_words/reverse_words_challenge.ipynb
Normal file
|
@ -0,0 +1,155 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small> <i> This notebook was prepared by Marco Guajardo. For license visit [github](https://github.com/donnemartin/interactive-coding-challenges) </i> </small>\n",
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Challenge Notebook\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Problem: Given a string of words, return a string with the words in reverse"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* [Constraits](#Constraint)\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",
|
||||||
|
"* Can we assume the string is ASCII?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Is whitespace important?\n",
|
||||||
|
" * no the whitespace does not change\n",
|
||||||
|
"* Is this case sensitive?\n",
|
||||||
|
" * yes\n",
|
||||||
|
"* What if the string is empty?\n",
|
||||||
|
" * return None\n",
|
||||||
|
"* Is the order of words important?\n",
|
||||||
|
" * yes\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Test Cases\n",
|
||||||
|
"* Empty string -> None\n",
|
||||||
|
"* \"the sun is very hot\" -> \"eht nus si yrev toh\"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Algorithm\n",
|
||||||
|
"* Refer to the [Solution](https://github.com/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_words/reverse_words_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": 21,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def reverse_words (S):\n",
|
||||||
|
" #TODO: implement me\n",
|
||||||
|
" pass "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test\n",
|
||||||
|
"<b> The following unit test is expected to fail until you solve challenge </b>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"class UnitTest (object):\n",
|
||||||
|
" def testReverseWords(self):\n",
|
||||||
|
" assert_equal(func('the sun is hot'), 'eht nus si toh')\n",
|
||||||
|
" assert_equal(func(''), None)\n",
|
||||||
|
" assert_equal(func('123 456 789'), '321 654 987')\n",
|
||||||
|
" assert_equal(func('magic'), 'cigam')\n",
|
||||||
|
" print('Success: reverse_words')\n",
|
||||||
|
" \n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = UnitTest()\n",
|
||||||
|
" test.testReverseWords()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__==\"__main__\":\n",
|
||||||
|
" main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Solution Notebook\n",
|
||||||
|
"* Review the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_words/reverse_words_solution.ipynb) for discussion on algorithms and code solutions."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
162
arrays_strings/reverse_words/reverse_words_solution.ipynb
Normal file
162
arrays_strings/reverse_words/reverse_words_solution.ipynb
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small> <i> This notebook was prepared by Marco Guajardo. For license visit [github](https://github.com/donnemartin/interactive-coding-challenges) </i> </small>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Solution notebook\n",
|
||||||
|
"## Problem: Given a string of words, return a string with the words in reverse"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* [Constraits](#Constraint)\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",
|
||||||
|
"* Can we assume the string is ASCII?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Is whitespace important?\n",
|
||||||
|
" * no the whitespace does not change\n",
|
||||||
|
"* Is this case sensitive?\n",
|
||||||
|
" * yes\n",
|
||||||
|
"* What if the string is empty?\n",
|
||||||
|
" * return None\n",
|
||||||
|
"* Is the order of words important?\n",
|
||||||
|
" * yes\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Algorithm: Split words into a list and reverse each word individually\n",
|
||||||
|
"Steps:\n",
|
||||||
|
"\n",
|
||||||
|
"* Check if string is empty\n",
|
||||||
|
"* If not empty, split the string into a list of words \n",
|
||||||
|
"* For each word on the list\n",
|
||||||
|
" * reverse the word\n",
|
||||||
|
"* Return the string representation of the list\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"\n",
|
||||||
|
"* Time complexity is O(n) where n is the number of chars.\n",
|
||||||
|
"* Space complexity is O(n) where n is the number of chars. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def reverse_words(S):\n",
|
||||||
|
" if len(S) is 0:\n",
|
||||||
|
" return None\n",
|
||||||
|
" \n",
|
||||||
|
" words = S.split()\n",
|
||||||
|
" for i in range (len(words)):\n",
|
||||||
|
" words[i] = words[i][::-1]\n",
|
||||||
|
" \n",
|
||||||
|
" return \" \".join(words)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting reverse_words_solution.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%%writefile reverse_words_solution.py\n",
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\n",
|
||||||
|
"class UnitTest (object):\n",
|
||||||
|
" def testReverseWords(self, func):\n",
|
||||||
|
" assert_equal(func('the sun is hot'), 'eht nus si toh')\n",
|
||||||
|
" assert_equal(func(''), None)\n",
|
||||||
|
" assert_equal(func('123 456 789'), '321 654 987')\n",
|
||||||
|
" assert_equal(func('magic'), 'cigam')\n",
|
||||||
|
" print('Success: reverse_words')\n",
|
||||||
|
" \n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = UnitTest()\n",
|
||||||
|
" test.testReverseWords(reverse_words)\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__==\"__main__\":\n",
|
||||||
|
" main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Success: reverse_words\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"run -i reverse_words_solution.py"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
16
arrays_strings/reverse_words/reverse_words_solution.py
Normal file
16
arrays_strings/reverse_words/reverse_words_solution.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
from nose.tools import assert_equal
|
||||||
|
|
||||||
|
class UnitTest (object):
|
||||||
|
def testReverseWords(self, func):
|
||||||
|
assert_equal(func('the sun is hot'), 'eht nus si toh')
|
||||||
|
assert_equal(func(''), None)
|
||||||
|
assert_equal(func('123 456 789'), '321 654 987')
|
||||||
|
assert_equal(func('magic'), 'cigam')
|
||||||
|
print('Success: reverse_words')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test = UnitTest()
|
||||||
|
test.testReverseWords(reverse_words)
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user