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 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 = [] |
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
#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 |
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 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, |
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
#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: |
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
license: mit | |
scrolling: yes |
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
license: mit |
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
license: mit | |
scrolling: yes |
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
license: mit |