Skip to content

Instantly share code, notes, and snippets.

View mrchocoborider's full-sized avatar

Nathan Johnson mrchocoborider

View GitHub Profile
@mrchocoborider
mrchocoborider / count_sort2.py
Created September 10, 2018 18:58
Interviewcake.com algorithmic pattern practice #2, Parker's solution.
def sort_scores(unsorted_scores, highest_possible_score):
# List of 0s at indices 0..highest_possible_score
score_counts = [0] * (highest_possible_score+1)
# Populate score_counts
for score in unsorted_scores:
score_counts[score] += 1
# Populate the final sorted list
sorted_scores = []
@mrchocoborider
mrchocoborider / count_sort.py
Created September 6, 2018 02:58
Interviewcake.com algorithmic pattern practice #2, my solution.
#define the function
def sort_scores(us, hps):
#Initialize the buckets array, a 0 for each entry from 0-100 (the highest possible score)
counter = [0] * (hps + 1)
#loop through the unsorted scores and add one to the counter array where
#the index is equal to the value of the score.
for i in us:
counter[i] += 1
#Initialize sorted list with the same length as the unsorted list
@mrchocoborider
mrchocoborider / greedy2.py
Last active September 6, 2018 03:01
Interviewcake.com algorithmic pattern practice #1, Parker's solution.
def get_max_profit(stock_prices):
if len(stock_prices) < 2:
raise ValueError('Getting a profit requires at least 2 prices')
# We'll greedily update min_price and max_profit, so we initialize
# them to the first price and the first possible profit
min_price = stock_prices[0]
max_profit = stock_prices[1] - stock_prices[0]
# Start at the second (index 1) time
# We can't sell at the first time, since we must buy first,
# and we can't buy and sell at the same time! # If we started at index 0,
@mrchocoborider
mrchocoborider / greedy.py
Last active September 6, 2018 02:58
Interviewcake.com algorithmic pattern practice #1, my solution.
#Set the max profit to -infinity, because if the first possible trade is a loss (negative #)
#we still want to keep track of it, and since we're checking if the potential profit is greater,
#-infinity is guaranteed to be smaller than any possible loss.
mp = float('-inf')
def get_max_profit(sp):
for i, s in enumerate(sp):
for j in range(i + 1, len(sp)):
p = sp[j] - sp[i]
if p > mp:
@mrchocoborider
mrchocoborider / .block
Last active July 26, 2018 21:44
Neighborhood Fruit Trees
license: mit
scrolling: yes
@mrchocoborider
mrchocoborider / .block
Last active July 26, 2018 22:10
WWSD #2: Leaflet Basic Styling
license: mit
@mrchocoborider
mrchocoborider / .block
Last active July 26, 2018 21:40
WWSD #1: Leaflet starter
license: mit
scrolling: yes
@mrchocoborider
mrchocoborider / .block
Last active July 26, 2018 21:03
Sf Parks with proximity detection
license: mit