Skip to content

Instantly share code, notes, and snippets.

View earlonrails's full-sized avatar

Kevin Krauss earlonrails

  • Disney Streaming
  • San Francisco
View GitHub Profile
#!/usr/bin/env ruby
Node = Struct.new(:value, :left, :right)
def create_tree(nodes, root=nil, idx=0)
if nodes.size > idx
root = Node.new(nodes[idx])
root.left = create_tree(nodes, root.left, 2 * idx + 1)
root.right = create_tree(nodes, root.right, 2 * idx + 2)
end
@earlonrails
earlonrails / lru.rb
Last active December 9, 2019 23:30
Least Recently Used cache in changed slightly from this: https://www.sitepoint.com/ruby-interview-questions-lru-cache-and-binary-trees/
#!/usr/bin/env ruby
# Doubly Linked List
Node = Struct.new(:key, :value, :next, :prev)
class LRU
attr_accessor :head, :tail, :max, :cache
def initialize(max)
@head = nil
@tail = nil
@earlonrails
earlonrails / traversal.rb
Last active May 11, 2018 19:23
DFS traversal and BFS Traversal in Ruby
#!/usr/bin/env ruby
require 'pry'
class Node
attr_accessor :val, :left, :right
def initialize(val, left = nil, right = nil)
@val = val
@left = left
@right = right
end
@earlonrails
earlonrails / goat_latin.rb
Last active May 9, 2018 05:22
The Popular goat latin interview question
#!/usr/bin/env ruby
sentence = ['This','is','goat','latin']
vowel = ['a', 'e', 'i', 'o', 'u']
result = []
sentence.each_with_index do |word, idx|
aaa = "ma" + "a" * (idx + 1)
if vowel.include?(word[0])
result << word + aaa
else
@earlonrails
earlonrails / split_array_sum.rb
Created May 9, 2018 04:22
Given an array of integers greater than zero, find if it is possible to split it in two (without reordering the elements), such that the sum of the two resulting arrays is the same. Print the resulting arrays. https://www.careercup.com/question?id=5716403849003008
#!/usr/bin/env ruby
test_cases = [
[1,2,3,4,5,6,21],
[1,90, 50, 30, 5, 3, 2, 1 ],
[1, 50, 900, 1000]
]
def array_sums(arr)
sum_one = 0
@earlonrails
earlonrails / merkle.rb
Created May 7, 2018 00:54
Merkle tree in ruby
#!/usr/bin/env ruby
class Merkle
attr_accessor :key, :parent, :left, :right
def initialize(array, parent = nil)
@parent = parent
@key = array.join
load(array)
end
@earlonrails
earlonrails / bft.rb
Created May 7, 2018 00:22
Breadth First Traversal in ruby
#!/usr/bin/env ruby
def bft(root)
buffer = ""
curr_level = 1
root[:level] = curr_level
q = [root]
while(q.any?) do
node = q.shift
if (node[:level] > curr_level)
@earlonrails
earlonrails / Rolodex.js
Created January 25, 2018 17:57
Contacts Problem in ES6, people still know what Rolodexs are right?
#!/usr/bin/env node
"use strict"
// hackerrank https://www.hackerrank.com/challenges/ctci-contacts/problem
// add hack
// add hackerrank
// find hac
// find hak
// var alphabet = 'abcdefghijklmnopqrstuvwxyz'
class Rolodex {
#!/usr/bin/env node
// https://www.geeksforgeeks.org/find-if-given-matrix-is-toeplitz-or-not/
class Toeplitz {
static check(matrix) {
for (var i = 1; i < matrix.length - 1; i++) {
let row = matrix[i]
let prevRow = matrix[i - 1]
for (var j = 1; j < row.length - 1; j++) {
#!/usr/bin/env node
class RansomNote {
constructor(words, magazineWords) {
this.words = this.parseDocument(words)
this.magazine = this.parseDocument(magazineWords)
}
parseDocument(words) {
var wordHash = {}