{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unit 3 Sections 5-7 Hacks\n", "> Hacks\n", "\n", "- toc: true\n", "- badges: true\n", "- comments: true\n", "- categories: [csp]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.5-3.7 Notes \n", "\n", "Boolean- binary variable with only two possible values, \"true\" or \"false\" There are multiple operators used to evaluate 2 variables. In pseudocode the relational operators are =, >, <, ≠, ≥, and ≤. Python uses != instead of ≠. These operators help determine the boolean value of a statement.\n", "\n", "Logical operators allow for boolean values to be evaluated. Pseudocode uses the logical operators NOT, AND, and OR. Javascript uses the same logic, but uses different ways to represent the operators: &&(and), | |(or), |(not).\n", "\n", "Algorithm: Finite set of instructions that accomplish a specific task\n", "Conditional Statements: allow the code to act based on multiple conditions, such as user input or stored data\n", "use in order to receive different outputs from a set of code, and help achieve the general purpose of the algorithm\n", "Categories: 2 broad types of conditional statements are if statements and if-else statements\n", "if statement: will go through certain statements if the if expression is true\n", "if-else statement: will go through a statement no matter what, but the statement which is run depends on the if expression is true or false\n", "\n", "Nested conditional: A conditional inside of a conditional. Used in algorithms in order to print specific data or run certain tasks, to create further conditions in algorithms.\n", "We can use flowcharts to help display an easy to understand diagram of what we want a code segment to do." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For hacks, make a copy of this notebook and answer the questions or complete the code, as described in comments. Additionally, blog about any missed questions, or what you learned from this lesson." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.5 Hacks\n", "\n", "## Binary Practice\n", "Using psuedocode operators determine if the statements are true or false. The number type will be indicated in parentheses.\n", "\n", "**1. 90(D) = 1000(B)**\n", "\n", "- A. True\n", "- B. False\n", "answer: B false because the binary value of B is larger than the value of D.\n", "\n", "**2. 10(D) ≠ 0110(B)**\n", "\n", "- A. True\n", "- B. False\n", "answer: A true because the binary value of B is not equal to the value of D. \n", "\n", "**3. 56(D) ≥ 111000(B)**\n", "\n", "- A. True\n", "- B. False\n", "answer: A true because the binary value of B is 56 which is equal to the value of D.\n", "\n", "**3. 99(D) < 1110011(B)**\n", "\n", "- A. True\n", "- B. False\n", "answer: A true because the binary value of B is larger than the value of D." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, complete the binary truth tables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
AND Operator
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Value 1Value 2Result
111
100
010
000
\n", "
\n", "
OR Operator
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Value 1Value 2Result
111
101
011
000
\n", "
\n", "
Not operator
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NotValueResult
Not10
Not00
\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Practice" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "# Testing out relational operators\n", "# Practice with these statements\n", "\n", "print(20 > 20) # How can you change the operator to print a value of False?\n", "\n", "x = 30\n", "y = 20\n", "z = 10\n", "print(x >= y + z) # How can this return true by only manipulating the operator?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.6 Hacks\n", "\n", "## AP Prep\n", "\n", "**1. What is displayed by this code?**\n", "- result <-- 75\n", "- IF result < 80 {\n", " DISPLAY(\"Please schedule a retake.\")\n", "}\n", "- ELSE {\n", " DISPLAY(\"Nice job!\")\n", "}\n", "\n", "1. Nice job!\n", "2. Display\n", "3. Please schedule a retake.\n", "4. 75\n", "\n", "answer: 3 \"Please schedule a retake.\" If condition is met, 75 < 80 so the if display is shown.\n", "\n", "**2. How is an if statement different from an if-else statement.**\n", "\n", "1. Extra words.\n", "2. An if statement will only go through a process if a condition is met. An if-else statement will go through code no matter the conditions.\n", "3. They are the exact same.\n", "4. An if statement will go through the entire code segment every single time and the if-else statement is always used in an algorithm, no matter the conditions.\n", "\n", "answer: 2, in if statements the criteria must be made in order to go through the code.\n", "\n", "**3. What would be most appropriate for this situation? Ben wants to check his bank account. If his car fuel is full, he will go to the bank. Otherwise, he will go home. If he goes to the bank, he will withdraw money only if his balance is above $1000.**\n", "\n", "1. If statement\n", "2. If-else statement\n", "\n", "answer: 2 If-else, because if he has gas he will go to the bank BUT else (gas is not full) he will go home.\n", "\n", "**4. What would be most appropriate for this situation? Luke wants to play basketball. If it is sunny outside he will go to the park to play basketball.**\n", "\n", "1. If statement\n", "2. If-else statement\n", "\n", "answer: 1 Because it does not say what else he will do if it is not sunny\n", "\n", "\n", "## Using Python" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lion\n", "tiger\n", "wildebeest\n", "Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!\n", "jellyfish\n", "blobfish\n", "raven\n", "This animal lives in the desert\n", "This animal lives in the desert\n", "This animal lives in the desert\n", "This animal lives in the desert\n", "This animal lives in the desert\n", "This animal lives in the desert\n", "This animal lives in the desert\n" ] } ], "source": [ "animals = [\"lion\", \"tiger\", \"wildebeest\", \"shark\", \"jellyfish\", \"blobfish\", \"raven\"]\n", "\n", "for i in animals:\n", " if i == \"shark\": # What boolean value does this statement cause?\n", " print(\"Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!\")\n", " else:\n", " print(i)\n", "\n", "for i in animals:\n", " if i == \"lion\" or \"wildebeest\" or \"raven\":\n", " print(\"This animal lives in the desert\")\n", " else: \n", " print(i + \"does not live in the desert\")\n", "# Practice\n", "# Using only one more if statement, alter the code to print out a statement saying if an animal lives in the desert, based on booleans" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.7 Hacks\n", "\n", "## Exercise 1\n", "- Create dictionaries for multiple food items, with the listed specifications\n", " - Chicken Alfredo, Meat: Chicken, Time to Prepare: 60 minutes\n", " - Cheese Quesadilla, Meat: None, Time to Prepare: 10 minutes\n", " - Beef Wellington, Meat: Beef, Time to Prepare: 150 minutes\n", "- Used nested conditionals, determine which meal you can cook, given that a) you have no meat at home, and b) you only have 30 minutes to make the meal" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sorry, you do not have enough time and cannot cook a Chicken Alfredo\n", "You can cook a Cheese Quesadilla\n", "Sorry, you do not have enough time and cannot cook a Beef Wellington\n" ] } ], "source": [ "# Write code here\n", "chickenAlfredo = {\n", " \"meat\": True, \n", " \"prepTime\": 60,\n", " \"name\": \"Chicken Alfredo\"\n", "}\n", "\n", "cheeseQuesadilla = {\n", " \"meat\": False, \n", " \"prepTime\": 10,\n", " \"name\": \"Cheese Quesadilla\"\n", "\n", "}\n", "\n", "beefWellington = {\n", " \"meat\": True,\n", " \"prepTime\": 150,\n", " \"name\": \"Beef Wellington\"\n", "\n", "}\n", "\n", "def cookMeal(dish):\n", " if dish[\"prepTime\"] <= 30: \n", " if dish[\"meat\"] == False:\n", " print(\"You can cook a\", dish[\"name\"])\n", " else:\n", " print(\"Sorry, you do not have meat and cannot cook a\", dish[\"name\"] )\n", " else:\n", " print(\"Sorry, you do not have enough time and cannot cook a\", dish[\"name\"] )\n", "\n", "cookMeal(chickenAlfredo)\n", "cookMeal(cheeseQuesadilla)\n", "cookMeal(beefWellington)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2\n", "\n", "Make a flowchart([here](https://www.lucidchart.com/pages/examples/flowchart-maker) is one we used) and write pseudocode for the following scenario.\n", "- Mr. Yeung would like to grade live reviews. \n", "- He wants to see if each student has at least 2 issues on their project. If they don't they receive a score of 2.0.\n", "- If they have at least 2 issues, check that they have completed at least 5 of their scrumboard tasks.\n", "- If they have completed 5 scrumboard tasks, give the student a 2.7. If they have not completed 5 scrumboard tasks, give them a score of 2.5. If they have completed more than 5 tasks, give them a score of 3.0.\n", "- How much would a student with 3 issues and 1 complete scrumboard task receive?\n", "\n", "Answer: A student with 3 issues and 1 completed scrumboard task would receive a 2.5\n", "\n", "![](/elliepang/images/flowchart.png)\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.10 64-bit", "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.10" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" } } }, "nbformat": 4, "nbformat_minor": 2 }