Skip to content

Instantly share code, notes, and snippets.

View amiralles's full-sized avatar
🏠
Working from home

Ale Miralles amiralles

🏠
Working from home
View GitHub Profile
@amiralles
amiralles / flatten.rb
Last active October 3, 2018 14:59
How to flatten an arbitrary length array without using Array#flatten method.
#!/usr/local/bin/ruby -w
# This module adds to the class that includes it the ability to flatten
# arbitrary nested arrays.
module ArrayFlattener
# This methods flattens an arbitrary nested array.
# It returns a flat array if the input is valid, nil is the input
# is nil, or throws an exception in any other case.
def flatten array, res = Array.new
@amiralles
amiralles / bitshift.rb
Last active October 2, 2018 14:39
Encode/Decode cell addresses into integers using ruby
#!/usr/local/bin/ruby -w
# Bit masks
MASKA = 0XFFFF
MASKB = 0XFFFFFFFF
MASKR = 0XFFFFFFFFFFFFFFFF
def letter? ch; ch =~ /[a-zA-Z]/ end
def number? ch; ch =~ /[0-9]/ end
def flatten array, res = Array.new
# 1. Does the candidate guard against invalid inputs?
return nil unless array
unless array.is_a?(Array)
# 2. When an error condition arises, does the candidate
# provides a comprehensive error message?
# (That is: an error message that other developer can
# look and immediately know what went wrong.)
# (*) See comments on die_arg_is_not_array for more details.
die_arg_is_not_array "array", array
#!/usr/bin/env ruby
class AVLTree
class Node
attr_accessor :key, :data, :height, :left, :right
def initialize key, data
self.key = key
self.data = data
self.height = 1
@amiralles
amiralles / linked_list.rb
Last active June 10, 2020 09:07
Singly linked list implemented in ruby
class LinkedList
class Node
attr_accessor :next, :data
def initialize data
self.data = data
self.next = nil
end
end
attr_accessor :head, :tail, :length
@amiralles
amiralles / doubly_linked_list.rb
Last active November 6, 2021 09:29
Doubly linked lists implemented in ruby
require_relative "./linked_list.rb"
class DoublyLinkedList < LinkedList
class Node < LinkedList::Node
attr_accessor :prev
end
# Inserts a new item into the list.
# Complexity: O(1).
def insert data
@amiralles
amiralles / circular_linked_list.rb
Last active December 8, 2019 18:21
Circular linked list implemented in ruby
class CircularList
class Node
attr_accessor :next, :data
def initialize data
self.data = data
self.next = nil
end
end
attr_accessor :head, :current, :length
@amiralles
amiralles / queue.rb
Last active December 8, 2019 18:23
Queue implemented in ruby
class Queue
class Node
attr_accessor :next, :data
def initialize data
self.data = data
self.next = nil
end
end
attr_accessor :head, :tail, :length
class HashTable
class Slot
attr_accessor :key, :value, :vacated
def initialize key, value
self.key = key
self.value = value
self.vacated = true
end
@amiralles
amiralles / stack.rb
Created November 5, 2018 13:05
Stack implemented in Ruby.
class Stack
class Node
attr_accessor :next, :data
def initialize data
self.data = data
self.next = nil
end
end
attr_accessor :head, :tail, :length