Home API Notes

Hacks

AP Classroom. Provide answers and thoughts on theoretical question form college board Video in section 4.3. They start at about the 9 minute mark.

  • Example 1

If we run Process X on Processor 1 and Process Y on Processor 2 in parallel, we can complete all three processes in a minimum of 50 seconds. During the first 50 seconds, Process X runs on Processor 1 while Process Y runs on Processor 2 for 10 seconds. After Process Y finishes, Process Z can run on Processor 2 for 30 seconds while Process X is still running on Processor 1. Finally, after Process Z completes, Process X takes an additional 10 seconds to finish running. Thus, the entire sequence of processes can be completed in 50 seconds, which is the time it takes for Process X to run.

  • Example 2

Running Process A and Process B sequentially would take 70 seconds (25 seconds for Process A and 45 seconds for Process B). However, running them in parallel would take only 45 seconds, which is the duration of Process B, as it takes longer than Process A. Hence, parallel processing reduces the total processing time by 25 seconds.

Data Structures. Build a List Comprehension example

  • list = [calc(item) for item in items]

Starting code

fruits = ["apples", "bananas", "pears", "oranges", "grapes"]

grocery_num = 0

for i in fruits:
    grocery_num += 1

print(grocery_num)
5

Modified code using list comprehension

fruits = ["apples", "bananas", "pears", "oranges", "grapes"]

# Use a list comprehension to create a new list containing all the elements in the fruits list
# Use the len() to get the length of the new list - equivalent to the number of items in the original fruits list
grocery_num = len([fruit for fruit in fruits])

# Print the result
print(grocery_num)
5