Skip to content

Instantly share code, notes, and snippets.

Avatar

Aziz Sharipov gagaception

View GitHub Profile
View lcs.rb
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
View longest_substring.rb
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}
View binary_tree_test.rb
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)
View fibanacci.rb
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]
View infinity_ll.rb
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)
View tr_tree.rb
class Tree
attr_accessor :value, :children
def initialize(value, children = [])
@value = value
@children = children
end
end
def BFS(data, target)
queue = [data]
View binary_tree.rb
class BinaryTree
attr_accessor :value, :left, :right
def initialize (value)
@value = value
end
end
class SortBinaryTree
View reversed_linked_list
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)
View image_blur#2
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|
View challenge#1.rb
class Image
attr_accessor :data
def initialize (data)
@data = data
end
def output
@data.each do |sub|
sub.each do |cell|
print cell