3.3 - 3.4 hacks and notes
for week 2
notes
An algorithm is a finite set of instructions that accomplish a specific task, us as humans, do algorithms on a daily basis.
Sequencing is doing steps in order, for example, doing the first step then the second then the third, etc.
Selection is when the programmer decides between two different outcomes.
Iteration is when you have to repeat a step until that condition is fulfilled.
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)
#comments for top code
# the code itself is sequencing (doing all orders in a specific order)
# the if statement is selection as it only runs if the if statement is met and sorts through numbers
# the for statement is iteration because it repeats the steps in it for i in numbers
i = 1
starString = "*"
while i <= 5:
j = 1
while j <= i:
print ("*", end= "")
j += 1
print ()
i += 1
#comments for bottom code
#the code itself is sequencing (doing all orders in a specific order)
#while j = 1 is selection because it is changing j
# while i loop is iteration because it repeats for if i is less than 5
# QUESTION 5
# code segment 1
numdog = 1
numcat = 0
numfish = 2
numparrot = 1
numbird = 1
numdog = numcat
numcat = numfish
numfish = numdog
print(numfish)
#this would print 0
#code segment 2
nummap = 1
numbooks = 2
numdrinks = 1
money = 770
numpens = 4
numpens = numbooks
money = money - numpens
print(money)
#assuming all are integers, money is 768
questions
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 ANS: 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 ANS: the value of hot is true, the value of cold is true
Sequencing (code below)
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3
num2 = num1 + num3
What is the value of num1 and num2? ANS: num1 = 6, num2 = 11
String Homework What does the code print?
Test 1 CODE BELOW 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? ANS: "SmithB@gmail.com"
TEST 2 CODE BELOW word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 length2 <- len(word2)/3 first <- substring(word1, 2, len1) second <- substring(word2, len2+3, len2) newWord <- concat(first, second) DISPLAY(newWord)
ANS: ompuook
after doing all of the questions, I found that the biggest area of weakness for me was vocabulary and making sure I understand exactly what each vocab term means. I just need to be more familiar with these kinds of terms.