What is an Algorithm?

An algorithm: a set of instructions that can accomplish a specific task.

An Algorithm Has Three Components

  1. Sequencing: Algorithms do tasks in the order of specification.

img1

  1. Selection: Helps choose two different outcomes based off a decision.

img1

  1. Iteration: If a condition is true, then the code can repeat.

img1

Algorithms Can Be Represented in Two Ways

  1. Flowcharts: Use shapes and arrows to represent the steps of an algorithm.

img1

img1

  1. Pseudocode: A blend of human language and coding format.

Hacks: Jamboard Flowchart

Click on this link for a group activity

Math Operations

  • Addition(+)
  • Subtraction(-)
  • Multiplication(*)
  • Division(/)
  • "MOD"(%) gets remainder

Order of Operations

  • Follow PEMDAS
  • Modulo step similar in order of operations as multiplication or division

Different Ways Values can be stored in variables

  • Number
  • Another variable
  • Result of operation
  • Result of procedure call
score = 0   # 1
score = newScore    # 2
score = newScore + 2    # 3
avgScore = allscores(20, 60, 80)    # 4

Sequencing

  • Important to keep track of variables
  • Every time a new value is assigned to a variable it overrides the old value

Hacks/Homework

Evaluate the variable "result".

Num1 = 50
Num2 = Num1 % 9 + 15
Num3 = Num2 / Num1 + ( Num2 * 2 )
Num4 = Num3 + Num1 / 5 - 10
Result = Num4 - Num2
# Result should be 20.4
Num1 = 10
Num2 = Num1 % 3 * 4
Num1 = Num2
Num3 = Num1 * 3
Result = Num3 % 2
# Result should be 0
valueA = 4
valueB = 90
valueC = 17
valueB = valueC - valueA
valueA = valueA * 10
if valueB > 10:
    print(valueC)
    # valueB is 86 so it should print valueC which is 17
type = "curly"
color = "brown"
length = "short"
type = "straight"
hair = type + color + length
print(hair)
# hair is straightbrownshort

Strings

String: a collection of characters(numbers, letters, spaces, special symbols, etc.)

Ways to use strings:

len(): find the length of a string

lower(): convert to lowercase

Pseudocode:

len(): returns the length of a string

concat(): returns a string made up of the concatenated strings ex. concat("string1", "string2") would return string1string2

substring(): returns the characters from the string beginning at the at the first position to the last so an example of this would be substring ("abcdefghijk", 2, 5) would print bcde (pseudocode starts at 1)

Hacks

Find the result of the following problems. Then convert the pseudocode to working python code using your knowledge of python string operators.

Problem 1

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

Problem 2

cookie = "choclate" 
cookie2 = "raisin" 
len1 = len(cookie) / 2 
len2 = len(cookie2) * 45 
vote1 = cookie + " vote " + str(len2)
vote2 = cookie2 + " vote " + str(len1)
votes = vote1+ " " + vote2
print(votes)
choclate vote 270 raisin vote 4.0