Section 3.9-3.11 Notes
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
# 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)
To find 69 using sequential search, the list would be iterated 7 times. Using a binary search instead, this process would be much more efficient. The search would start from the middle index, then going to the first and last index and dividing by two. This process is repeated until the number 69 is found.
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. Binary search takes out half of the possibilities every iteration. Since you start at the middle index, you can either choose to pick the group that is greater than the middle index, or the group that is lower. As a result, you will rule out half of the known possibilities every single time you make a cut.
9
[64,36,16,11,9] Explain which number you are finding, how many check it would take, and make a binary search tree
If 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.
See comment for image