Last active
December 15, 2015 13:19
-
-
Save TalkativeTree/5266786 to your computer and use it in GitHub Desktop.
finding the number of potential outcomes of flipping a coin or some other chance related outcome.
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
def combination(num_tosses) | |
if num_tosses == 0 | |
potential_outcomes = 0 | |
elsif potential_outcomes = ["heads", "tails"].length ** num_tosses | |
end | |
return "There are #{potential_outcomes} potential outcomes" | |
end | |
puts combination(8) #=> "There are 256 potential outcomes" | |
# another way to do this is with #repeated_permutation | |
#def combination(num_tosses) | |
# if num_tosses == 0 | |
# 0 | |
# else | |
# ["heads", "tails"].repeated_permutation(num_tosses).to_a.length | |
# end | |
#end | |
#puts combination(4) => 16 | |
#if you inspect the array => | |
#=> [["heads", "heads", "heads", "heads"], ["heads", "heads", "heads", "tails"], ["heads", "heads", "tails", "heads"], | |
#=> ["heads", "heads", "tails", "tails"], ["heads", "tails", "heads", "heads"], ["heads", "tails", "heads", "tails"], | |
#=> ["heads", "tails", "tails", "heads"], ["heads", "tails", "tails", "tails"], ["tails", "heads", "heads", "heads"], | |
#=> ["tails", "heads", "heads", "tails"], ["tails", "heads", "tails", "heads"], ["tails", "heads", "tails", "tails"], | |
#=> ["tails", "tails", "heads", "heads"], ["tails", "tails", "heads", "tails"], ["tails", "tails", "tails", "heads"], | |
#=> ["tails", "tails", "tails", "tails"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment