Important Vocab/Notes

  • Indexes: the number that represents an element in a list
  • Element a integer or string in a list.
  • Algorithms can be written in different ways and still accomplish the same tasks
  • Algorithms that appear similar can yield different side effects or results
  • Some conditional statements can be written as equivalent Boolean expressions
  • Some Boolean expressions can be written as equivalent conditional statements
  • Different algorithms can be developed or used to solve the same problem
  • Algorithms can be created from an idea, by combining existing algorithms, or by modifying existing algorithms.

1. Write this Boolean statement in the form of a conditional (if/else) statement:

stayInside⟵((isCold) OR (isRaining))

{
if(isRaining){
        stayInside <- true
    }
    if(isCold){
        stayInside <- true
    }
else {
        stayInside <- false
    }
    }
  • This code is saying that if it is raining or cold then you stay inside. If it is not cold or rainy then you dont stay inside.

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
attempts = []

i = 1 
while i <= 4:
    attempts.append(random.randint(1,10))
    i = i + 1
    
print(attempts)
print("Maximum:", max(attempts))
[2, 7, 3, 9]
Maximum: 9
  • This code generates 4 values in the range 1-10 and then from there they choose the highest value out of those 4 values.

3. Create an algorithm that will allow the arrow to reach the gray square:

{
if canmoveFoward{
    moveFoward
}
else{
    canturnRight{
     turnRight
    }
    canturnLeft{
     turnLeft
    }   
}
}
  • This code has you move foawrd until you are not able to and then once you can you have to turn right or left.

4. Make a binary search tree of different the list [1,2,3,4,6,9,11,69]

  • This tree starts at 6 and then on each side it does the same process until you get every value. The process is adding the highest and lowest index and then dividing by 2 and then you choose the corresponding number to that index.

5. Explain thorughly how to find the number 69 in the list above (use key words)

  • Using sequential search you would go through the list comparing 69 to every value in the list until the number (69) matches 69. In the order this list is it will take 7 iterations to find 69
  • Using binary search you would start in the middle, you can find this by adding the first and last last ((1+8)/2=4.5). You have to round up to 5 for the index, so you start at number 6 in the list. You do this on either side until you get your desired value. (You would want to get 8 because the index of 8 = 69)

6. Make a diagram explaining how you found the list (not tree, include equation)

  • You first want to find the middle so you do that. Once you do that you do the same process again and you get 11, which is the number wanted.
  • Original: ["store","Market","Walmart",Target","Ralphs"]
  • Correct Order: ["Market","Ralphs","store","Target","Walmart"]
  • Alphabetical order would be the best way to put it
  • Binary search is more efficient than sequential search because sequential search goes one by one in the list comparing each value to the value you are looking for. Binary search on the other hand basically divides the amount of answers it could be after each step, making it a lot faster to find a certian number in a large list.

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 36 it would take 2 checks. You would start at 16 because it is the middle index. Then you would do 1 + 3 = 4 and then divide by 2 to get 2 for the index. Index 2 is the number 36, so we would stop there.

Blog/Final Thoughts

  • I learned about conditional statements vs boolean
  • Furthed my knowledge on algorithms
  • Experienced more with different types of algorithms
  • I learned about binary search and sequential search
  • I learned that binary search is more effiecient than sequential search