This file contains hidden or 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 lcs(first, second) | |
return nil if first.nil? || second.nil? | |
x, xs, y, ys = first[0..0], first[1..-1], second[0..0], second[1..-1] | |
if x == y | |
x + lcs(xs, ys) | |
else | |
[lcs(first, ys), lcs(xs, second)].max_by {|x| x.size} | |
end | |
end |
This file contains hidden or 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
module LongestSubstring | |
def self.find(first, second) | |
return nil if first.nil? || second.nil? | |
return nil if first != second | |
x, xs, y, ys = first[0..0], first[1..-1], second[0..0], second[1..-1] | |
if x == y | |
x + self.find(xs, ys) | |
else | |
[self.find(first, ys), self.find(xs, second)].max_by {|x| x.size} |
This file contains hidden or 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
require 'minitest/autorun' | |
require_relative 'binary_tree' | |
class BinaryTreeTest < Minitest::Test | |
def test_data_is_saved | |
assert_equal 7, BinaryTree.new(7).value | |
end | |
def test_inserting_left | |
seven = SortBinaryTree.new(7) |
This file contains hidden or 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 recursive_fib(n) | |
if n <= 1 | |
return n | |
else | |
result = recursive_fib(n-2) + recursive_fib(n-1) | |
end | |
end | |
def itterative_fib(n) | |
fib = [0, 1] |
This file contains hidden or 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 LinkedListNode | |
attr_accessor :value, :next_node | |
def initialize(value, next_node=nil) | |
@value = value | |
@next_node = next_node | |
end | |
end | |
def print_values(list_node) |
This file contains hidden or 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 Tree | |
attr_accessor :value, :children | |
def initialize(value, children = []) | |
@value = value | |
@children = children | |
end | |
end | |
def BFS(data, target) | |
queue = [data] |
This file contains hidden or 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 BinaryTree | |
attr_accessor :value, :left, :right | |
def initialize (value) | |
@value = value | |
end | |
end | |
class SortBinaryTree |
This file contains hidden or 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 LinkedListNode | |
attr_accessor :value, :next_node | |
def initialize(value, next_node=nil) | |
@value = value | |
@next_node = next_node | |
end | |
end | |
def print_values(list_node) |
This file contains hidden or 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 Image | |
attr_accessor :data | |
def initialize (data) | |
@data = data | |
end | |
def image_blur | |
all_done = false | |
@data.each_with_index do |row, row_index| | |
row.each_with_index do |value, column_index| |
This file contains hidden or 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 Image | |
attr_accessor :data | |
def initialize (data) | |
@data = data | |
end | |
def output | |
@data.each do |sub| | |
sub.each do |cell| | |
print cell |