Problem 1
sort 4 numbers from lowest to highest
nums = [4,1,3,2] # 4 numbers
for i in range(len(nums)-1): # looping w/ two variables
for j in range(i+1,len(nums)): # i,j indices where i < j
if nums[i] > nums[j]: # if the left element is greater than the right element, swap them
a = nums[i] # have to make a copy since we are rewriting nums[i]
nums[i] = nums[j] # swap
nums[j] = a # make nums[j] the copy
print(nums)
# overall not that efficient of an algorithm
# runs in O(N^2) time, but for a 4-element list this is just fine
[1, 2, 3, 4]
Problem 2
Benefits of simulations:
- Much less expensive than actually conducting the experiments
- Easier to repeat
- Safer to conduct
Drawback of simulations:
Simulations only provide estimated results from models, so conduct experiments will still produce the more accurate results.
Problem 3
Take for example the YouTube algorithm (obvious but good example). Such algorithms will take data from user browsing on their websites, such as what videos they watch, how long they watch, what videos are related to what other videos, and what they search. All of these factors will go into consideration in the algorithm to recommend the user other things that they may take interest in and engage them for longer browsing sessions.