{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## The Middle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose that Yulin, another INF110 student, is tasked with writing code to find the middle of three distinct numbers named `a`, `b`, and `c`. (By “middle” we mean the value that is neither the maximum nor the minimum.) For example, given the values 4, 2, and 7, the code should give back 4.\n", "\n", "Yulin has a moment of inspired thought and rapidly types the following into her Jupyter notebook:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a + b + c - (max(a, b, c) + min(a, b, c))\n", "# Originally: a + b + c - max(a, b, c) + min(a, b, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is her code correct? Why or why not? If not, make a *simple* change to the cell above to fix it.\n", "\n", "*Hint:* Running the cell will produce an error because we haven't defined `a`, `b`, and `c`, but you're free to make any changes you want to test your ideas. (When you submit, just indicate which cell is your final answer.)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "888" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Testing the interesting function: a + b + c - max(a, b, c) + min(a, b, c)\n", "\n", "# Using the test input vals a = 4, b = 2, c = 7\n", "\n", "##a = 4\n", "##b = 2\n", "##c = 7\n", "\n", "a = 999\n", "b = 345\n", "c = 888\n", "\n", "a + b + c - (max(a, b, c) + min(a, b, c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code was not correct, would return a value that is not the middle one. By adding parentheses before the call of max and after the call of min, you can return a middle value of three variables every single time." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 1 }