Vocab

Term Definition
algorithms set of instructions that can accomplish a specific task
Pseudocode A blend of human language and coding format
Strings A string is a collection of characters. character can be anything from numbers, letters, spaces
String concatenation combining 2 or more strings to make a new strings in order to create a new string
Substrings A substring is a part of and already existing string
Boolean a data type with two possible values: true or false
Logical Operators these types of operators don't necessarily deal with equivalent/non-equivalent values
Conditionals determines which part of an algorithm are executed based on a condition being true or false
Nested Conditionals consist of conditional statements within other conditional statements
Lists sequence of variables
Index a term used to sort data in order to reference to an element in a list (allows for duplicates)
Elements the values in the list assigned to an index
condition is a boolean expression when an expression outputs either true or false
Selection process used in algorithms where a conditional if statement leads to one of two outcomes
Iteration process that allows certain things to happen until a condition is satisfied
Binary Search A search algorithm that locates the position of a target value within a sorted array by repeatedly dividing the search interval in half; can only be used when the list is sorted. Because of its divide-and-conquer approach, the amount of work required to find an item grows much more slowly with Binary Search than with Sequential Search. In fact, with this logarithmic behavior
Linear Search A process that checks every element in the list sequentially until the desired element is found or all elements have been searched. Can be used in any type of list. Has linear performance.
Bits most basic unit of information in computing and digital communications
byte a group of binary digits or bits (usually eight) operated on as a unit
hexadecimal relating to or using a system of numerical notation that has 16 rather than 10 as its base.
Procedural Abstraction provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it
Parameters input values of a procedure
Arguments specify the values of the parameters when a procedure is called
Libraries collection of precompiled codes that can be used later on in a program for sme specific well-defined operations
API contains specific direction for how the procedures in a library can behave or be used
Simulation a tested scenario used for viewing results/outputs to prepare for them in real world situations
heuristic solution an approach to a problem that produces a solution that isn’t necessarily optimal but can be used when normal methods to an optimal solution would take forever
algorithmic efficiency The ability of an algorithm to solve a problem in an efficient way
decidable problem problem in cs and mathematics for which an algo can be created that can always produce a correct answer
undecidable problem problem in cs and mathematics for which it is impossible to create an algorithm that can always provide a correct answer or solution.

Examples

Algorithm:

temp = int(input("Select a temperature from 0 to 99 degrees F"))
if (temp >= 90):
    print("It's too hot outside!")
else:
    if (temp >= 65):
        print("Sure I will play outside!")
    else: 
        print("It is too cold outside!")
Sure I will play outside!

Conditionals and Booleans:

IsHoliday = False
IsWeekday = True
if IsHoliday:
    driveWork = True
else: 
    if IsWeekday: 
        driveWork = True
    else: 
        driveWork = False
print(driveWork)
True

Selection and Iteration:

sum = 1
counter = 3
loop = 0
while (loop < 4):
    sum = sum + counter
    counter = counter + 2
    loop = loop + 1
else:
    print(sum)
25

List, Index, and Element :

fruits = ["apple", "grape", "strawberry"]
index = 1

print (fruits[index])
grape

String Concatenation and Substrings:

Noun = "Mr.Mortenson" 
Adjective = "handsome" 
Adjective2 = "Very" 
Verb = "is" 
abrev = Noun[0:6]
yoda = Adjective2 + " " + Adjective + " " + abrev + " " + Verb + "."
print(yoda)
Very handsome Mr.Mor is.

Logical Operators:

print("1 > 2 or 5 < 12:", 1 > 2 or 5 < 12)
# Output TRUE  using OR ^

# Output FALSE using NOT
print("7 > 8:", not 7 > 8)

# Output FALSE using AND
print("10 > 20:", 10 > 20 and False)
1 > 2 or 5 < 12: True
7 > 8: True
10 > 20: False

Binary Search:

def BinarySearch(array, x, low, high):

    # Repeat until the pointers low and high meet each other 
    while low <= high:

        mid = low + (high - low)//2 # find the middle (taking the higest index number plus the lowest and divided by two)

        if array[mid] == x: # if desired number is the middle is found return desired number (middle number) 
            return mid

        elif array[mid] < x: 
            low = mid + 1

        else:
            high = mid - 1

    return -1


array = [3, 4, 5, 6, 7, 8, 9]
x = 4

result = BinarySearch(array, x, 0, len(array)-1)

if result != -1:
    print("Element is present at index " + str(result))
else:
    print("Not found")

Nested Conditionals:

infoP = []

infoP.append({
    "Product": "Cheese",
    "Expired": True,
    "Cost": 15,
})

infoP.append({
    "Product": "Wine",
    "Expired": False,
    "Cost": 75,
})

def print_info(p_rec):
    if (p_rec["Expired"]): 
            print("\t", "This product is no good")
    else:
        if (p_rec["Cost"] > 50):
            print("\t", "This product is too expensive")
        elif (p_rec["Cost"] > 25 and p_rec["Cost"] <= 50):
            print("\t", "This is a regular product")
        else:
            print("\t", "This is a cheap product")

def print_data(p_rec): #formatting
    print("\t", "Product:", p_rec["Product"])  
    print("\t", "Expired:", p_rec["Expired"]) 
    print("\t", "Cost:", p_rec["Cost"])
    print_info(p_rec)
    print("\n")

def data_entry(): #defining the function that asks for user input
    product = input("What is the product? (one word)")
    expired_string = input("Is it expired? (True or False)")
    cost = input("How much did it cost(dont include $, round to nearest whole number)")

    if (expired_string == "True"):
        expired = True
    else:
        expired = False

    infoP.append({ #appends the user input to the dictionary
        "Product": product,
        "Expired": expired,
        "Cost": int(cost),
   
    })

def main():
    Continue = True #defining continue as true
    while Continue:
        inp = input("Would you like to add a product to the database, type no if you want to exit)?")
        if inp == "no":
            print("Come back again!")
            Continue = False
        elif inp == "add":
            data_entry()
        else:
            print("Invalid input. Please try again")

    length = len(infoP) #defines length as the number of records
    print("Total Number of Records: ", length) 
    for record in infoP:
            print_data(record)
            
 
main()

Simulation:

height = float(input("height in meters?"))

weight = input("weight in pounds?")

stuff = (2 * (height / 9.8))**(1/2)

print("It will take", stuff,"seconds for an object that weighs",weight,"pounds","to fall ",height,"meters in a vacuum")