Skip to content

Instantly share code, notes, and snippets.

@AnielaMW
Created February 12, 2019 00:35
Show Gist options
  • Save AnielaMW/83335f753a6ad6d8003697ca83aec914 to your computer and use it in GitHub Desktop.
Save AnielaMW/83335f753a6ad6d8003697ca83aec914 to your computer and use it in GitHub Desktop.
Stairs Challenge_Ruby
puts "Stairs Challenge"
# Given
# x = the number of steps to the top of a stair
# and
# n = is the maximum stride to be taken
# Solve for the total number of ways to reach the top step.
# Assumptions
# There will always be at least one way.
# 0 is not a valid stride.
def ways(top_step, stride)
stair = (1..top_step)
arr = [1]
stair.each do |step|
arr.push(arr.reduce(:+))
arr.shift if arr.length > stride
end
arr[-1]
end
puts ways(3, 3) == 4
puts ways(5, 3) == 13
puts ways(8, 3) == 81
puts ways(6, 4) == 29
puts ways(5, 5) == 16
puts ways(10, 5) == 464
puts ways(42, 10) == 2163721825536
puts ways(1765, 23) == 1039602300720182949531290166735978963919254872540498389861689268819873787659903275891016947657827704312413119114251022853135423993607616893994932325611731804159556097350124510528972587480084537728338077697792780337510887693066344283593811293503921936884013646257847864345096282056840310793188819726663155676582613070220100469550542883893298739091317146652816585042727521424577561376926579159495635578786368512865680619639826539672721693182234770208133052035383723399343718433609037709851685140183430843762289204200541279141793210368
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment