Skip to content

Instantly share code, notes, and snippets.

RubyLearning Git & GitHub Course RLGGHC

Block or report user

Report or block RLGGHC

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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 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 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 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
View polynomial.rb
class Polynomial
def initialize(arg)
raise ArgumentError.new('We expect an array at least') unless arg.is_a? Array
raise ArgumentError.new('Need at least 2 coefficients') if arg.length < 2
@exponents = []
arg.reverse.inject(0) do |index,item|
@exponents.push( { "factor" => item, "exponent" => index }) unless item == 0
index + 1
end
You can’t perform that action at this time.