Skip to content

Instantly share code, notes, and snippets.

@techmaniack
Created August 6, 2012 00:09
Show Gist options
  • Save techmaniack/3268313 to your computer and use it in GitHub Desktop.
Save techmaniack/3268313 to your computer and use it in GitHub Desktop.
An implementation of 'Find-S' Algorithm in Ruby
#!/usr/bin/env ruby
# find-s.rb
#
# Copyright 2012 AbdulKarim Memon <abdulkarim@reversehack.in>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Implementation of 'Find-S Algorithm in Ruby.'
#
# Sample Input file ("find-s.input")
# sunny,warm,1
# sunny,cold,0
# rainy,warm,1
#
# "H" is the final Output Hash with the most generalized hypothesis.
#
require 'csv'
H={"sky"=>[],"temp"=>[]}
h=[]
#reading from the CSV formatted input
CSV.read('find-s.input').each do |item|
#entry is an Array
h.push ({"sky"=>item[0], "temp"=>item[1]}) if item[2] != "0"
end
# puts h
# => {"sky"=>"sunny", "temp"=>"warm"}
# => {"sky"=>"rainy", "temp"=>"warm"}
# push each 'value' of the positive input set to the
# generalized hypothesis
h.each do |entry|
H["sky"] << entry["sky"]
H["temp"] << entry ["temp"]
end
# remove the duplicate entries from each "value" array.
# H.each do
# H["sky"].uniq!
# H["temp"].uniq!
# end
H.each_value(&:uniq!) # is the same as .each_value{|o| o.uniq}
puts H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment