Video 3.8-10 Hacks
Notes 3.8-10
Section 8
- Understanding What is Iteration
- Using for and while loops
Necessary Vocabulary
-
Iteration - Repitition of a Process
-
For Loop
- FOR LOOP repeats a function for a set number of times; I is the number of times repeated
-
While Loop
- The while loop is used to repeat a section of code an unknown number of times until a specific condition is met
-
Initialization - What sets the counter variable to a starting value. For example (var i = 0) represents an initial value of 0.
- Condition - Allows the computer to know whether or not to keep repeating the loop.
- increment/decrement - Modifies the counter variable after each repetition.
What is Iteration?
Iterative statements are also called loops, and they repeat themselves over and over until the condition for stopping is met.
In College Board's Pseudocode, the first is a REPEAT n TIMES loop, where the n represents some number.
The second type of loop is a REPEAT UNTIL (condition) loop, where the loop will continue to run until a condition is met.
Conceptually, a while loop is very similar to an if conditional, except that a while is continually executed until it's no longer true and an if is only executed once.
Section 10
- Understanding how to edit lists by adding, inserting, and removing data
- Using loops to iterate through lists and abstract data
- Determine the results or side effects of iteration statements
- Write sorting algorithms using iteration
Necessary Vocabulary
-
Indexing / List Index - The position of an element in a list, starting from 0
-
append
,remove
,pop
- Various methods, append adds an element to the end, remove removes at an index, and pop removes the last item.
-
Elements [in a list] - An item in a list.
-
Nesting - Having one data type or function inside another data type or function, such as lists or loops.
- array - Another name for a list, depends on the language
What are Lists?
- Lists are a collection of data in a sequence that is an iterable
- Each sequence is demarcated with an index, starting from 0. This is known as base 0 indexing
- In memory, it is stored as a variable name with multiple pointers to each variable stored in a certain order
- Lists can also be called arrays
- Lists have methods that act upon the list and change them. This moves the pointers within RAM to change the parts of the list.
Nested Lists
Uses of Nested lists
Placing lists within lists allows you to have arrays of similar data together, and create complexity.
Some uses include:
- Creating 2d Arrays
- Storing similar, but slightly different categories (sublists)
- Create a matrix
original_list = [1, 2, 3, 4, 5]
print("List before reverse : ",original_list)
reversed_list = []
for value in original_list:
reversed_list = [value] + reversed_list
print("List after reverse : ", reversed_list)
Excercise 2
- Task: Similar to insertion sort, this algorithm takes an unsorted array and returns a sorted array. Unlike insertion sort where you iterate through the each element and move the smaller elements to the front, this algorithm starts at the beginning and swaps the position of every element in the array
list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
print(f"array before sort {list}")
def insertion_sort(list):
for index in range(1,len(list)): # repeats through length of the array
value = list[index]
i = index - 1
while i >= 0:
if value < list[i]:
list[i+1] = list[i] # shift number in slot i to the right
list[i] = value # shift value left into slot i
i = i - 1
else:
break
IS = insertion_sort(list)
print(f"array after sort {list}")