Skip to content

Instantly share code, notes, and snippets.

View drem-darios's full-sized avatar

Drēm Darios drem-darios

View GitHub Profile
@drem-darios
drem-darios / coin_change.py
Created December 28, 2016 19:25
Coin change problem using dynamic programming
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
@drem-darios
drem-darios / permutation.py
Last active December 29, 2016 05:45
Example of how to print all permutations of a String
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])
@drem-darios
drem-darios / max_heap.py
Created January 8, 2017 06:42
Example of a max heap implementation
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):
@drem-darios
drem-darios / in_order_traversal.py
Last active January 11, 2017 02:02
In order traversal of a tree
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
@drem-darios
drem-darios / pre_order_traversal.py
Last active January 11, 2017 02:01
Pre order traversal of a tree
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
@drem-darios
drem-darios / breadth_first_traversal.py
Created January 15, 2017 06:45
Breadth first traversal of a tree
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:
@drem-darios
drem-darios / fib_fun.py
Last active January 15, 2017 08:18
Example of recursive and iterative Fibonacci sequence along with empirical analysis of run time
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)
@drem-darios
drem-darios / BSTDistance.java
Last active February 14, 2020 06:06
Given a list of values, create a BST that can determine the distance between two given nodes.
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);
}
@drem-darios
drem-darios / MovieNetwork.java
Last active November 2, 2020 07:20
Implement a function to return top rated movies in the network of movies reachable from the current movie.
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);
@drem-darios
drem-darios / WordCount.java
Last active June 23, 2020 05:01
Totals the unique words found
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:";