diff --git a/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2.ipynb b/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2.ipynb index e590226443912b2d27268452c87e6cca8103b858..9699aee04d0aad6b9a53ac6c460b52e8d8c60e62 100644 --- a/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2.ipynb +++ b/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2.ipynb @@ -124,8 +124,9 @@ "## Learning Objectives\n", "After this lecture you will be able to...\n", "- Read and write nested conditional statements\n", - "- Define, state reasons for, and provide examples of refactoring.\n", - "- Refactor code containing conditional statements into boolean expressions\n" + "- Define, state reasons for, and provide examples of refactoring\n", + "- Refactor code containing conditional statements into boolean expressions\n", + "- Recognize and utilize some common refactoring patterns\n" ] }, { @@ -365,7 +366,7 @@ "source": [ "### You Try It\n", "\n", - "How give it a try. Finish the `refactor_combo()` function so its behavior is exactly the same as check_combination(). Try and do it without using a single conditional. Also, write tests to prove that its behavior is the same." + "Now give it a try. Finish the `refactor_combo()` function so its behavior is exactly the same as check_combination(). Try and do it without using a single conditional. Also, write tests to prove that its behavior is the same." ] }, { @@ -434,6 +435,101 @@ "## Also write tests to prove that its behavior hasn't changed\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Common Refactoring Patterns\n", + "\n", + "There are some common refactoring patterns that you can recognize and use to restructure your code. Take, for example, the `or` operator. Imagine needing to `or` together three boolean values. There are multiple ways that this can be done that will result in the same before. Take a look at the multiple implementations of the `or2()` function to see three different ways that this can be done." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def or2(cond1, cond2, cond3): #Version 1\n", + " return cond1 or cond2 or cond3\n", + "\n", + "## now use the function to see its behavior\n", + "print(or2(False,True,False))\n", + "## TODO: add more tests of the function\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def or2(cond1, cond2, cond3): #Version 2\n", + " rv = False\n", + " rv = rv or cond1\n", + " rv = rv or cond2\n", + " rv = rv or cond3\n", + " return rv\n", + "\n", + "##TODO: add tests to demonstrate this version behaves the same as the previous version\n", + "print(or2(False,True,False))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def or2(cond1, cond2, cond3): #Version 3\n", + " if cond1:\n", + " return True\n", + " elif cond2:\n", + " return True\n", + " elif cond3:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "##TODO: Again, test that its behavior is the same\n", + "print(or2(False,True,False))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What are the advantages or disadvantages of these different versions of the `or2()` function. Some methods may write less code while other methods may seem more understandable.\n", + "\n", + "### You Try It\n", + "\n", + "Try refactoring the `and2()` function to create other versions that have the same behavior." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def and2(cond1, cond2, cond3):\n", + " return cond1 and cond2 and cond3\n", + "\n", + "print(and2(True,True,False))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "##TODO: write an alternate way of implementing this same behavior\n", + "\n", + "def and2(cond1, cond2, cond3):\n", + " pass" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -554,7 +650,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.6" } }, "nbformat": 4, diff --git a/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2_Solution.ipynb b/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2_Solution.ipynb index f9542108b00ea8b4cda7585b13a4d2bd21892e34..f4affb64daca62230408fcb44fe66fe51edd527d 100644 --- a/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2_Solution.ipynb +++ b/f24/Louis_Lecture_Notes/09_Conditionals2/Lec_09_Conditionals2_Solution.ipynb @@ -495,7 +495,7 @@ "source": [ "### You Try It\n", "\n", - "How give it a try. Finish the `refactor_combo()` function so its behavior is exactly the same as check_combination(). Try and do it without using a single conditional. Also, write tests to prove that its behavior is the same." + "Now give it a try. Finish the `refactor_combo()` function so its behavior is exactly the same as check_combination(). Try and do it without using a single conditional. Also, write tests to prove that its behavior is the same." ] }, { @@ -604,6 +604,179 @@ "print(check_different_refactor(True, True))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Common Refactoring Patterns\n", + "\n", + "There are some common refactoring patterns that you can recognize and use to restructure your code. Take, for example, the `or` operator. Imagine needing to `or` together three boolean values. There are multiple ways that this can be done that will result in the same before. Take a look at the multiple implementations of the `or2()` function to see three different ways that this can be done." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "def or2(cond1, cond2, cond3): #Version 1\n", + " return cond1 or cond2 or cond3\n", + "\n", + "## now use the function to see its behavior\n", + "print(or2(False,True,False))\n", + "## TODO: add more tests of the function\n", + "print(or2(False,False,False))\n", + "print(or2(False,False,True))\n", + "print(or2(False,True,True))\n", + "print(or2(True,True,True))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "def or2(cond1, cond2, cond3): #Version 2\n", + " rv = False\n", + " rv = rv or cond1\n", + " rv = rv or cond2\n", + " rv = rv or cond3\n", + " return rv\n", + "\n", + "##TODO: add tests to demonstrate this version behaves the same as the previous version\n", + "print(or2(False,True,False))\n", + "print(or2(False,False,False))\n", + "print(or2(False,False,True))\n", + "print(or2(False,True,True))\n", + "print(or2(True,True,True))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "def or2(cond1, cond2, cond3): #Version 3\n", + " if cond1:\n", + " return True\n", + " elif cond2:\n", + " return True\n", + " elif cond3:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "##TODO: Again, test that its behavior is the same\n", + "print(or2(False,True,False))\n", + "print(or2(False,False,False))\n", + "print(or2(False,False,True))\n", + "print(or2(False,True,True))\n", + "print(or2(True,True,True))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What are the advantages or disadvantages of these different versions of the `or2()` function. Some methods may write less code while other methods may seem more understandable.\n", + "\n", + "### You Try It\n", + "\n", + "Try refactoring the `and2()` function to create other versions that have the same behavior." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "def and2(cond1, cond2, cond3):\n", + " return cond1 and cond2 and cond3\n", + "\n", + "print(and2(True,True,False))\n", + "print(and2(True,False,False))\n", + "print(and2(True,True,True))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "##TODO: write an alternate way of implementing this same behavior\n", + "\n", + "def and2(cond1, cond2, cond3):\n", + " rv = True\n", + " rv = rv and cond1\n", + " rv = rv and cond2\n", + " rv = rv and cond3\n", + " return rv\n", + "\n", + "print(and2(True,True,False))\n", + "print(and2(True,False,False))\n", + "print(and2(True,True,True))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -813,7 +986,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.6" } }, "nbformat": 4,