Unit 3: Algorithms Binary/Data Terms

Variables:An abstraction inside a program that holds a specific value or meaning defined by the programmer.

Data Types:

Integer- highScore (involves math, save as integer or numbers)

String- firstName (name is text, so it is a string)

Boolean- isSunny (2 options, true or false)

String- phoneNumber (no math, just numbers)

Arithmetic Operators

Plus indicated addition (a + b)

Minus indicates subtractions (a - b)

Asterisk indicated multiplication (a * b)

Slash indicated division (a / b)

Managing Complexity with Variables

Lists: Allows you to complete a process for each value in the list, or store multiple values to one variable.

2D Lists: Array within an array. You can pull single arrays through using index and variable commands.

Ex: T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

Dictionaries: Allows the storage or data keys and values (assigning a certain piece of data to fit the category it is assigned)

Class: A particular data structure.

Algorithms: An algorithm is a finite set of instructions that accomplish a specific task, us as humans, do algorithms on a daily basis.

Sequence: A specific order a process is completed, which impacts the output.

Selection: Programmer decides between 2 different functions

Iteration: Repetition of a process

For loop: repeats a section of code a set number of times

While loop: repearts a section of code an unknown number of times, until the code is told to break

Expressions: A piece of syntax in coding

Comparison Operators

Operators used in programming languages which compare two different values. These can often be used to set up statements which return a boolean value (true/false), which can be stored in variables. All examples below are in Python and were checked using the bool() command, which returns the True/False output.

== : checks if 2 values are equal to each other (ex. “Hrar?” == “Hrar?” OR 1 == 1.0)

!=: checks if 2 values are NOT equal to each other

<, <=, =>, >: checks if left value is less than (and equal to) or greater than (and equal to) the right value

in/not in : Checks if the specified value is an element in the specified list. in returns true if the element is in the list; not in returns true if the element is not in the list. (Ex. List = [1, 3, 5, 7, 9, “hi”])

1 in List

3 in List

5 in List

“hi” in List

“What” not in List

“Why” not in List

2 in List

4 in List

6 in List

9 not in List

7 not in List

“Weird” in List

Booleans Expressions and Selection

The value of a boolean variable (True/False) can be used as conditions in selection (often known as if statements). The condition of the if statement is the boolean variable; if its value is True, the condition passes. A possible function of this is in Python code below

//Assume the grade was calculated by a computer program

//The bool command is here to make a boolean variable

//In a larger computer program, the value was likely

//generated by previous commands

Grade = 89.49

isA = bool(Grade >= 90)

if isA:

print(“Yay!”)

else:

print(“hmm”)

OUTPUT: hmm

Booleans Expressions and Iteration

Certain forms of iteration (specifically while loops in Python) can use a boolean variable as a condition (similar to selection commands). As you may expect, the while loop executes commands until the value of the variable is false. In terms of a computer program, this could perhaps be used to execute commands critical for maintaining a certain process until that process is no longer needed. This could also potentially be used to periodically send notifications until a certain condition is met (ex: an assignment is submitted)

Here’s some informal python code:

num = 10

correct = bool(num > 0)

while correct:

print(num)

num = num - 1

correct = bool(num > 0)

OUTPUT: 10 9 8 7 6 5 4 3 2 1

Truth Tables: A table for a logical operator (ex: AND, OR, XOR) containing each variable and all possible input and output results of that operator.

AND TABLE: All inputs must be true to return output of true.

Input 1 Input 2 Input 3
False False False
False True False
True False False
True True True

OR TABLE: At least one input must be true to return output of true.

Input 1 Input 2 Input 3
False False False
False True True
True False True
True True True

XOR TABLE: Exactly one input must be true to return output of true.

Input 1 Input 2 Input 3
False False False
False True True
True False True
True True False

Characters

The units which make up a string. These can be letters, numbers, special symbols (!@#$%^&*), or even spaces. Regardless, if one types, each keystroke outputs one character.

Ex: “keyboard hi!!!” has 14 characters: 10 letters, three exclamation marks, and a space.

Strings

A variable data type which consists of a combination of letters, numbers, and other special characters (ex: @#$%^&*). They are seen inside quotation marks, and operators cannot affect any numbers in the string.

Ex: stringVar = “Hello World!”

“Hello World!” is a string.

Length

The number of elements in a list. Here’s an example in Python:

numList = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1]

This list has a length of 17 because it has 17 elements. Each element is separated by a comma.

Concatenation: the act of joining two strings into a single string. Often, these strings are printed to an output terminal/console. Here’s an example in pseudocode:

concat(“tri”, “llion”)

This concatenates the strings “tri” and “llion”, making a new string “trillion”.

Upper, Lower, Traversing Strings

The first two (upper and lower) seem to be referring to Python methods. They are quite simple. I think it would be easiest to explain with a demonstration:

String = “WhYaReWeDoInGtHiS”

print(string)

print(string.upper())

print(string.lower())

Output:

WhYaReWeDoInGtHiS

WHYAREWEDOINGTHIS

whyarewedoingthis

Relatively intuitive. The upper() method changes all lowercase letters in a string to uppercase, while the lower() method does the opposite. A potential reason for this would be ensuring uniform case (especially of uppercase when making important headings like page/screen titles) Traversing strings behaves highly similar to iterating elements in a list, except that instead of iterating through a list, it’s a string. Elements in a list are now the characters of a string. A potential application of this could be to search all strings for a specific substring. Here’s some example python code:

#Very informative example here: #https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/

String = “Thirty Trillion Dollars”

for i in String:

if i == “T”

print(“T detected!”)

Output:

T detected!

T detected!

Python If, Elif, Else conditionals;

Python if statements execute associated lines of code if the if condition is satisfied.

If the if condition is not satisfied, an Elif statement is executed as fallback… with another condition and associated commands

If all associated Elif conditions are not satisfied, there can be an else statement which unconditionally executes associated commands.

Ex: input = input(“enter a string”)

if input == “Hi”:

print(“Hello World!”)

print(“How are you doing today?”)

elif input == “Why”:

print(“That’s a good question to ask about everything that happens.”)

elif input == “Who are you”:

print(“I’m an anonymous guest. Pleased to meet you.”)

else:

print(“Have a great day!”)

This code uses if statements and elif statements to return strings for three special inputs (“Hi,” “Why,” “Who are you”). If the input were any of those strings, the associated command would be executed. Otherwise, the command associated with the else statement (which prints “Have a great day!”) would be executed.

Nested Selection Statements: When a program can choose from many paths to lead to a result, think of flowcharts where one condition must be satisfied before another can be picked Python For/While loops with Range with List: There are two ways loops can be used one is with a range and the other is with a list. When using the range method a variable is iterated a set number of times based on the range (typically an integer value), while with the list method the loop is iterated based on the length of a list.

Range:

i = 0

while i < 5:

print(i + 1)

i += 1

Output: 1, 2, 3, 4, 5

List:

list = [“a”, “b”, “c”]

i = 0

while i<len(list):

print(list[i])

i = i + 1

Output: a, b, c

Combining loops with conditionals to Break: Makes it so that the loop will stop as soon as a condition is met

Continue: A statement used in while and for loops which starts the code from the beginning, in the image below you can see how the “h” in “Python” was not printed since the continue statement skipped the print(letter) and made the code start from the beginning

for letter in “python”:

if letter == “n”:

	continue

print(letter)

Output: pytho

Procedural Abstraction: A model of what the code should do but it doen’t tell how to do it. It’s basically using a procedure to name the idea.

Python Def: It defines a function and makes code simpler by allowing for the code in the function to be called

Procedures: Code that doesn’t return a value but allows for a specific task to be completed

Parameters: Values that are passed into functions when they are defined in addition(a + b), a and b will be given a value and that value is a parameter

Return Values: Values that are outputed when a function is completed. An example of this was when we were using console.log(“message”) when testing in AppLab