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
#!/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 |
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
#!/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 |
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
#!/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 |
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
#!/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 |
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
#!/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 |
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
#!/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 |
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
#!/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) |
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
#!/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 { |
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
#!/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++) { |
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
#!/usr/bin/env node | |
class RansomNote { | |
constructor(words, magazineWords) { | |
this.words = this.parseDocument(words) | |
this.magazine = this.parseDocument(magazineWords) | |
} | |
parseDocument(words) { | |
var wordHash = {} |
NewerOlder