Challenge:
Find the number of ways to climb some stairs with n steps and with some limitations. You should be able to run the tests below on TIO https://tio.run/ without timing out. – 60 seconds. (Typically a fraction of a second is well within reach for most languages if a good optimizing strategy is applied).
The input is a list of positive numbers:
- the first number in the input is the total number of steps in the stairway
- the rest of the input is the different number of steps you are allowed to climb at once, but you're only allowed to use n steps a maximum n times if n>1. So if 2 is allowed you're only allowed to take 2 steps a maximum of 2 times. And 3 steps maximum 3 times and so on for all n>1. So if 1 is allowed, you can take 1 step as many times as you like.
- you should not "overstep", with a stairway of 5 steps and only 2 steps at once are allowed, there is no way to climb it (output 0)
Allowed assumptions: all input numbers are positive integers, at least 1 (0, negative and fractional numbers need no special handling). The list of allowed steps are unique numbers and ordered if it helps. Also the size of the stairs can be the last number or a separate part of the input if that's helpful to your implementation ("reorganizing" input don't need to be a part of the problem)
Output:
- a number which is the number of ways to climb the stairs
Examples:
- Input: 3,1 Output: 1 (as there is only one way when you're only allowed one step at a time)
- Input: 3,1,2 Output: 3 (since you can climb in three ways: 1+1+1 or 1+2 or 2+1)
- Input: 3,4 Output: 0 (you should always end at the top, you cannot take 4 steps since the stairs only has 3)
- Input: 4,1,2,3 Output: 7 (1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 3+1, 1+3)
- Input: 6,2 Output: 0 (since you're not allowed to take 2 steps 3 times)
- Input: 6,2,1 Output: 12 (2+2+1+1, 2+1+2+1, 2+1+1+2, 2+1+1+1+1, 1+2+2+1, 1+2+1+2, 1+2+1+1+1, 1+1+2+2, 1+1+2+1+1, 1+1+1+2+1, 1+1+1+1+2, 1+1+1+1+1+1. But 2+2+2 isn't allowed)
- Input: 99,99,1 Output: 2 (99 or 1+1+1+1+...99 times)
More tests:
2,1 → 1
10,1 → 1
3,1,2 → 3
3,4 → 0
3,2 → 0
4,1,2 → 5
4,1,2,3 → 7
6,2 → 0
6,2,1 → 12
7,1,2 → 17
5,1,2,3 → 13
15,1,2,7 → 266
39,3,2,7 → 301
99,11,3,2 → 1981
0,1
give output0
? Half the existing answers output1
in this case (which sounds correct to me given your specification: there is exactly one way to do nothing!), while another wastes characters wrapping the answer inIf[#<1,0,...]
just to "fix" that case. \$\endgroup\$0,1 → 0
was the easy one and0,1 → 1
would have required extra code. If your comment get at least three upvotes, I'll be willing to add steps>0 as a constraint to the problem and remove that test case. \$\endgroup\$0,1 → 0
. \$\endgroup\$