Skip to content

Instantly share code, notes, and snippets.

View RLGGHC's full-sized avatar

RubyLearning Git & GitHub Course RLGGHC

View GitHub Profile
@RLGGHC
RLGGHC / tinyxml.rb
Created October 28, 2009 03:53
BasicObject example
# Courtesy David Black
class TinyXML < BasicObject
def method_missing(m, *args, &block)
$stdout << "<#{m}>\n"
if block
$stdout << yield
end
$stdout << "\n</#{m}>"
return
end
class Circuit
def initialize(resistors)
@resistors = resistors
@final_node = 0
@resistors.each do |(source, destination, resistance)|
@final_node = [@final_node, source, destination].max
end
require 'set'
class ClosedCircuit
Infinity = 1/0.0
def initialize(input, source, dest)
@graph = GraphParser.parse(input)
@source = source
@dest = dest
end
@RLGGHC
RLGGHC / README.rdoc
Created November 7, 2009 07:51 — forked from thuss/README.rdoc
require "test/unit"
class TestFindRedundantResistors < Test::Unit::TestCase
def test_find_redundant_resistors
actual = find_redundant_resistors [
[ 'A', 'B', 50],
[ 'A', 'D', 150],
[ 'B', 'C', 250],
[ 'B', 'E', 250],
[ 'C', 'E', 350],
[ 'C', 'D', 50],
class Graph
require 'set'
INFINITY = 1 << 64
# Constructor
def initialize
@graph = magic_hash
# The graph // {node => { edge1 => weight, edge2 => weight}, node2 => ...
@nodes = Set.new
end
EDGES = [
[ 'A', 'B', 50],
[ 'A', 'D', 150],
[ 'B', 'C', 250],
[ 'B', 'E', 250],
[ 'C', 'E', 350],
[ 'C', 'D', 50],
[ 'C', 'F', 100],
[ 'D', 'F', 400],
[ 'E', 'G', 200],
############
#Class Hash#
############
#Dijkstra's algorythm requires a priority queue, the easiest way to make one is to add a method
#to hash that pops the key/value pair with the lowest value.
class Hash
#Return the key/value pair with the lowest value as an array, deletes the pair.
def pop_lowest_value!
smallest_pair = self.min{ |a,b| a[1] <=> b[1]}
self.delete(smallest_pair[0]) if smallest_pair
require 'set'
class ClosedCircuit
Infinity = 1/0.0
def initialize(input, source, dest)
@graph = GraphParser.parse(input)
@source = source
@dest = dest
end
@RLGGHC
RLGGHC / Circuits.rb
Created November 21, 2009 01:18 — forked from ruprict/Circuits.rb
#!/usr/bin/env ruby
class CircuitSegment
attr_accessor :startNode, :endNode, :ohmage
def initialize(startNode, endNode, ohmage)
@startNode=startNode
@endNode=endNode
@ohmage=ohmage
end
end