Skip to content

Instantly share code, notes, and snippets.

@neila
neila / mergesort.py
Last active January 26, 2019 14:25
CS110 2.1 Preclass Code
# 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 = []
@neila
neila / cs112assignment1.R
Last active January 26, 2019 15:57
CS112 Assignment 1 Code
######### 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
@neila
neila / increment_maxsubarray.py
Created January 29, 2019 13:56
CS110 3.1 Preclass code
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)
@neila
neila / heapsort.py
Created January 31, 2019 13:54
CS110 3.2 Preclass Work
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
@neila
neila / heapq_max.py
Last active December 9, 2019 16:45
CS110 4.1 Preclass Work
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
@neila
neila / hire.py
Last active February 7, 2019 17:32
CS110 4.2 Preclass
import random
import math
import matplotlib.pyplot as plt
import numpy as np
masterinterview = []
masterhire = []
for apps in range(1,1501):
interviewlist = []
hirelist = []
@neila
neila / hat.py
Created February 7, 2019 16:26
CS110 4.2 Preclass
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)):
@neila
neila / badsort.py
Created February 12, 2019 13:53
CS110 5.1 Preclass
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):
@neila
neila / medianfind.py
Created February 12, 2019 17:35
CS110 5.1 Preclass
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)
@neila
neila / quicksort.py
Created February 21, 2019 14:23
CS110 6.2 Preclass
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]