Created
March 1, 2013 13:47
-
-
Save DVG/5064760 to your computer and use it in GitHub Desktop.
Prints out a boolean truth table for a given number of variables
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 Combinations | |
attr_accessor :number_of_variables | |
def initialize(number_of_variables=2) | |
@number_of_variables = number_of_variables | |
end | |
def print_combinations | |
combinations.each do |row| | |
row.each_with_index do |v, i| | |
print "| " if i == 0 | |
print string_val(v) | |
print " | " unless i == number_of_variables | |
print "\n" if i == number_of_variables - 1 | |
end | |
end | |
end | |
def combinations | |
([true, false]*number_of_variables).combination(number_of_variables).to_a.uniq | |
end | |
def string_val(val) | |
if val == true | |
"T" | |
else | |
"F" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment