Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am dougyoung on github.
  • I am dougyoung (https://keybase.io/dougyoung) on keybase.
  • I have a public key ASAE5lQfO03fPjNXY6RW1aodXjMBr7trsxHfDn2_TMQZ2wo

To claim this, I am signing this object:

@dougyoung
dougyoung / int_min_heap.py
Created January 5, 2018 21:57
Implementation of an integer min heap in Python3, O(n*log(n)) time.
# TODO: Should make this an object for sharing.
heap = []
def hasParent(index):
return parentIndex(index) >= 0 and parentIndex(index) < len(heap)
def hasLeftChild(index):
return leftChildIndex(index) < len(heap)
def hasRightChild(index):
return rightChildIndex(index) < len(heap)
@dougyoung
dougyoung / medianHeap.java
Last active April 15, 2019 06:22
Running median of an input stream - O(n*log(n))
import java.util.*;
class Main {
private static class MedianHeap {
PriorityQueue<Integer> lowers = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer one, Integer two) {
return -1 * one.compareTo(two);
}
});
PriorityQueue<Integer> uppers = new PriorityQueue<>();
@dougyoung
dougyoung / fibonacci.go
Last active January 1, 2018 23:31
Dynamic programming implementation of the Fibonacci sequence, time complexity of O(n).
package main
import "fmt"
func fibo(x uint64, y uint64, n uint64) uint64 {
fibos := [2]uint64{x, y}
var fibo uint64;
for i := uint64(2); i < n+1; i++ {
fibo = fibos[0] + fibos[1]
@dougyoung
dougyoung / prime_number_sieve.go
Last active April 15, 2019 06:22
Sieve of Eratosthenes in Golang - O(n(log(log(n))))
package main
import "fmt"
func sieve(n uint64) []bool {
primeBits := make([]bool, n-2)
for i := uint64(2); i < n; i++ {
for j := i; (i*j)-2 < n-2; j++ {
primeBits[(i*j)-2] = true
# Set working directory
setwd('/Users/dougyoung/Downloads')
# Load data
inboundLeadData <- read.csv('Inbound Lead Regression Analysis Data.csv', stringsAsFactors=TRUE)
# Inspect data
class(inboundLeadData)
dim(inboundLeadData)
names(inboundLeadData)
# Recursive finds number:
def fib_recur(n)
return n if n <= 1
fib_recur(n-1) + fib_recur(n-2)
end
# Computes F(n − 2) twice, F(n − 3) three times, etc., each time from scratch.
# O(2^n)
# Recursive with memorization:
@dougyoung
dougyoung / anagram.rb
Last active August 29, 2015 14:01
Anagram discoverer in Ruby
require 'set'
class Anagram
DefaultWordlist = (File.open '/usr/share/dict/words' rescue [])
.inject(Set.new) { |s, word| s << word.downcase.chomp }
def initialize(wordlist = DefaultWordlist)
@wordlist_signed = wordlist
.inject(Hash.new { [] }) { |h, word| h[sign(word)] <<= word; h }
end
@dougyoung
dougyoung / prime_number_sieve.rb
Created November 12, 2013 18:47
Prime number sieve
def prime_number_sieve(n)
primes = []
integers_up_to_n = (2..n).to_a
integers_up_to_n.each do |i|
next if i.nil?
primes << i
not_prime = i + i
while (not_prime <= n) do
integers_up_to_n[not_prime - 2] = nil
not_prime = not_prime + i
class Product
def self.recommend(products, distribution)
accumulative_distribution_intervals = ProbabilityHelper.cumulative_intervals(distribution)
accumulative_distribution_sum = ProbabilityHelper.cumulative_sum(distribution)
random_point_in_accumulative_distribution = Random.rand * accumulative_distribution_sum
accumulative_distribution_intervals.each_with_index do |interval, index|
if random_point_in_accumulative_distribution >= interval[0] && random_point_in_accumulative_distribution < interval[1]
return products[index]
end
end