This Group: Theo, Haeryn, Ellie, and Kaylee

3.3 Video 1 Hacks

Show two examples and label which one is sequence, selection, iteration

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

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

for i in numbers:
    if (numbers[i] % 2 == 1):
        odds.append(numbers[i])

print(numbers[2])
print(evens)
print(odds)
2
[0, 2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]

Explanation

What is happening in this code is that print(numbers[2]) is printing the numbers list but only the item with the value of 2, so it will output 2. Printing evens is making a math function and then if the remainder is 0 it will make a new list. Printing odds is making a math function and them if the remainder of that when divided by 2 is 1, then it makes a new list with odd numbers from the original list

Answers 1

All the steps combined are sequencing

The step "for i in numbers:" is iteration because they go through all the numbers.

"if (numbers[i] % 2 == 0)" is selection because they sort each number to find the even ones.

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

Explanation

i is set to 1 and then another variable is set to a star. Then another variable is j which is also set to 1. The function keeps repeating itself over and over until i is not equal to or less than 5.

Answers 2

All the steps are a sequence

"While i <= 5:" is iteration because they repeat until i reaches 5

"While j <= i:" is selection because this is where they decide what j is

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: 7

My Answer: a: 1 b: 7 c: 3 d: 7

Explanation

First a is set equal to b so a is 1. then b becomes the sum of c and d so b becomes 7. Then d is set equal to b so then d becomes 7 since b was redefined in the last line of code. c is never redefined so it stays the same.

Click for the answer! 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

My Answer: 1

Explanation

This is because cold becomes set equal to hot, so we know both variables are equal to true. Since the variable cold has been redefined, when hot is set equal to cold, so both variables are equal to hot, so it is option 1.

Click for the answer! 1. the value of hot is true, the value of cold is 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.

Given the following code segment below:

w ⟵ boat

a ⟵ walk

t ⟵ sea

e ⟵ separate

r ⟵ cane

w ⟵ t r ⟵ e t ⟵ r

What is the value of w?

Click for the answer! sea This is the answer because the value of w is only changing once, and that it is becoming equal to the value of t, which is sea.
a = "apple"
b = "ball"
c = "cake"
d = "dessert"
e = "elephant"
 
a=c
c=a
b=d
d=a
e=b

print(a)
cake
Click for the answer! cake because a is only being changed once and it is being set to c which is cake. The output would be cake because a is only redefined once when it is set equal to c. That means that a is set to cake and when a is printed it will output cake.
  1. Sequencing
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3      
num2 = num1 + num3      # num2 is now the new num1 + num3

What is the value of num1 and num2?

My Answers: num1 = 6 num2 = 11

Click for the answer! num1 = 6, num2 = 11. This is because num1 is redefined as the sum of num2 and num3, so then it becomes 5+1 so num1 is equal to 6. num2 is defined as the sum of num1 and num3, but since num1 was redefined we have to use the new value. Then it will be 6+5, which is 11.
Score: 1/1

3.4 Video 1 Hacks

String Homework


  • Test 1

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

  • What would the result be?

Explanation

You can see the output of each one after the line of code. When var is assigned the output will be just B because it is taking the substring of "Bob". name is assigned lastName and var so it is combining them to make SmithB. Then email is combining name and @gmail.com so the final output SmithB@gmail.com

My Answer: SmithB@gmail.com

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


  • Test 2

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


My Answer: ompuook

Explanation

You can see the outputs after each line of code. Length 1 is taking the length of word 1 which is 8 and dividing it by 2 to get 4. Length 2 is taking the length of word 2 and dividing it by 3 to get 3. The variable first is taking the 2nd letter to the 4th letter of word1 so the output will be ompu. The variable second substring too word2 and then found the length and added 3 to it to get 6 and get the other character of 3. We have to reverse it to get [3,6] so the output will be ook. All this is assigned to another variable called newWord and then printed to get ompuook.

Answers

Test 1

  • Result: "SmithB@gmail.com"

    Test 2

  • Result: "ompuook"