IF (isCold or isRaining) {
stayInside ⟵ True
}
ELSE {
stayInside ⟵ False
}
2
Create an algorithm that uses selection and/or iteration that will represent one player’s complete turn.
- During a turn, each player gets 4 attempts/chances to get the greatest number possible.
- During each attempt, the player will use a random number generator to select a random number from 1 to 10.
- After they have had 4 chances, their score is the greatest number they received from the random number generator, and their turn is over.
import random
numAttempts = []
i = 1
while i <= 4:
numAttempts.append(random.randint(1,10))
i = i +1
print("Attempts:", numAttempts)
print ("----------------------------------")
print("Maximum number from player:")
print(max(numAttempts))
for x in range(0, 3):
move forward
turn right
for x in range(0, 5):
move forward
turn right
for x in range(0, 2):
move forward
# this wouldn't be the fastest way for the robot to get to the other side but
# it is much more efficient coding wise
{
if CANMOVEFORWARD{
moveForwards
}
else{
if CANTURNRIGHT{
turnright
}
if CANTURNLEFT{
turnleft
}
}
}
5
Explain thoroughly how to find the number 69 in the list above (use key words)
- Using sequential search we would iterate through the list and compare each number until the number 69 matches, this would take 7 iterations so a binary search would be a faster way to do this
- using binary search, you would start from the middle index, then you continue to the next numbers by looking at the first and last index, and dividing by two. You keep following through until you get 69.
8
Explain why Binary Search is more efficient than Sequential Search
- Binary search is more efficient because it moves exponentially while sequential search moves through the list one by one.In big data sets, binary search would rule out half the numbers in one iterations, while sequential search has to go through every possibility. Therefore, binary search is far faster and efficient.
9
[64,36,16,11,9] Explain which number you are finding, how many check it would take, and make a binary search tree
- I am searching for the number 11, which would take two iterations. You would start at 16, the middle index. Then you would do 3+5/2 to get 4, so the next middle index would be 11, which is the number we want to find.