intro-to-python/06_text/01_exercises.ipynb

996 lines
28 KiB
Text
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note**: Click on \"*Kernel*\" > \"*Restart Kernel and Run All*\" in [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/) *after* finishing the exercises to ensure that your solution runs top to bottom *without* any errors. If you cannot run this file on your machine, you may want to open it [in the cloud <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_mb.png\">](https://mybinder.org/v2/gh/webartifex/intro-to-python/develop?urlpath=lab/tree/06_text/01_exercises.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 6: Text & Bytes (Coding Exercises)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The exercises below assume that you have read the [first part <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/06_text/00_content.ipynb) of Chapter 6.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Detecting Palindromes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Palindromes <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_wiki.png\">](https://en.wikipedia.org/wiki/Palindrome) are sequences of characters that read the same backward as forward. Examples are first names like \"Hannah\" or \"Otto,\" words like \"radar\" or \"level,\" or sentences like \"Was it a car or a cat I saw?\"\n",
"\n",
"In this exercise, you implement various functions that check if the given arguments are palindromes or not. We start with an iterative implementation and end with a recursive one.\n",
"\n",
"Conceptually, the first function, `unpythonic_palindrome()`, is similar to the \"*Is the square of a number in `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]` greater than `100`?*\" example in [Chapter 4 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/04_iteration/03_content.ipynb#Example:-Is-the-square-of-a-number-in-[7,-11,-8,-5,-3,-12,-2,-6,-9,-10,-1,-4]-greater-than-100?): It assumes that the `text` argument is a palindrome (i.e., it initializes `is_palindrom` to `True`) and then checks in a `for`-loop if a pair of corresponding characters, `forward` and `backward`, contradicts that.\n",
"\n",
"**Q1**: How many iterations are needed in the `for`-loop? Take into account that `text` may contain an even or odd number of characters! Inside `unpythonic_palindrome()` below, write an expression whose result is assigned to `chars_to_check`!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your answer > "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q2**: `forward_index` is the index going from left to right. How can we calculate `backward_index`, the index going from right to left, for a given `forward_index`? Write an expression whose result is assigned to `backward_index`! Then, use the indexing operator `[]` to obtain the two characters, `forward` and `backward`, from `text`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q3**: Finish `unpythonic_palindrome()` below! Add code that adjusts `text` such that the function is case insensitive if the `ignore_case` argument is `True`! Make sure that the function returns once the first pair of corresponding characters does not match!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def unpythonic_palindrome(text, *, ignore_case=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; must be an individual word\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answer to Q3\n",
" is_palindrome = ...\n",
" if ignore_case:\n",
" ...\n",
" # answer to Q1\n",
" chars_to_check = ...\n",
"\n",
" for forward_index in range(chars_to_check):\n",
" # answer to Q2\n",
" backward_index = ...\n",
" forward = ...\n",
" backward = ...\n",
"\n",
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" # answer to Q3\n",
" if ...:\n",
" is_palindrome = ...\n",
" ...\n",
"\n",
" return is_palindrome"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4**: Ensure that `unpythonic_palindrome()` works for the provided test cases (i.e., the `assert` statements do *not* raise an `AssertionError`)! Also, for each of the test cases, provide a brief explanation of what makes them *unique*!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"noon\") is True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 1 >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"Hannah\") is True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 2 >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"Hannah\", ignore_case=False) is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 3 >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"radar\") is True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 4 >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"Hanna\") is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 5 >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert unpythonic_palindrome(\"Warsaw\") is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your explanation 6 >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`unpythonic_palindrome()` is considered *not* Pythonic as it uses index variables to implement the looping logic. Instead, we should simply loop over an *iterable* object to work with its elements one by one.\n",
"\n",
"**Q5**: Copy your solutions to the previous questions into `almost_pythonic_palindrome()` below!\n",
"\n",
"**Q6**: The [reversed() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#reversed) and [zip() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#zip) built-ins allow us to loop over the same `text` argument *in parallel* in both forward *and* backward order. Finish the `for` statement's header line to do just that!\n",
"\n",
"Hint: You may need to slice the `text` argument."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def almost_pythonic_palindrome(text, *, ignore_case=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; must be an individual word\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" is_palindrome = ...\n",
" if ignore_case:\n",
" ...\n",
" chars_to_check = ...\n",
"\n",
" # answer to Q6\n",
" for ... in ...:\n",
"\n",
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" # answers from above\n",
" if ...:\n",
" is_palindrome = ...\n",
" ...\n",
"\n",
" return is_palindrome"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7**: Verify that the test cases work as before!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"noon\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"Hannah\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"Hannah\", ignore_case=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"radar\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"Hanna\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert almost_pythonic_palindrome(\"Warsaw\") is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8**: `almost_pythonic_palindrome()` above may be made more Pythonic by removing the variable `is_palindrome` with the *early exit* pattern. Make the corresponding changes!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def pythonic_palindrome(text, *, ignore_case=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; must be an individual word\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" if ignore_case:\n",
" ...\n",
" chars_to_check = ...\n",
"\n",
" for ... in ...:\n",
"\n",
" print(forward, \"and\", backward) # added for didactical purposes\n",
"\n",
" if ...:\n",
" # answer to Q8\n",
" ...\n",
"\n",
" # answer to Q8\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: Verify that the test cases still work!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"noon\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"Hannah\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"Hannah\", ignore_case=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"radar\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"Hanna\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert pythonic_palindrome(\"Warsaw\") is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10**: `pythonic_palindrome()` is *not* able to check numeric palindromes. In addition to the string method that implements the case insensitivity and that essentially causes the `AttributeError`, what *abstract behaviors* are numeric data types, such as the `int` type in the example below, missing that would also cause runtime errors? "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pythonic_palindrome(12321)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11**: Copy your code from `pythonic_palindrome()` above into `palindrome_ducks()` below and make the latter conform to *duck typing*!\n",
"\n",
"Hints: You may want to use the [str() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#func-str) built-in. You only need to add *one* short line of code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def palindrome_ducks(text, *, ignore_case=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; must be an individual word\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answer to Q11\n",
" ...\n",
" # answers from above\n",
" if ignore_case:\n",
" text = ...\n",
" chars_to_check = ...\n",
"\n",
" for ... in ...:\n",
" if ...:\n",
" ...\n",
"\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12**: Verify that the two new test cases work as well!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert palindrome_ducks(12321) is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert palindrome_ducks(12345) is False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`palindrome_ducks()` can *not* process palindromes that consist of more than one word."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"palindrome_ducks(\"Never odd or even.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"palindrome_ducks(\"Eva, can I stab bats in a cave?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"palindrome_ducks(\"A man, a plan, a canal - Panama.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"palindrome_ducks(\"A Santa lived as a devil at NASA!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"palindrome_ducks(\"\"\"\n",
" Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane,\n",
" Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette,\n",
" Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette,\n",
" Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee,\n",
" Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13**: Implement the final iterative version `is_a_palindrome()` below. Copy your solution from `palindrome_ducks()` above and add code that removes the \"special\" characters (and symbols) from the longer example palindromes above so that they are effectively ignored! Note that this processing should only be done if the `ignore_symbols` argument is set to `True`.\n",
"\n",
"Hints: Use the [replace() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/stdtypes.html#str.replace) method on the `str` type to achieve that. You may want to do so within another `for`-loop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_a_palindrome(text, *, ignore_case=True, ignore_symbols=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; may be multiple words\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
" ignore_symbols (bool): If special characters like \".\" or \"?\" and others\n",
" are ignored; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" ...\n",
" if ignore_case:\n",
" ...\n",
" # answer to Q13\n",
" if ignore_symbols:\n",
" for ... in ...:\n",
" ...\n",
" # answers from above\n",
" chars_to_check = ...\n",
"\n",
" for ... in ...:\n",
" if ...:\n",
" ...\n",
"\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14**: Verify that all test cases below work!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"noon\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Hannah\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Hannah\", ignore_case=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"radar\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Hanna\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Warsaw\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(12321) is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(12345) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Never odd or even.\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Never odd or even.\", ignore_symbols=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"Eva, can I stab bats in a cave?\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"A man, a plan, a canal - Panama.\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"A Santa lived as a devil at NASA!\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert is_a_palindrome(\"\"\"\n",
" Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane,\n",
" Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette,\n",
" Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette,\n",
" Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee,\n",
" Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.\n",
"\"\"\") is True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's look at a *recursive* formulation in `recursive_palindrome()` below.\n",
"\n",
"**Q15**: Copy the code from `is_a_palindrome()` that implements the duck typing, the case insensitivity, and the removal of special characters!\n",
"\n",
"The recursion becomes apparent if we remove the *first* and the *last* character from a given `text`: `text` can only be a palindrome if the two removed characters are the same *and* the remaining substring is a palindrome itself! So, the word `\"noon\"` has only *one* recursive call while `\"radar\"` has *two*.\n",
"\n",
"Further, `recursive_palindrome()` has *two* base cases of which only *one* is reached for a given `text`: First, if `recursive_palindrome()` is called with either an empty `\"\"` or a `text` argument with `len(text) == 1`, and, second, if the two removed characters are *not* the same.\n",
"\n",
"**Q16**: Implement the two base cases in `recursive_palindrome()`! Use the *early exit* pattern!\n",
"\n",
"**Q17**: Add the recursive call to `recursive_palindrome()` with a substring of `text`! Pass in the `ignore_case` and `ignore_symbols` arguments as `False`! This avoids unnecessary computations in the recursive calls. Why is that the case?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" < your answer >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def recursive_palindrome(text, *, ignore_case=True, ignore_symbols=True):\n",
" \"\"\"Check if a text is a palindrome or not.\n",
"\n",
" Args:\n",
" text (str): Text to be checked; may be multiple words\n",
" ignore_case (bool): If the check is case insensitive; defaults to True\n",
" ignore_symbols (bool): If special characters like \".\" or \"?\" and others\n",
" are ignored; defaults to True\n",
"\n",
" Returns:\n",
" is_palindrome (bool)\n",
" \"\"\"\n",
" # answers from above\n",
" ...\n",
" if ignore_case:\n",
" ...\n",
" if ignore_symbols:\n",
" for ... in ...:\n",
" ...\n",
"\n",
" # answer to Q16\n",
" if ...:\n",
" ...\n",
" elif ...:\n",
" ...\n",
"\n",
" # answer to Q17\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q18**: Lastly, verify that `recursive_palindrome()` passes all the test cases below!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"noon\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Hannah\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Hannah\", ignore_case=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"radar\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Hanna\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Warsaw\") is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(12321) is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(12345) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Never odd or even.\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Never odd or even.\", ignore_symbols=False) is False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"Eva, can I stab bats in a cave?\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"A man, a plan, a canal - Panama.\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"A Santa lived as a devil at NASA!\") is True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert recursive_palindrome(\"\"\"\n",
" Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane,\n",
" Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette,\n",
" Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette,\n",
" Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee,\n",
" Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.\n",
"\"\"\") is True"
]
}
],
"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.8.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}