Skip to content

Instantly share code, notes, and snippets.

View iaintshine's full-sized avatar

Boguslaw Mista iaintshine

View GitHub Profile
@iaintshine
iaintshine / http.rb
Created August 13, 2014 17:50
A sample Ruby application showing how to make an HTTP request using TCP Sockets
#!/usr/bin/env ruby
# http.rb
#
# Sample application showing how to create HTTP request using TCP Sockets in Ruby
require 'socket'
class Request
attr_reader :method, :host, :resource, :client, :lines
@iaintshine
iaintshine / anagram.rb
Created August 13, 2014 18:33
A Ruby program which checks if two strings are anagrams of each other using two methods: sorting and frequency counting
#!/usr/bin/env ruby
# anagram.rb
module Anagram
ASCII_COUNT = 256
def sort
self.chars.sort.join
end
@iaintshine
iaintshine / anagram_palindrome.rb
Created August 13, 2014 19:02
A Ruby program which checks if any anagram of a given string is a palindrome or not
#!/usr/bin/env ruby
# anagram_palindrome.rb
module AnagramPalindrome
ASCII_COUNT = 256
def anagram_of_palindrome?
empty?
histogram = Array.new ASCII_COUNT, 0
@iaintshine
iaintshine / stack.rb
Created August 14, 2014 16:52
A Ruby implementation of a Stack data structure using a Single Linked List
require 'test/unit'
# LIFO
class Stack
include Enumerable
Node = Struct.new :element, :next
attr_reader :head, :size
@iaintshine
iaintshine / queue.rb
Created August 14, 2014 17:07
A Ruby implementation of a Queue data structure using a Single Linked List
require 'test/unit'
class Queue
include Enumerable
Node = Struct.new :element, :next
attr_reader :head, :tail, :size
def initialize(items = [])
@iaintshine
iaintshine / priority_queue.rb
Created August 14, 2014 20:48
A Ruby implementation of a Priority Queue data structure using a Sorted Double Linked List
require 'test/unit'
class PriorityQueue
include Enumerable
Node = Struct.new :priority, :element, :prev, :next
attr_reader :head, :tail, :size
def initialize(items = [])
@iaintshine
iaintshine / heap.rb
Created August 14, 2014 22:29
A Ruby implementation of a Binomial Min Heap data structure using Array-Based representation
require 'test/unit'
class Heap
Node = Struct.new :key, :value do
include Comparable
def <=> (other)
self.key <=> other.key
end
end
@iaintshine
iaintshine / heap.rb
Created August 14, 2014 22:47
A Ruby implementation of a Heapsort Algorithm using a Binomial Min Heap data structure
class Heap
Node = Struct.new :key, :value do
include Comparable
def <=> (other)
self.key <=> other.key
end
end
def initialize(items = [])
@iaintshine
iaintshine / tree.rb
Created August 15, 2014 07:15
A Ruby implementation of a Tree data structure and Tree Traversal algorithms
require 'test/unit'
module Tree
class Node
include Comparable
include Enumerable
attr_accessor :parent, :children, :element
def initialize(element)
@iaintshine
iaintshine / binary_search.rb
Created August 15, 2014 08:30
A Ruby implementation of a Binary Search Algorithm
require 'test/unit'
module Search
extend self
def binary(container, item)
_binary container, item, 0, container.length
end
def _binary(container, item, min, max)