Skip to content

Instantly share code, notes, and snippets.

RubyLearning Git & GitHub Course RLGGHC

View GitHub Profile
@RLGGHC
RLGGHC / tinyxml.rb
Created Oct 28, 2009
BasicObject example
View tinyxml.rb
# 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
View gist:222836
class Circuit
def initialize(resistors)
@resistors = resistors
@final_node = 0
@resistors.each do |(source, destination, resistance)|
@final_node = [@final_node, source, destination].max
end
View closed-circuit.rb
require 'set'
class ClosedCircuit
Infinity = 1/0.0
def initialize(input, source, dest)
@graph = GraphParser.parse(input)
@source = source
@dest = dest
end
View README.rdoc
View find_redundant_resistors.rb
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],
View rpcfn3.rb
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
View closed-circuit.rb
require 'set'
class ClosedCircuit
Infinity = 1/0.0
def initialize(input, source, dest)
@graph = GraphParser.parse(input)
@source = source
@dest = dest
end
View 003_short_circuit
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],
View gist:239937
############
#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
View 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
You can’t perform that action at this time.