Skip to content

Instantly share code, notes, and snippets.

@Ludek1234
Created June 17, 2018 11:21
Show Gist options
  • Save Ludek1234/32382bfb9574d3e4e5ee2d22ff834f84 to your computer and use it in GitHub Desktop.
Save Ludek1234/32382bfb9574d3e4e5ee2d22ff834f84 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reverse##\n",
"Define a function called __reverse__ that takes a string text and returns that string in reverse. For example: reverse(\"abcd\") should return \"dcba\".\n",
"\n",
"You may not use __reversed or [::-1]__ to help you with this.\n",
"\n",
"You may get a string containing special characters (for example, !, @, or #).\n",
"### Hint###\n",
"Consider how you would loop through text starting from the last character through the first character.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'#@!FEDCBA'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def reverse(text):\n",
" new_text = \"\" \n",
" for char in text: \n",
" new_text = char + new_text \n",
" return (new_text)\n",
"reverse(\"ABCDEF!@#\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ABCDEF!@#'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def reverse(text):\n",
" new_text = \"\" \n",
" for char in text: \n",
" new_text = new_text + char\n",
" return (new_text)\n",
"reverse(\"ABCDEF!@#\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment