Defining Algorithms

  • An algorithm is a process or set of rules to be followed through CODE
  • can be written in differeent ways and still accomplish the tasks
  • appear similar may yeild different results
  • conditionals can be written the same as boolean expressions

Conditionals vs Booleans

  • condition is a booleans expresson when an expression outputs either tru or false
  • boolean values can only ever hold true or false

3.8 part 3

Binary Search

  • determine the number of iterations to find value uin data set
  • Binary search: search algoristhm that finds the position of a target value w/in an array
  • an algorithm for iterating to find a value inside adata set
  • starts in the middle of a data set of numbers and eliminates 1/2 the data, process is repeated until value is found
def binary_search(arr, x):
    low = 0
    high = len(arr)-1
    mid = 0

    if low<=high:
        mid = (low + high) // 2 #integer part
        
        if x == arr[mid]:
            return mid
        elif x < arr[mid]:
            high = mid - 1
            return high
        else:
            low = mid + 1
            return low
    else:
        return -1

arr = [1,2,3,4,5,6,7,8,9,10,11]
x = 11

result = binary_search(arr, x)

if result != -1:
    print("Found at position : ",str(result))
else:
    print("Not in the array!")
Found at position :  6
import random # allows the program to generate random numbers

score = 0
iterate = 0

while (iterate < 3):
    var1 = random.randint(1, 20)
    if (var1 >= score):
        score = var1
    iterate = iterate + 1
else:
    print(f"Score: {score}")
Score: 12