Beta

Tower of Hanoi

Description:

Your Task

Implement a solution for the Tower of Hanoi.

Tower of Hanoi in a nutshell

You begin with an array of arrays of length 3, and the first array with an x amount of floors (ascending order)

var next_step;
var tower = [[1, 2, 3, ..., x], [], []]

You can move one value at a time

next_step = [[2, 3, ..., x], [], [1]]

You cannot move a number on top of a smaller one

next_step = [[3, ..., x], [], [2, 1]] // This cannot happen since 2 is bigger than 1

The aim of the game is to move every value from the left-most array one by one to the right-most one, following the rules described above and (for this kata) the initial structure of the implementation explained below.

Implementation

For this Kata you will create a function hanoi that takes as a parameter the amount of disks the tower is going to have

var hanoi = function(disks) { }

This function will posses a nextStep function in which you'll work the logic to move each individual disk one call at a time

var tower = hanoi(3)
tower.nextStep() // [[2, 3], [], [1]]
tower.nextStep() // [[3], [2], [1]]

And once it arrives to the last step, if called again it should throw an error

var tower = hanoi(3)
/* Magic */
tower.nextStep() // [[], [], [1, 2, 3]]
tower.nextStep() // Error ==> Sequence contains no more elements.

More about the Tower of Hanoi

Puzzles
Mathematics
Algorithms

Similar Kata:

Stats:

CreatedMar 12, 2015
PublishedMar 12, 2015
Warriors Trained147
Total Skips7
Total Code Submissions128
Total Times Completed22
JavaScript Completions22
Total Stars10
% of votes with a positive feedback rating100% of 14
Total "Very Satisfied" Votes14
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments14
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • karudedios Avatar
  • monadius Avatar
  • dfhwze Avatar
Ad