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.

3.5 Hacks

Binary Practice

Using psuedocode operators determine if the statements are true or false. The number type will be indicated in parentheses.

1. 90(D) = 1000(B)

  • A. True
  • B. False

☆ My answer: 1000(B) = 8(D) ≠ 90(D). Since the 2 values aren't equal to each other, the answer is false.

2. 10(D) ≠ 0110(B)

  • A. True
  • B. False

☆ My answer: 0110(B) = 6(D) ≠ 10(D). Since the 2 values aren't equal to each other, the answer is true because the question is asking if they aren't equal to each other.

3. 56(D) ≥ 111000(B)

  • A. True
  • B. False

☆ My answer: 111000(B) = (8 + 16 + 32)(D) = 56(D) ≤ 56(D). Since this comparison can be satisfied when the 2 values are equal to each other (because 56(D) has to be greater than or equal to 111000(B)), the answer is true.

3. 99(D) < 1110011(B)

  • A. True
  • B. False

☆ My answer: 1110011(B) = (1 + 2 + 16 + 32 + 64)(D) = 115(D) > 99(D). Since 1110011(B) is indeed greater than 99(D), the answer is true.

Now, complete the binary truth tables

AND Operator
Value 1 Value 2 Result
1 1 1
1 0 0
0 1 0
0 0 0
OR Operator
Value 1 Value 2 Result
1 1 1
1 0 1
0 1 1
0 0 0
Not operator
Not Value Result
Not 1 0
Not 0 1

Python Practice

# Practice with these statements

print(20 == 20) # How can you change the operator to print a value of False?
# ^ we can make this print a value of False by changing == to !=
x = 30
y = 20
z = 10
print(x > y + z) # How can this return true by only manipulating the operator?
# ^ we can make this return true by changing > sing to either >=, <=, or ==.

# Manipulate the variables x, y, and z to make the below statement return true
x = z # sets x to value of z
print(x == z)
True
False
True

3.6 Hacks

AP Prep

1. What is displayed by this code?

  • result <-- 75
  • IF result < 80 { DISPLAY("Please schedule a retake.") }
  • ELSE { DISPLAY("Nice job!") }
  1. Nice job!
  2. Display
  3. Please schedule a retake.
  4. 75

☆ My answer: Since result is set to the value of 75 and 75 < 80, Please schedule a retake. will be displayed.

2. How is an if statement different from an if-else statement.

  1. Extra words.
  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.
  3. They are the exact same.
  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.

☆ My answer: The answer is #2 because an if statement will only be run if the condition for it is met, while an if-else statement will be run no matter what because the either the if statement or else statement has to be true.

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.

  1. If statement
  2. If-else statement

☆ My answer: The answer is #2 because both if statements and else statements are needed in this situation.

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.

  1. If statement
  2. If-else statement

☆ My answer: The answer is #1 because only a single if statement is needed for this situation. If it's sunny outside, he will go to the park and play basketball.

Using Python

animals = ["lion", "tiger", "wildebeest", "shark", "jellyfish", "blobfish", "raven"]

for i in animals:
    if i == "shark": # This statement will cause a boolean value of True only when i is set to the value of "shark"
        print("Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!")
    if i == "raven":
        print("Some species of ravens live in the desert.")
    else:
        print(i)

# Practice
# 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
lion
tiger
wildebeest
Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!
shark
jellyfish
blobfish
Some species of ravens live in the desert.

3.7 Hacks

Exercise 1

  • Create dictionaries for multiple food items, with the listed specifications
    • Chicken Alfredo, Meat: Chicken, Time to Prepare: 60 minutes
    • Cheese Quesadilla, Meat: None, Time to Prepare: 10 minutes
    • Beef Wellington, Meat: Beef, Time to Prepare: 150 minutes
  • 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
chickenAlfredo = {
    "Food": "Chicken Alfredo",
    "Meat": "Chicken",
    "Time to Prepare (min)": 60
}

cheeseQuesadilla = {
    "Food": "Cheese Quesadilla",
    "Meat": "None",
    "Time to Prepare (min)": 10
}

beefWellington = {
    "Food": "Beef Wellington",
    "Meat": "Beef",
    "Time to Prepare (min)": 150
}

#No Meat and in 30 min or less:

def canCook(meal):
    if meal["Meat"] == "None":
        if meal["Time to Prepare (min)"] <= 30:
            print("This meal (" + meal["Food"] + ") can be cooked!")
        else:
            print("This meal can't be cooked.")
    else:
        print("This meal (" + meal["Food"] + ") can't be cooked.")

canCook(chickenAlfredo)
canCook(cheeseQuesadilla)
canCook(beefWellington)
This meal (Chicken Alfredo) can't be cooked.
This meal (Cheese Quesadilla) can be cooked!
This meal (Beef Wellington) can't be cooked.

Exercise 2

Make a flowchart(here is one we used) and write pseudocode for the following scenario.

  • Mr. Yeung would like to grade live reviews.
  • 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.
  • If they have at least 2 issues, check that they have completed at least 5 of their scrumboard tasks.
  • 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.
  • How much would a student with 3 issues and 1 complete scrumboard task receive?

(Flowchart on Comment)

Pseudocode:

if issues ≥ 2:

    if tasks == 5:

        score ⟵ 2.7

        if tasks > 5:

            score ⟵ 3.0

else:

    score ⟵ 2.0

(ignore boxes)