Hacks

  • title: Homework Assignment for 3.3-4
  • tags: [hw]
  • toc: true
  • comments: true

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 = []


HACK

3.3 Video 1 Hacks

list = [1, 2, 3, 4, 5]
new_list = []
for num in list:
    if num % 4 == 2:
        new_list.append("new")
print(new_list)
['new']

Over here, we have an iteration through the "for num in list", as it is iterating through the entire list.

The "if num % 4 == 2" is a selection, because it is selecting a specific number.

Hack

i = 1

1 < 5

j = 1

1 <= 1

print –> *

#iterate to next i

i = 2

2 < 5

j = 1

1 <= 2

print –> *

2 <=2

print –> *

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

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
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.
  2. Sequencing num1 = 3 num2 = 1 num3 = 5 num1 = num2 + num3
    num2 = num1 + num3 # num2 is now the new num1 + num3 Hack: Problem 1-4 explanation

#1

a = 7

b = 1

c = 3

d = 4

a = 1

b = 3 + 4 = 7

d = 7

Answer: a = 1, b = 7, c = 3, d = 7

#2

hot = true

cold = false

cold = hot = true

hot = cold= true

Answer: cold = true, hot = true

#3 Own code segment with 5 variables a = 3

b = 7

c = 10

d = b + a

e = c / d

what is d * e ?

Explanation:

d = 7 + 3 = 10

e = 10 / 10 = 1

d * e = 10 * 1 = 10

#4

num1 = 3

num2 = 1

num3 = 5

num1 = num2 + num3 = 1 + 5 = 6

num2 = num1 + num3 = 3 + 6 = 9

What is the value of num1 and num2?

Click for the answer! num1 = 6, num2 = 11

3.3 Video 3 Hacks

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”


  • 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) —————————————————————

Answers

Test 1

  • Result: “SmithB@gmail.com”

Test 2

  • Result: “ompuook”

STRING HACKS

#1

firstName = “Vardaan”

lastName = “Yatavelli”

var = substring(firstName, 1, 1) = “V”

name = concat(lastName, var) = “YatavelliV”

email = concat(name, “gmail.com”) = vardsin28@gmail.com

#2

word1 = “computer”

word2 = “textbooks”

length1 = len(word1)/2 = 8 / 2 = 4

length2 = len(word2)/3 = 9 / 3 = 3

first = substring(word1, 2, len1) = ompu (starting at 2nd letter, 4 letters)

second = substring(word2, len2 + 3, len2) = ook (starting 6th letter, 3 letters)

newWord = concat(first, second)