Created
October 15, 2011 18:34
-
-
Save vasanthela/1289945 to your computer and use it in GitHub Desktop.
Prerequisite AI Programming Assignment
This file contains hidden or 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
# to run this file, in terminal window, do the following commands | |
# gem install rspec | |
# rspec prob_spec.rb | |
class Dice | |
#you can code the class here | |
#if you want to move the class code, move it to a file labelled as dice.rb | |
#place the statement: require "dice" | |
#at the top of this spec file | |
end | |
class Solve | |
#you can code the class here | |
#if you want to move the class code, move it to a file labelled as solve.rb | |
#place the statement: require "solve" | |
#at the top of this spec file | |
end | |
describe "Dice" do | |
context "in context to initializing class variables" do | |
it "should allow you to configure the number of sides for a dice, with each side having a label, and a unique probability" do | |
dice = Dice.new(2, ["heads","tails"], [0.5,0.5]) | |
dice.number_of_choices.should == 2 | |
dice.choice_labels.should == ["heads","tails"] | |
dice.("heads").should == 0.5 | |
dice.("tails").should == 0.5 | |
end | |
it "should fail if you give it probability values that do not add up to 1" do | |
expect { Dice.new(1,["heads"],[0.9]) }.should raise_error | |
end | |
end | |
end | |
describe "Solve" do | |
it "should solve for the probability of a choice of events" do | |
one_sided_die = Dice.new(1,"only side",[1.0]) | |
#Given 3 Dice and 5 Buckets, how many ways are there to place the Dice in the Buckets | |
#Also known as k Choose n or 5 choose 3 | |
Solve.new(one_sided_die).choose(5, 3).should == 10 | |
end | |
it "should solve for the probability of a list of ordered events" do | |
four_sided_die = Dice.new(4, ["1","2","3","4"], [0.20,0.20,0.20,0.40]) | |
Solve.new(four_sided_die).prob_of_ordered_events(["1","4","2"]).should == 0.016 | |
end | |
it "should solve for the probability of a set of unordered events" do | |
two_sided_die = Dice.new(2,["heads","tails"],[0.5,0.5]) | |
Solve.new(two_sided_die).prob_of_unordered_events(["heads","tails"]).should == 0.5 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the post http://hungrymonkeys.posterous.com/75598210