Add Tower of Hanoi.

This commit is contained in:
Oleksii Trekhleb 2018-05-14 09:00:42 +03:00
parent 44b0a99a80
commit 8c102a3f62
2 changed files with 10 additions and 7 deletions

View File

@ -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

View File

@ -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);
});
});