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 / 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
@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
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 / myls.c
Last active September 24, 2019 02:25
Alternative implementation for the UNIX ls command.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
@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
@amiralles
amiralles / set.rb
Last active December 8, 2019 19:22
Set implementation in Ruby
require_relative "./linked_list.rb"
class Set
# Set's constructor.
def initialize
@list = LinkedList.new
end
# Inserts a member into the current set.
@amiralles
amiralles / btree.rb
Last active December 8, 2019 20:06
General purpose binary tree implemented in Ruby.
class BTree
attr_accessor :root, :size
class Node
attr_accessor :parent, :data, :left, :right
def initialize parent, data
self.parent = parent
self.data = data
end
class AVLTree
# Represents an entry into the avl tree.
class Node
attr_accessor :key, :data, :height, :left, :right, :deleted
def initialize key, data
self.key = key
self.data = data
self.height = 1