3.3 Video 1 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]

The cell is a sequence, the for loop is an iteration since it's repeating an action, and the if statement is a selection since it's giving parameters to only print specific numbers.

i = 1
starString = "*"
while i <= 5:
  j = 1
  while j <= i:
    print ("*", end= "")
    j += 1
  print ()
  i += 1
*
**
***
****
*****

The cell is a sequence, the while i <= 5 is iteration since it tells the code to repeat until i = 5, an the while j <= i is selection since it gives values for j.

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 since a is assigned b and the value of b is 1 b = 7 since b is assigned the sum of c and d which is 3 + 4 which is 7 c = 3 since c is assigned as 3 d = 7 since d is assigned as b which was previously assigned as 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: since cold is assigned as hot which was assigned true and hot is assigned as cold which was assigned true

Making Questions

  1. Consider the following coding segment: a ⟵ 5\ b ⟵ 10\ c ⟵ 2\ d ⟵ 4\ a ⟵ b - a\ b ⟵ c + d\ d ⟵ a + b

What is the value of d?

Answer: 11; a is assigned as b - a which can be rewritten as 10 - 5 which can be simplified to 5, giving the value of 5 to a. The value of b is c + d which can be written as 2 + 4 which is simplified to 6. And since d is assigned as a + b, by using the most recent values of these variables you would get 5 + 6 which is simplified to 11

  1. Consider the following coding segment:

a ⟵ 1\ b ⟵ 2\ c ⟵ 3\ d ⟵ 4\ a ⟵ c**\ b ⟵ a + d\ d ⟵ a + b + c

What is the value of d?

Answer: 25; to get the value of a one must square c which whould make a = 9, by adding that to d one gets 9 + 4 which is 13, and to get the value of d one would add 9 + 13 + 3 which gives 25.

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

num1 = 6 since num2 + num3 can be rewritten as 1 + 5 and that simplifies to 6\ num2 = 11 since num1 + num3 can be rewritten as 6 + 5 which is 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; only the firstletter is taken from firstName and it's added to the end of lastName which gives SmithB and then @gmail.com is concatinated with the name which gives SmithB@gmail.com

Test 2

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)

Answer: ompuook; length1 can be written as 8/2 or 4 since "computer" is 8 letters long and length2 is 3 since it can be written as 9/3 which is 3. The variable first is ompu since it's 4 letters long and starts at the second letter and second is ook since it's 3 letters and starts at the 6th letter. And when these are added together one gets "ompuook"