Learning Objective:

  1. Express an algorithm that uses sequencing without using a programming language

Essential Knowledge and Vocab

  • Algorithms can be expressed in many ways and can be executed by programs that are implemented using programming languages.
  • Every algorithm can be constructed using combinations of sequencing, selection, and iteration

Variable- storing an actual value, the value of a variable within another variable, the result of an operation, or result of a procedural call

Algorithm- finite set of instructions that accomplish a specific task Steps:

  1. Sequencing- doing steps in order, doing the first step then the second then the third, etc.
  2. Selection- when the programmer decides between two different outcomes
  3. Iteration- when you repeat a step until a certain condition is fulfilled

Example

Most algorithms use a flow chart to demonstrate how the algorithm proceeds.

  1. Set largestNumber to 0
  2. Get next number in list
  3. If number is larger than largestNumber then set largestNumber to number
  4. If there are more numbers in list, go back to Step 2
  5. Display largestNumber

Sequencing: Steps 1-5 in order

Selection: Step 3

Iteration: Step 4

Practice: calculate and display average of 25, 43, and 18

num 1 ⟵ 25

num 2 ⟵ 43

num 3 ⟵ 18

average ⟵ (num 1 + num 2 + num 3)/3

Answer: 28.667

Practice: consider the following code segment, which uses the variables a, b, c:

a ⟵ 1

b ⟵ 2

c ⟵ 3

a ⟵ b

b ⟵ c

display(a)

display(c)

  • which is displayed as a result of running the code segment
  1. 1 1
  2. 1 2
  3. 2 3

Answer: 3

Practice: consider the following code segment

s ⟵ 10

l ⟵ 20

a ⟵ 30

y ⟵ 40

s ⟵ l

l ⟵ a

y ⟵ l

a ⟵ s

what is the value of a as a result of executing the code segment?

  1. 10
  2. 20
  3. 30
  4. 40

Answer: 2. 20 is the value

Arithmetic Operators

addition: a + b

subtraction: a - b

multiplication: a * b

division: a / b

MOD represent the Modulus operator. Returns the value after division: a MOD b. Like a remainder

ex: 

9 MOD 2 = 1

Goal: To evaluate expressions that manipulate strings

Vocab and Examples:

String: a sequence of characters

  1. Len: finds the number of characters in a string

    len("school")

    Result: "5"


  1. String concatenation: combines two or more strings into one

    concat("computers", "arecool" )

    Result: "computersarecool"


  1. Substring: a part of a existing string

    substring("APCSPrinciples", 3, 6)

    Result: "CSPrin"

    Starts at the 3rd character ("C") and takes 6 characters after that ("SPrin), then in all ("CSPrin")

Homework Hacks

numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []

for i in numbers:
    if (numbers[i] % 2 == 0):
        evens.append(numbers[i])

print(evens)
[0, 2, 4, 6, 8, 10]
  1. Sequence: The whole code running through all of the steps, starting with defining the lists, going through them to take the even numbers, then printing them Selection: selection is when the code selects the even numbers out of the list "if (numbers[i] % 2 == 0):" Iteration: the for loop is iteration because it repeatedly goes through the list and appends the even numbers from the list "for i in numbers:"
i = 1
starString = "*"
while i <= 5:
  j = 1
  while j <= i:
    print ("*", end= "")
    j += 1
  print ()
  i += 1
*
**
***
****
*****
  1. Sequence: all of the steps Selection: "while j <= i:" because they choose what j is Iteration: "while i<+ 5:" because the function repeats until they choose 5

3.3 Video 2 Hacks

Practice Problems

  1. given the following code segment below:

a ⟵ 7

b ⟵ 1

c ⟵ 3

d ⟵ 4

a ⟵ b

b ⟵ c + d

d ⟵ b

find the value for a, b, c, d a = 1 b = 7 c = 3 d = 7

  1. consider the following code segment:

hot ⟵ true

cold ⟵ false

cold ⟵ hot

hot ⟵ cold

what are the values of hot and cold after executing the code segment?

  1. the value of hot is true, the value of cold is true
  2. the value of hot is false, the value of cold is true
  3. the value of hot is true, the value of cold is false
  4. the value of hot is false, the value of cold is false

Answer: 1. Both are true

  1. Make TWO of your own code segments that contain at least 5 defined variables, then provide the answer and EXPLAIN why your answer is correct.

dog ⟵ yes

cat ⟵ no

cat ⟵ dog

owl ⟵ cow

Display dog Answer: yes the answer is correct because yes is assigned to dog in the beginning and it is never changed afterward


joselyn ⟵ 3 lina ⟵ 11 naja ⟵ 7 amitha ⟵ 1

joselyn ⟵ amitha naja ⟵ lina lina ⟵ amitha

Display each person's value Answer: Joselyn = 1 Lina = 1 Naja = 11 Amitha = 1

  1. Sequencing
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3      
num2 = num1 + num3      # num2 is now the new num1 + num3

Answer: num1 = 1 + 5 = 6 num2 = 6 + 5 = 11

3.4 Video 1 Hacks

String Homework


  • Test 1

    firstName <- "Bob" lastName <- "Smith" var <- substring(firstName, 1, 1) name <- concat(lastName, var) email <- concat(name, "@gmail.com") DISPLAY(email)

  • What would the result be?

    Hint: var = "B" name = "SmithB"

Answer: SmithB@gmail.com


  • Test 2

    word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 = 4 length2 <- len(word2)/3 = 3 first <- substring(word1, 2, len1) = ompu second <- substring(word2, len2+3, len2) = ook newWord <- concat(first, second)
    DISPLAY(newWord)

Answer: ompuook