Skip to content

Instantly share code, notes, and snippets.

@TalkativeTree
Last active December 15, 2015 13:19
Show Gist options
  • Save TalkativeTree/5266786 to your computer and use it in GitHub Desktop.
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.
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