Unit 3 Lesson 8 & 10 Hacks and Notes
Iterations and Lists
Notes:
- Iteration: a repeating portion of an algorithm, repeats a specified number of times or until a given condition is met
- Iteration Statements: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
- Repeat Until: if the condition evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop.
- Traversing Lists: where all elements in the list are accessed, or a partial traversal, where only a portion of elements are accessed (can be a complete traversal)
- Complete Traversal: All elements in a list are assessed
- Partial Traversal: Only a given portion of elements are assessed
- Iterative Traversal: When loops are used to iterate through a list and to access each single element at a time.
- insert( ) allows a value to be inserted into a list at index i
- append( ) allows a value to be added at the end of a list
- remove( ) allows an element at index i to be deleted from a list
- length( ) returns the number of elements currently in a specific list
Hacks 3.8.1
- Define an iteration
Part of an algorithm that repeats until a condition is met.
- Make your own example of an iteration with at least 4 steps and a stopping condition (similar to mine that I did)
Wrapping Christmas Gifts:
i. there are n gifts that need to be wrapped
ii. grab gift to wrap
iii. grab wrapping paper
iv. wrap gift
v. repeat steps 2-4 until all gifts are wrapped
- Program a simple iteration
schedule = ["calc", "bio", "engineering", "csp"]
i = 0
for x in schedule:
print(schedule[i])
i+=1
for x in range(10, 0, -1):
print(x)
- Using while loop, make a list of numbers which will form an output of 3,16,29,42,55,68,81
numbers = [3, 16, 29, 42, 55, 68, 81]
x = 0
while x < len(numbers):
print(numbers[x])
x=x+1
nums = ["10", "15", "20", "25", "30", "35"]
potentialMin = nums[0]
for item in nums:
if item < potentialMin:
potentialMin = item
print("The minimum number in the list is:", potentialMin)
-
Look at the APCSP Reference Sheet and take notes about ones you don't understand (at least 4)
-
Assign the value of x to aList[i]
aList[i] <- x
-
Any values in aList at indecies >= i are shifted one position to the right. Length of list is increased by 1, value is plaed at index i in aList
insert(aList, i, value)
-
Appends aList in increased by 1, and value placed at end of aList
append(aList, value)
-
Remove the item at index i in aList and shifts to the left any values at indices greater that i. The length of aList is decreasing by 1
remove(aList, i)
-
- List's Quiz
import getpass, sys
import random
def ask_question (question, answer):
print(question)
ans = input(question)
print(ans)
if ans == answer:
print("Correct!")
return 1
else:
print("Wrong")
return 0
question_list = ["What allows a value to be inserted into a list at index i?" , "What allows an element at index i to be deleted from a list?" , "What returns the number of elements currently in a specific list?" , "What allows a value to be added at the end of a list?"]
answer_list = ["index()", "remove()", "length()" , "append()"]
# Set points to 0 at the start of the quiz
points = 0
# If the length of the quiz is greater than 0, then random questions will be chosen from the "question_list" set
while len(question_list) > 0:
index = random.randint(0, len(question_list) - 1)
# The points system where a point is rewarded for each correct answer
points = points + ask_question(question_list[index], answer_list[index])
# If a question or answer has already been used, then it shall be deleted
del question_list[index]
del answer_list[index]
# Calculating score using the points system and dividing it by the total number of questions (6)
score = (points / 4)
# Calculating the percentage of correct answers by multiplying the score by 100
percent = (score * 100)
# Printing the percentage, and formatting the percentage in a way where two decimals can be shown (through "{:.2f}")
print("{:.2f}".format(percent) + "%")
# Adding final remarks based upon the users given scores
if points >= 5:
print("Your total score is: ", points, "out of 4. Amazing job!")
elif points == 4:
print("Your total score is: ", points, "out of 4. Not too bad, keep on studying! " )
else:
print("Your total score is: ", points, "out of 4. Its alright, better luck next time!")