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_change(coin_values, total, coin_count, used_coins): | |
for cents in range(total+1): | |
count = cents | |
coin = 1 | |
for coin_value in coin_values: | |
if coin_value <= cents: # Could use a lambda here | |
if coin_count[cents-coin_value] + 1 < count: | |
count = coin_count[cents-coin_value] + 1 | |
coin = coin_value | |
coin_count[cents] = count |
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 permutation(str): | |
_permutation('', str) | |
def _permutation(prefix, str): | |
n = len(str) | |
if n == 0: | |
print prefix | |
else: | |
for i in range(0, n): | |
_permutation(prefix + str[i], str[0:i] + str[i + 1:n]) |
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
class MaxHeap(object): | |
heap_values = None | |
def __init__(self): | |
self.heap_values = [] | |
def get_max(self): | |
return self.heap_values[0] | |
def insert(self, insert_me): |
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 in_order_recursive(input, index): | |
if index >= len(input) or input[index] == None: | |
return | |
in_order_recursive(input, (index * 2) + 1) | |
print input[index], | |
in_order_recursive(input, (index * 2) + 2) | |
in_order_recursive([3,2,1,1,1,1,0,1,0], 0) | |
# 1 1 0 2 1 3 1 1 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
def pre_order_recursive(input, index): | |
if index >= len(input) or input[index] == None: | |
return | |
print input[index], | |
pre_order_recursive(input, (index * 2) + 1) | |
pre_order_recursive(input, (index * 2) + 2) | |
pre_order_recursive([3,2,1,1,1,1,0,1,0], 0) | |
# 3 2 1 1 0 1 1 1 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
def bf_traversal(input): | |
queue = [] | |
queue.append(0) # Add root | |
index = 0 | |
while len(queue) > 0 and index < len(queue): | |
node = queue[index] | |
print input[node], | |
left = (index * 2) + 1 | |
right = (index * 2) + 2 | |
if left < len(input) and input[left] is not None: |
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 time | |
current_milli_time = lambda: int(round(time.time() * 1000)) | |
def fib_recursive(input): | |
if input == 0: | |
return 0 | |
if input == 1: | |
return 1 | |
return fib_recursive(input - 1) + fib_recursive(input - 2) |
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 java.util.*; | |
public class BSTDistance | |
{ | |
public static void main(String args[]) { | |
int[] input = new int[]{5,6,3,1,2,4}; | |
int result = bstDistance(input, 2, 4); | |
System.out.println(result); | |
} | |
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 java.util.*; | |
import java.util.stream.Collectors; | |
public class MovieNetwork { | |
public static void main(String[] args) { | |
Movie A = new Movie(0, 11.2f); | |
Movie B = new Movie(1, 2.4f); | |
Movie C = new Movie(2, 3.6f); | |
Movie D = new Movie(3, 4.8f); |
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 java.io.*; | |
import java.util.*; | |
/* | |
* Given the input string, count the number of unique words found | |
*/ | |
public class WordCount { | |
private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'-"; | |
private static String input1 = "After beating the eggs, Dana read the next step:"; |
OlderNewer