3.3 Hacks Part 1

Label steps of sequence, selection, and iteration for the given examples (at least that’s how I interpreted the question)

Example 1:

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)

I’ll outline some lines:

  1. Assigns a list to numbers
  2. Defines evens as an empty list
  3. Uses a for loop to iterate through each element in numbers. Steps 4-5 repeated for each element in the list numbers.
  4. Checks if an element’s value divided by 2 has a remainder of 0 (using modulus)
  5. If the condition passes, this element is appended to evens
  6. print the evens list.

There is sequencing for all lines in this case, since the lines of code are executed in a certain order.

Selection is largely seen in lines 4-5, where elements from numbers are conditionally appended to evens (elements with values that have a remainder of 0 when divided by 2 are appended.)

Iteration is seen in Line 3, where associated lines of code are executed for each element in a list.

Example 2

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

Here’s line by line explanation

  1. Sets variable i to 1. This is likely an index.
  2. makes a string variable starString with “*”
  3. Will execute all associated lines of code while the index i is less than or equal to 5
  4. Sets variable j to 1. This is likely an index. j is reset to 1 each time line 3 is re-run
  5. Will execute all associated lines of code while j is less than or equal to i.
  6. Will print a “*”; the end= ““ prevents the print statement from creating a new line
  7. Increases the index j by 1. Keep in mind this change is undone each time line 3 is run.
  8. This print() statement essentially moves to a new line of code
  9. Increases the index i by 1.

Sequencing is seen in all lines of code since they are executed in a certain order

Selection is not largely seen in the code

Iteration is seen in Line 3 (which will execute all associated lines below until i is greater than 5) and in Line 5 (which executes associated lines 6-7 until j is greater than i)

3.3 Hacks Part 2

1) Given the code segment, give the values of a, b, c, and d

a ⟵ 7
b ⟵ 1
c ⟵ 3
d ⟵ 4
a ⟵ b
b ⟵ c + d
d ⟵ b

I’ll make a table of changes for each line | Line | Change | |-|-| | 1 | a is 7 | | 2 | b is 1 | | 3 | c is 3 | | 4 | d is 4 | | 5 | a is b (1) | | 6 | b is sum of c and d (7) | | 7 | d is b (7) |

a ⟵ 1
b ⟵ 7
c ⟵ 3
d ⟵ 7

2) Given the code segment: What are the values of hot and cold?

hot ⟵ true
cold ⟵ false
cold ⟵ hot
hot ⟵ cold

After true was assigned to hot and false was assigned to cold, hot8 (true) was assigned to cold, making both true. Assigning cold to hot (true to true) has no effect, so both are true (Answer is 1)

3) Make two code segments with at least 5 variable definitions. Provide the answer and explain reasoning

I think these are supposed to be sequencing challenges. OK here goes. For reference I’m doing these in Python.

var1 = 987
var2 = 121
var3 = 1337
var2 = var3 - 1211
var1 = var2 % 7
varsum = var1 + var2 + var3
print(varsum)

The first three lines are rather self explanatory. I assigned these variables some values.

Line 4 is simple enough. The new value of var2 is the value of var3 minus 1211, which is 1337 - 1211 = 126. I used a calculator to avoid arithmetic errors.

Line 5 involves taking the remainder of 126 divided by 7, since this is a modulus function. Oddly enough, 126/7 is exactly 18, so the value of 0 is assigned to var1.

Line 6 involves adding all the variables (taking into account their new values). This is as simple as 0 + 126 + 1337 = 1463.

Line 7 just prints the sum I calculated.

Well here comes another challenge, which I’ll do in pseudocode to demonstrate functionalities from 3.4

bricks ← "100 million"
milesDriven ← "fifty thousand"
natlDebt ← "thirty-one trillion dollars" 
worldPop ← "8 billion"
part1 ← substring(milesDriven, 1, 5)
part2 ← substring(bricks, 5, 7)
part3 ← substring(worldPop, 3, 7)
wordSalad ← concat(part1, part2, part3, natlDebt)
DISPLAY(wordSalad)

Lines 1-4 are simple enough. Assign some values to four variables.

Line 5 assigns the first 5 characters of milesDriven (which is “fifty”) to part1

Line 6 assigns 7 characters from bricks to part2, starting with the fifth character from bricks. This is “million”

Line 7 assigns 7 characters from worldPop to part3, starting with the third character. This is “billion”

Line 8 is a monstrosity. It concatenates each of the part variables and the natlDebt variable. This is assigned to the wordSalad variable.

Line 9 outputs wordSalad, which is “fifty million billion thirty-one trillion dollars”

To be honest, I don’t know if it’s possible to run commands inside the concat parameters, so I assigned each of the substring commands its own variable. In fact, I didn’t find any of the substr or concat commands on the exam reference sheet, which is very peculiar. Of course that only resulted in my code failing to function on an online pseudocode tester.

Sequencing Challenge: Find num1 and num2

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

3.3 Hacks Part 3

Looks like this doesn’t exist.

3.4 Hacks

Test 1. Find the output.

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

Test2. Find the output

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)

-That’s better. The value of length1 is 4, so it will take 4 characters from “computer” starting from the second character. This will yield “ompu” which is assigned to first

-Line 8 prints newWord, which is ompuook.