Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c102a3

Browse files
committed
Add Tower of Hanoi.
1 parent 44b0a99 commit 8c102a3

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

‎src/algorithms/uncategorized/hanoi-tower/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Animation of an iterative algorithm solving 6-disk problem
2121

2222
With `3` disks, the puzzle can be solved in `7` moves. The minimal
2323
number of moves required to solve a Tower of Hanoi puzzle
24-
is `2n − 1`, where `n` is the number of disks.
24+
is `2^n − 1`, where `n` is the number of disks.
2525

2626
## References
2727

‎src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import hanoiTower from '../hanoiTower';
33
describe('hanoiTower', () => {
44
it('should solve tower of hanoi puzzle with 2 discs', () => {
55
const moveCallbackMock = jest.fn();
6+
const numberOfDiscs = 2;
67

7-
hanoiTower(2, moveCallbackMock);
8+
hanoiTower(numberOfDiscs, moveCallbackMock);
89

9-
expect(moveCallbackMock).toHaveBeenCalledTimes(3);
10+
expect(moveCallbackMock).toHaveBeenCalledTimes((2**numberOfDiscs)-1);
1011

1112
expect(moveCallbackMock.mock.calls[0][0]).toBe(1);
1213
expect(moveCallbackMock.mock.calls[0][1]).toEqual([1, 2]);
@@ -23,17 +24,19 @@ describe('hanoiTower', () => {
2324

2425
it('should solve tower of hanoi puzzle with 3 discs', () => {
2526
const moveCallbackMock = jest.fn();
27+
const numberOfDiscs = 3;
2628

27-
hanoiTower(3, moveCallbackMock);
29+
hanoiTower(numberOfDiscs, moveCallbackMock);
2830

29-
expect(moveCallbackMock).toHaveBeenCalledTimes(7);
31+
expect(moveCallbackMock).toHaveBeenCalledTimes((2**numberOfDiscs)-1);
3032
});
3133

3234
it('should solve tower of hanoi puzzle with 6 discs', () => {
3335
const moveCallbackMock = jest.fn();
36+
const numberOfDiscs = 6;
3437

35-
hanoiTower(6, moveCallbackMock);
38+
hanoiTower(numberOfDiscs, moveCallbackMock);
3639

37-
expect(moveCallbackMock).toHaveBeenCalledTimes(63);
40+
expect(moveCallbackMock).toHaveBeenCalledTimes((2**numberOfDiscs)-1);
3841
});
3942
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /