Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,28 @@ public static int numberOfWays(int n) {

return curr; // Ways to reach the nth step
}

/**
* A runnable example demonstrating how to use {@link #numberOfWays(int)}
* and displaying the progression of ways to climb staircases of sizes 0 through 10.
*
* @param args command-line arguments (optional)
*/
public static void main(String[] args) {
System.out.println("=== Climbing Stairs Demonstration ===");
System.out.println("You can climb either 1 or 2 steps at a time.");
System.out.println("Recurrence: ways(n) = ways(n - 1) + ways(n - 2)\n");

for (int steps = 0; steps <= 10; steps++) {
int ways = numberOfWays(steps);
System.out.printf("Steps: %2d --> Distinct ways: %3d%n", steps, ways);
}

System.out.println("\nExample breakdown for n = 4 (5 ways):");
System.out.println(" 1. 1 + 1 + 1 + 1");
System.out.println(" 2. 1 + 1 + 2");
System.out.println(" 3. 1 + 2 + 1");
System.out.println(" 4. 2 + 1 + 1");
System.out.println(" 5. 2 + 2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Unit and example verification tests for {@link ClimbingStairs}.
*
* Fixes: #27
*/
public class ClimbingStairsTest {

@Test
void testBaseCases() {
assertEquals(0, ClimbingStairs.numberOfWays(0), "0 steps should have 0 ways");
assertEquals(1, ClimbingStairs.numberOfWays(1), "1 step should have 1 way");
}

@ParameterizedTest
@CsvSource({
"2, 2",
"3, 3",
"4, 5",
"5, 8",
"6, 13",
"7, 21",
"8, 34",
"9, 55",
"10, 89"
})
void testKnownValues(int n, int expectedWays) {
assertEquals(expectedWays, ClimbingStairs.numberOfWays(n),
String.format("For n = %d, expected %d ways", n, expectedWays));
}

@Test
void testFibonacciRecurrenceInvariant() {
// For any n >= 3: numberOfWays(n) == numberOfWays(n-1) + numberOfWays(n-2)
for (int n = 3; n <= 25; n++) {
int waysN = ClimbingStairs.numberOfWays(n);
int waysNMinus1 = ClimbingStairs.numberOfWays(n - 1);
int waysNMinus2 = ClimbingStairs.numberOfWays(n - 2);
assertEquals(waysNMinus1 + waysNMinus2, waysN,
"Recurrence relation ways(n) = ways(n-1) + ways(n-2) must hold for n = " + n);
}
}

@Test
void testLargerInput() {
// n = 30 -> 1346269
assertEquals(1346269, ClimbingStairs.numberOfWays(30));
}

@Test
void testMainMethodExecutesWithoutError() {
assertDoesNotThrow(() -> ClimbingStairs.main(new String[0]),
"The runnable main demonstration should execute without exceptions");
}
}