mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add Tower of Hanoi.
This commit is contained in:
parent
44b0a99a80
commit
8c102a3f62
@ -21,7 +21,7 @@ Animation of an iterative algorithm solving 6-disk problem
|
||||
|
||||
With `3` disks, the puzzle can be solved in `7` moves. The minimal
|
||||
number of moves required to solve a Tower of Hanoi puzzle
|
||||
is `2n − 1`, where `n` is the number of disks.
|
||||
is `2^n − 1`, where `n` is the number of disks.
|
||||
|
||||
## References
|
||||
|
||||
|
@ -3,10 +3,11 @@ import hanoiTower from '../hanoiTower';
|
||||
describe('hanoiTower', () => {
|
||||
it('should solve tower of hanoi puzzle with 2 discs', () => {
|
||||
const moveCallbackMock = jest.fn();
|
||||
const numberOfDiscs = 2;
|
||||
|
||||
hanoiTower(2, moveCallbackMock);
|
||||
hanoiTower(numberOfDiscs, moveCallbackMock);
|
||||
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes(3);
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);
|
||||
|
||||
expect(moveCallbackMock.mock.calls[0][0]).toBe(1);
|
||||
expect(moveCallbackMock.mock.calls[0][1]).toEqual([1, 2]);
|
||||
@ -23,17 +24,19 @@ describe('hanoiTower', () => {
|
||||
|
||||
it('should solve tower of hanoi puzzle with 3 discs', () => {
|
||||
const moveCallbackMock = jest.fn();
|
||||
const numberOfDiscs = 3;
|
||||
|
||||
hanoiTower(3, moveCallbackMock);
|
||||
hanoiTower(numberOfDiscs, moveCallbackMock);
|
||||
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes(7);
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);
|
||||
});
|
||||
|
||||
it('should solve tower of hanoi puzzle with 6 discs', () => {
|
||||
const moveCallbackMock = jest.fn();
|
||||
const numberOfDiscs = 6;
|
||||
|
||||
hanoiTower(6, moveCallbackMock);
|
||||
hanoiTower(numberOfDiscs, moveCallbackMock);
|
||||
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes(63);
|
||||
expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user