Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- // Question from https://www.facebook.com/groups/javaharikrishna/permalink/2189431594515722/
- public class ClimbStairsTest extends TestCase {
- private static int waysToClimbStairs(final int numberOfStairs) {
- if (numberOfStairs == 0) {
- return 1; // There's only one way: You're already there, with zero steps!
- } else if (numberOfStairs == 1) {
- return 1; // 1 step only
- // } else if (numberOfStairs == 2) {
- // return 2; // 1+1 and 2
- } else {
- final int solutionAfterGoingUpOneStair = waysToClimbStairs(numberOfStairs - 1);
- final int solutionAfterGoingUpTwoStairs = waysToClimbStairs(numberOfStairs - 2);
- return solutionAfterGoingUpOneStair + solutionAfterGoingUpTwoStairs;
- }
- }
- public void test() {
- assertUniqueWaysToClimbStairs(1, 0); // There's only one way: You're already there, with zero steps!
- assertUniqueWaysToClimbStairs(1, 1);
- assertUniqueWaysToClimbStairs(2, 2);
- assertUniqueWaysToClimbStairs(3, 3);
- assertUniqueWaysToClimbStairs(5, 4);
- assertUniqueWaysToClimbStairs(8, 5);
- assertUniqueWaysToClimbStairs(13, 6);
- assertUniqueWaysToClimbStairs(21, 7);
- assertUniqueWaysToClimbStairs(34, 8);
- assertUniqueWaysToClimbStairs(55, 9);
- assertUniqueWaysToClimbStairs(89, 10);
- // Turns out, it's the Fibonacci Sequence.
- }
- private static void assertUniqueWaysToClimbStairs(final int expectedWays, final int numberOfStairs) {
- assertEquals("for <" + numberOfStairs + "> stairs;", expectedWays, waysToClimbStairs(numberOfStairs));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement