August 2023 to June 2024
View the Project on GitHub IshanCornick/new_student
Lets start off with the basics. What is an algorithm?
What are algorithms used for?
Why are algorithms important?
# Psuedo Code 1
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
ELSE
{
IF (temp >= 65){
DISPLAY ("sure I will play outside")
}
ELSE
{
DISPLAY ("its too cold outside")
}
}
# Psuedo Code 2
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
IF (temp >= 65)
{
DISPLAY ("sure I will play outside")
}
IF (temp < 65)
{
DISPLAY ("its too cold outside")
}
What is the difference between the two pieces of code in the cell above?
# Code 1
print("What is the temperature outside in F?")
temp = int(input())
if temp >= 90:
print("its too hot outide")
else:
if temp >= 65:
print("sure I will play outside")
else:
print("its too cold outside")
What is the temperature outside in F?
sure I will play outside
# Code 2
print("What is the temperature outside in F?")
temp = int(input())
if temp >= 90:
print("its too hot outide")
if temp >= 65:
print("sure I will play outside")
if temp < 65:
print("its too cold outside")
What is the temperature outside in F?
its too hot outide
sure I will play outside
What happens if we plug 56 for the temp? What happens if we plug 95 in?
If we plug 56 in, then it will display the text “its too cold outside.” It first checks the first input, if temp is greater than or equal to 90. Which it isn’t so it moves on to the next if. If temp is greater than or equal to 65, which it isn’t so it checks the last if/else statement and displays the text its too cold outside.
If we plug 95 in we get two different results. Code one displays “its too hot outide” but code two displays “its too hot outide” and “sure I will play outside.” Why is this?
Adjust Pseudo Code #2 so that it has the same output as Code #1 for all inputs.
# Insert your code here:
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
ELIF (temp >= 65)
{
DISPLAY ("sure I will play outside")
}
ELIF (temp < 65)
{
DISPLAY ("its too cold outside")
}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 2
1 # Insert your code here:
----> 2 DISPLAY ("What is the temperature outside in F?")
3 temp <- INPUT()
4 IF (temp >= 90)
NameError: name 'DISPLAY' is not defined
Quick reminder:
We have given an algorithm that uses conditionals and two boolean statements that should have the same output as the conditional. Which boolean statement works and which one doesn’t?
# Psuedo Code
IF (isHoliday)
{
driveWork<- False
}
ELSE
{
IF (iswWeekday)
{
driveWork <_ True
}
ELSE
{
driveWork <- False
}
}
# Option 1:
driveWork <- ( (isHoliday) AND (isWeekday))
# Option 2:
driveWork <- ( (NOT (isHoliday)) AND (isWeekday))
Option 2 is the correct answer
Using the commands listed below, move the robot (gray triangle) through the white squares to the gray square wihtout touching the black squares in the least amount of lines as possible.
Commands allowed: MOVE_FORWARD()
REPEAT 4 TIMES
MOVE_FORWARD()
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 MOVE_FORWARD()
2 ROTATE_RIGHT()
NameError: name 'MOVE_FORWARD' is not defined
Change your code to an algorithm that works for any given course.
Hint: use if, elif, else, and CAN_MOVE()
# Insert your code here:
IF CAN_MOVE is true:
MOVE_FORWARD()
ELIF:
ROTATE_RIGHT()
MOVE_FORWARD())
Create an algorithm that uses selection and/or iteration to determine the cost of one item. THe display at the stores says the follorwing:
# Psuedo Code
DISPLAY ("What is the cost of the item?")
cost <- INPUT()
DIPLAY ("Is the tag green or red (type "True" if it is green, type "False" for red)")
tag <- BOOL(INPUT())
IF (tag == True) # Check if it is green tag (refer to lines above)
{
cost <- 0.75 * cost
}
IF (tag == False) # Check if it is green tag (refer to lines above)
{
cost <- 0.40 * cost
}
cost <- 1.10 * cost
print("What is the cost of the item?")
cost = int(input())
print("Is the tag green or red (type "True" if it is green, type "False" for red)")
tag = bool(input())
if tag == True: # Check if it is green tag (refer to lines above)
cost = 0.75 * cost
if tag == False: # Check if it is green tag (refer to lines above)
cost = 0.40 * cost
cost = 1.10 * cost # accounting for tax
6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Collatz proposed that this sequence of numbers would always terminate at 1. The problem of whether this is true or not for all positive integers is still unsolved today.
Lets create an algorithm that will start with any positive integer “n” and display the full sequence of numbers that result from this conjecture.
# Psuedo Code
DISPLAY ("choose a value for n")
n <- INPUT()
DISPLAY (n)
REPEAT UNTIL (n = 1)
{
IF (n MOD 2 = 0)
{
n <- n/2
}
ELSE
{
n <- 3 * n + 1
}
DISPLAY (n)
}
# Code
print("choose a value for n")
n = int(input())
print(n)
while n != 1:
if n % 2 == 0:
n = n/2
else:
n = (n*3)+1
print(n)
choose a value for n
6
3.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0
We are given an algorhtim (below) for a robot to move from the current square to the grey square, completing the course (below). However it doesn’t work. Why does the given algorithm not work?
Using the commands listed below, fix the algorithm
Commands allowed: MOVE_FORWARD()
REPEAT UNTIL (goalReached)
{
IF (CAN_MOVE(FORWARD))
{
MOVE_FORWARD
}
ELSE
{
IF CAN_MOVE(RIGHT)
{
ROTATE_RIGHT
MOVE_FORWARD
}
}
}
It will go on the black square and forever go into a square loop
# Insert your updated algorithm here:
REPEAT UNTIL (goalReached)
{
IF (CAN_MOVE(FORWARD) and NOBLACK_BOX(true))
{
MOVE_FORWARD
}
ELSE
{
IF CAN_MOVE(RIGHT)
{
ROTATE_RIGHT
MOVE_FORWARD
}
}
}
Create an algorithm that uses selection and/or iteration that will represent one player’s turn in a game.
During a turn, each player gets 4 attempts/chances to get the greates number possible.
During each attempt the player will use a random number generator to select a random number 1-10.
After the player has had 4 chances, their score is the greatest number they receieved from the random number generator, and their turn is over.
import random
def play_turn():
greatest_number = 0
for attempt1 in range(1, 5):
random_number = random.randint(1, 10)
print(f"Attempt {attempt1}: Random number is {random_number}")
if random_number > greatest_number:
greatest_number = random_number
print(f"Player's 1 score for this turn: {greatest_number}")\
great_number = 0
for attempt2 in range(1, 5):
random_num = random.randint(1, 10)
print(f"Attempt {attempt2}: Random number is {random_num}")
if random_num > great_number:
great_number = random_num
print(f"Player's 2 score for this turn: {great_number}")
if great_number > greatest_number:
print(f"Player 2 wins with the higher number of: {great_number}")
elif greatest_number > great_number:
print(f"Player 1 wins with the higher number of: {greatest_number}")
else:
print(f"Player 1 and 2 tied with a score of: {greatest_number}")
play_turn()
Attempt 1: Random number is 1
Attempt 2: Random number is 5
Attempt 3: Random number is 4
Attempt 4: Random number is 8
Player's 1 score for this turn: 8
Attempt 1: Random number is 8
Attempt 2: Random number is 8
Attempt 3: Random number is 6
Attempt 4: Random number is 4
Player's 2 score for this turn: 8
Player 1 and 2 tied with a score of: 8
Use the following flowchart to assist you:
Lets start off with the basics. What is an algorithm?
What are algorithms used for?
Why are algorithms important?
# Psuedo Code 1
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
ELSE
{
IF (temp >= 65){
DISPLAY ("sure I will play outside")
}
ELSE
{
DISPLAY ("its too cold outside")
}
}
# Psuedo Code 2
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
IF (temp >= 65)
{
DISPLAY ("sure I will play outside")
}
IF (temp < 65)
{
DISPLAY ("its too cold outside")
}
What is the difference between the two pieces of code in the cell above?
# Code 1
print("What is the temperature outside in F?")
temp = int(input())
if temp >= 90:
print("its too hot outide")
else:
if temp >= 65:
print("sure I will play outside")
else:
print("its too cold outside")
What is the temperature outside in F?
sure I will play outside
# Code 2
print("What is the temperature outside in F?")
temp = int(input())
if temp >= 90:
print("its too hot outide")
if temp >= 65:
print("sure I will play outside")
if temp < 65:
print("its too cold outside")
What is the temperature outside in F?
its too hot outide
sure I will play outside
What happens if we plug 56 for the temp? What happens if we plug 95 in?
If we plug 56 in, then it will display the text “its too cold outside.” It first checks the first input, if temp is greater than or equal to 90. Which it isn’t so it moves on to the next if. If temp is greater than or equal to 65, which it isn’t so it checks the last if/else statement and displays the text its too cold outside.
If we plug 95 in we get two different results. Code one displays “its too hot outide” but code two displays “its too hot outide” and “sure I will play outside.” Why is this?
Adjust Pseudo Code #2 so that it has the same output as Code #1 for all inputs.
# Insert your code here:
DISPLAY ("What is the temperature outside in F?")
temp <- INPUT()
IF (temp >= 90)
{
DISPLAY("its too hot outide")
}
ELIF (temp >= 65)
{
DISPLAY ("sure I will play outside")
}
ELIF (temp < 65)
{
DISPLAY ("its too cold outside")
}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 2
1 # Insert your code here:
----> 2 DISPLAY ("What is the temperature outside in F?")
3 temp <- INPUT()
4 IF (temp >= 90)
NameError: name 'DISPLAY' is not defined
Quick reminder:
We have given an algorithm that uses conditionals and two boolean statements that should have the same output as the conditional. Which boolean statement works and which one doesn’t?
# Psuedo Code
IF (isHoliday)
{
driveWork<- False
}
ELSE
{
IF (iswWeekday)
{
driveWork <_ True
}
ELSE
{
driveWork <- False
}
}
# Option 1:
driveWork <- ( (isHoliday) AND (isWeekday))
# Option 2:
driveWork <- ( (NOT (isHoliday)) AND (isWeekday))
Option 2 is the correct answer
Using the commands listed below, move the robot (gray triangle) through the white squares to the gray square wihtout touching the black squares in the least amount of lines as possible.
Commands allowed: MOVE_FORWARD()
REPEAT 4 TIMES
MOVE_FORWARD()
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 MOVE_FORWARD()
2 ROTATE_RIGHT()
NameError: name 'MOVE_FORWARD' is not defined
Change your code to an algorithm that works for any given course.
Hint: use if, elif, else, and CAN_MOVE()
# Insert your code here:
IF CAN_MOVE is true:
MOVE_FORWARD()
ELIF:
ROTATE_RIGHT()
MOVE_FORWARD())
Create an algorithm that uses selection and/or iteration to determine the cost of one item. THe display at the stores says the follorwing:
# Psuedo Code
DISPLAY ("What is the cost of the item?")
cost <- INPUT()
DIPLAY ("Is the tag green or red (type "True" if it is green, type "False" for red)")
tag <- BOOL(INPUT())
IF (tag == True) # Check if it is green tag (refer to lines above)
{
cost <- 0.75 * cost
}
IF (tag == False) # Check if it is green tag (refer to lines above)
{
cost <- 0.40 * cost
}
cost <- 1.10 * cost
print("What is the cost of the item?")
cost = int(input())
print("Is the tag green or red (type "True" if it is green, type "False" for red)")
tag = bool(input())
if tag == True: # Check if it is green tag (refer to lines above)
cost = 0.75 * cost
if tag == False: # Check if it is green tag (refer to lines above)
cost = 0.40 * cost
cost = 1.10 * cost # accounting for tax
6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Collatz proposed that this sequence of numbers would always terminate at 1. The problem of whether this is true or not for all positive integers is still unsolved today.
Lets create an algorithm that will start with any positive integer “n” and display the full sequence of numbers that result from this conjecture.
# Psuedo Code
DISPLAY ("choose a value for n")
n <- INPUT()
DISPLAY (n)
REPEAT UNTIL (n = 1)
{
IF (n MOD 2 = 0)
{
n <- n/2
}
ELSE
{
n <- 3 * n + 1
}
DISPLAY (n)
}
# Code
print("choose a value for n")
n = int(input())
print(n)
while n != 1:
if n % 2 == 0:
n = n/2
else:
n = (n*3)+1
print(n)
choose a value for n
6
3.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0
We are given an algorhtim (below) for a robot to move from the current square to the grey square, completing the course (below). However it doesn’t work. Why does the given algorithm not work?
Using the commands listed below, fix the algorithm
Commands allowed: MOVE_FORWARD()
REPEAT UNTIL (goalReached)
{
IF (CAN_MOVE(FORWARD))
{
MOVE_FORWARD
}
ELSE
{
IF CAN_MOVE(RIGHT)
{
ROTATE_RIGHT
MOVE_FORWARD
}
}
}
It will go on the black square and forever go into a square loop
# Insert your updated algorithm here:
REPEAT UNTIL (goalReached)
{
IF (CAN_MOVE(FORWARD) and NOBLACK_BOX(true))
{
MOVE_FORWARD
}
ELSE
{
IF CAN_MOVE(RIGHT)
{
ROTATE_RIGHT
MOVE_FORWARD
}
}
}
Create an algorithm that uses selection and/or iteration that will represent one player’s turn in a game.
During a turn, each player gets 4 attempts/chances to get the greates number possible.
During each attempt the player will use a random number generator to select a random number 1-10.
After the player has had 4 chances, their score is the greatest number they receieved from the random number generator, and their turn is over.
import random
def play_turn():
greatest_number = 0
for attempt1 in range(1, 5):
random_number = random.randint(1, 10)
print(f"Attempt {attempt1}: Random number is {random_number}")
if random_number > greatest_number:
greatest_number = random_number
print(f"Player's 1 score for this turn: {greatest_number}")\
great_number = 0
for attempt2 in range(1, 5):
random_num = random.randint(1, 10)
print(f"Attempt {attempt2}: Random number is {random_num}")
if random_num > great_number:
great_number = random_num
print(f"Player's 2 score for this turn: {great_number}")
if great_number > greatest_number:
print(f"Player 2 wins with the higher number of: {great_number}")
elif greatest_number > great_number:
print(f"Player 1 wins with the higher number of: {greatest_number}")
else:
print(f"Player 1 and 2 tied with a score of: {greatest_number}")
play_turn()
Attempt 1: Random number is 1
Attempt 2: Random number is 5
Attempt 3: Random number is 4
Attempt 4: Random number is 8
Player's 1 score for this turn: 8
Attempt 1: Random number is 8
Attempt 2: Random number is 8
Attempt 3: Random number is 6
Attempt 4: Random number is 4
Player's 2 score for this turn: 8
Player 1 and 2 tied with a score of: 8
Use the following flowchart to assist you: