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
#!/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 |
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
#!/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 |
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 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 |
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
#!/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 |
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 LinkedList | |
class Node | |
attr_accessor :next, :data | |
def initialize data | |
self.data = data | |
self.next = nil | |
end | |
end | |
attr_accessor :head, :tail, :length |
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_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 |
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 CircularList | |
class Node | |
attr_accessor :next, :data | |
def initialize data | |
self.data = data | |
self.next = nil | |
end | |
end | |
attr_accessor :head, :current, :length |
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 Queue | |
class Node | |
attr_accessor :next, :data | |
def initialize data | |
self.data = data | |
self.next = nil | |
end | |
end | |
attr_accessor :head, :tail, :length |
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 HashTable | |
class Slot | |
attr_accessor :key, :value, :vacated | |
def initialize key, value | |
self.key = key | |
self.value = value | |
self.vacated = true | |
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
class Stack | |
class Node | |
attr_accessor :next, :data | |
def initialize data | |
self.data = data | |
self.next = nil | |
end | |
end | |
attr_accessor :head, :tail, :length |
OlderNewer