Skip to content

Instantly share code, notes, and snippets.

@Stephenitis
Created March 28, 2013 17:52
Show Gist options
  • Save Stephenitis/5265365 to your computer and use it in GitHub Desktop.
Save Stephenitis/5265365 to your computer and use it in GitHub Desktop.
How many different ways are there to flip a fair coin 5 times?
def combination(length=5)
return [[]] if length == 0
combination(length-1).collect {|c| [:h] + c } +
combination(length-1).collect {|c| [:t] + c }
end
combination
print "*There are #{combination.length} ways"
#my output
#"There are 32 ways"
#[[:h, :h, :h, :h, :h], [:h, :h, :h, :h, :t], [:h, :h, :h, :t, :h], [:h, :h, :h, :t, :t], [:h, :h, :t, :h, :h], [:h, :h, :t, :h, :t], [:h, :h, :t, :t, :h], [:h, :h, :t, :t, :t], [:h, :t, :h, :h, :h], [:h, :t, :h, :h, :t], [:h, :t, :h, :t, :h], [:h, :t, :h, :t, :t], [:h, :t, :t, :h, :h], [:h, :t, :t, :h, :t], [:h, :t, :t, :t, :h], [:h, :t, :t, :t, :t], [:t, :h, :h, :h, :h], [:t, :h, :h, :h, :t], [:t, :h, :h, :t, :h], [:t, :h, :h, :t, :t], [:t, :h, :t, :h, :h], [:t, :h, :t, :h, :t], [:t, :h, :t, :t, :h], [:t, :h, :t, :t, :t], [:t, :t, :h, :h, :h], [:t, :t, :h, :h, :t], [:t, :t, :h, :t, :h], [:t, :t, :h, :t, :t], [:t, :t, :t, :h, :h], [:t, :t, :t, :h, :t], [:t, :t, :t, :t, :h], [:t, :t, :t, :t, :t]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment