Lesson 2 3.3-3.4 Notes and Hacks
Joselyn Anda
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:
- Sequencing- doing steps in order, doing the first step then the second then the third, etc.
- Selection- when the programmer decides between two different outcomes
- 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.
- Set largestNumber to 0
- Get next number in list
- If number is larger than largestNumber then set largestNumber to number
- If there are more numbers in list, go back to Step 2
- 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
- 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?
- 10
- 20
- 30
- 40
Answer: 2. 20 is the value
Goal: To evaluate expressions that manipulate strings
Vocab and Examples:
String: a sequence of characters
Len: finds the number of characters in a string
len("school")
Result: "5"
String concatenation: combines two or more strings into one
concat("computers", "arecool" )
Result: "computersarecool"
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")
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)
- 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
- 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
- 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
- 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?
- the value of hot is true, the value of cold is true
- the value of hot is false, the value of cold is true
- the value of hot is true, the value of cold is false
- the value of hot is false, the value of cold is false
Answer: 1. Both are true
- 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
- 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)