Created
February 16, 2010 17:46
-
-
Save joshrendek/305731 to your computer and use it in GitHub Desktop.
This file contains 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 Canonical | |
def initialize(eq) | |
@eq = eq.split('+') | |
@new_eq = [] | |
end | |
def binary_to_decimal(x) | |
y = 1 | |
z = 0 | |
x.gsub(/ /,'').reverse.scan(/(0|1)/).each do |x| | |
z = z + (y * x[0].to_i) | |
y *= 2 | |
end | |
z | |
end | |
def missing? | |
@missing = [] | |
index = 0 | |
for e in @eq | |
if e.index('a').nil? | |
@missing << 'a' | |
end | |
if e.index('b').nil? | |
@missing << 'b' | |
end | |
if e.index('c').nil? | |
@missing << 'c' | |
end | |
if e.index('a') && e.index('b') && e.index('c') | |
@missing << '' | |
end | |
#p "Term " + index.to_s + " missing: " + @missing[-1] if @missing[-1] != '' | |
index+=1 | |
end | |
end | |
def show | |
@ordered_hash = {} | |
bin = {} | |
#p "SHOWING" | |
tmp = '' | |
index = 0 | |
for e in @new_eq | |
bin[binary_to_decimal(e.gsub("a'", "0").gsub("b'", "0").gsub("c'", "0").gsub("a", "1").gsub("b", "1").gsub("c", "1"))] = e | |
index+=1 | |
end | |
show = [] | |
bin.sort.collect{|k,v| show << v } | |
p show.join('+') | |
end | |
def insert_missing | |
index = 0 | |
for e in @eq | |
#p "Working on: " + e | |
if @missing[index].size > 0 | |
@new_eq << e+@missing[index] | |
@new_eq << e+@missing[index]+"'" | |
else | |
@new_eq << e | |
end | |
index += 1 | |
end | |
end | |
def show_eq | |
p @eq.join('+') | |
end | |
end | |
q = Canonical.new("a'b'c+a'b'c'+abc'+a'bc'+abc+abc'+ab'c+ab'c'") | |
q.show_eq | |
q.missing? | |
q.insert_missing | |
p " ----> " | |
q.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment