This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First subarray is arr[l..m] | |
# Second subarray is arr[m+1..r] | |
def merge(arr, l, middle, r): | |
leftlength = middle - l + 1 | |
rightlength = r - middle | |
# create temporary arrays | |
left = [] | |
right = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### RUN THE CODE BELOW IN R. R-STUDIO IS THE RECOMMENDED IDE. BOTH R AND R-STUDIO ARE FREE. | |
######### QUESTIONS SHOULD BE POSTED TO PIAZZA | |
######### THE ACTUAL ASSIGNMENT BEGINS ON LINE 71 | |
#########Provided Code (some parts commented out because I didn't want it)######### | |
### Multilateral Development Institution Data | |
foo <- read.csv("https://tinyurl.com/yb4phxx8") # read in the data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def increment_max(lst, prevmax): | |
currentmax = prevmax | |
for i in range(len(lst)): | |
if sum(lst[i:len(lst)]) > currentmax: | |
currentmax = sum(lst[i:len(lst)]) | |
return currentmax | |
###test#### | |
sho = [-2,-3,4,-1,-2,1,5,-2] #max subarray has sum 7 at sho[2:6] | |
increment_max(sho, 7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def swap(lst,i,j): | |
lst[i],lst[j] = lst[j],lst[i] | |
def heapify(lst,n,i): | |
root = i # initialize root | |
left = 2*i + 1 #define left child | |
right = 2*i + 2 #define right child | |
if left < n and lst[root] < lst[left]: #if the left child is bigger than root, switch | |
root = left | |
if right < n and lst[root] < lst[right]: #if the right child is bigger than root, switch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def swap(lst,i,j): | |
lst[i],lst[j] = lst[j],lst[i] | |
def heapify(lst,n,i): | |
root = i # initialize root | |
left = 2*i + 1 #define left child | |
right = 2*i + 2 #define right child | |
if left < n and lst[root] < lst[left]: #if the left child is bigger than root, switch | |
root = left | |
if right < n and lst[root] < lst[right]: #if the right child is bigger than root, switch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
import matplotlib.pyplot as plt | |
import numpy as np | |
masterinterview = [] | |
masterhire = [] | |
for apps in range(1,1501): | |
interviewlist = [] | |
hirelist = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
def hatproblem(people): | |
hats = [i for i in range(1,people+1)] | |
hatsreplica = [i for i in range(1,people+1)] | |
random.shuffle(hatsreplica) | |
count = 0 | |
for i in range(len(hats)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def check(list): | |
for i in range(len(list) - 1): | |
if list[i] > list[i + 1]: | |
return False | |
return True | |
def badsort(list): | |
while not check(list): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
for i in range(100): | |
list = [int(100*random.random()) for i in range(100)] | |
sigma = random.randint(1,49) | |
sigma | |
low = np.percentile(list, 50-sigma) | |
high = np.percentile(list, 50+sigma) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def swap(lst, a, b): | |
lst[a], lst[b] = lst[b], lst[a] | |
def median(lst): | |
n = len(lst) | |
return sorted(lst)[n//2] | |
def partition(lst,low,high): | |
i = ( low-1 ) | |
pivot = lst[high] |
OlderNewer