top of page
Writer's pictureAlex Martinez

DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle



 

Other posts from this series:

 

In this post:

 


This challenge is based on the Tower of Hanoi mathematical puzzle - this is a tough one!

Try to solve this challenge on your own to maximize learning. We recommend you refer to the DataWeave documentation only. Try to avoid using Google or asking others so you can learn on your own and become a DataWeave expert!


⚠️ Important: There are 3 different scenarios to test your code is working properly and to ensure there are no hardcoded values. However, only the first scenario is provided in the Playground link below. Please copy+paste the other two scenarios to make sure your solution is working properly.



Input


Consider the following JSON inputs:


SCENARIO 1

{
    "moves": 0,
    "disks": 3,
    "targetTower": "C",
    "towers": {
        "A": [
            1,
            2,
            3
        ],
        "B": [],
        "C": []
    }
}

SCENARIO 2

{
    "moves": 0,
    "disks": 4,
    "targetTower": "C",
    "towers": {
        "A": [
            1,
            2,
            3,
            4
        ],
        "B": [],
        "C": []
    }
}

SCENARIO 3

{
    "moves": 0,
    "disks": 7,
    "targetTower": "Y",
    "towers": {
        "X": [],
        "Y": [],
        "Z": [
            1,
            2,
            3,
            4,
            5,
            6,
            7
        ]
    }
}


Explanation of the problem


Create a DataWeave script to solve the Tower of Hanoi puzzle. The input payload contains the number of moves (starting at 0), the total number of disks in the game, the target tower where all the disks have to be moved, and the 3 towers containing each disk.



(You can use this link to play the game online)


Here are the rules of the game:

  • With each move, you can only move one disk at a time from its current stack to a different stack.

  • You can only place smaller disks on top of bigger disks. For example, you can place disk 1 on top of disk 3 but you can't place disk 5 on top of disk 2.

  • You can only move the disk at the top of any stack.



Expected output


For each of the three scenarios from the input, these are the expected outputs:


SCENARIO 1

{
  "moves": 7,
  "disks": 3,
  "targetTower": "C",
  "towers": {
    "A": [
      
    ],
    "B": [
      
    ],
    "C": [
      1,
      2,
      3
    ]
  }
}

SCENARIO 2

{
  "moves": 15,
  "disks": 4,
  "targetTower": "C",
  "towers": {
    "A": [
      
    ],
    "B": [
      
    ],
    "C": [
      1,
      2,
      3,
      4
    ]
  }
}

SCENARIO 3

{
  "moves": 127,
  "disks": 7,
  "targetTower": "Y",
  "towers": {
    "X": [
      
    ],
    "Y": [
      1,
      2,
      3,
      4,
      5,
      6,
      7
    ],
    "Z": [
      
    ]
  }
}




Clues


If you're stuck with your solution, feel free to check out some of these clues to give you ideas on how to solve it!


Clue #1

Clue #2

Clue #3

Clue #4



Answer


If you haven't solved this challenge yet, we encourage you to keep trying! It's ok if it's taking longer than you thought. We all have to start somewhere ✨ Check out the clues and read the docs before giving up. You got this!! 💙


There are many ways to solve this challenge, but you can find here my solution. I'm sure you all can make it better! :) Mine is super long 😂


Solution


I also recorded myself coming up with the solution, but without explanations. Just some lo-fi music and a screen :) you can leave the video in the background while working! :D let me know if you find this useful.



Feel free to comment your code below for others to see! 😄


Subscribe to receive notifications as soon as new content is published ✨








811 views15 comments

15 Comments


Luciana Jones
Luciana Jones
4 days ago

Many academic programs include statistics as a required course, hence a student's whole academic performance can significantly affect whether or not they get excellent marks in the topic. Help with statistics homework may give students the direction and encouragement need to reach their academic objectives in this field of study. Having tutors highlight areas where students find difficulty and getting tailored instruction can allow them to better grasp the content under study. They also help students get ready for exams, which usually produces better test scores. Students who want pay someone to take my online math class service and have the chance to learn from subject-matter experts. Students are in excellent hands as tutors in statistics are highly knowledgeable and experienced teachers.…

Like

Students who have a full schedule of classes may find themselves with little time to complete tasks that have tight due dates. By choosing to use online resources such as java assignment helper in USA, students can achieve academic success and yet participate actively in class activities. Studying might be intimidating in the beginning, particularly when it comes to assignments that call for a thorough comprehension of the material and exceptional writing abilities. In order to get over this obstacle, students frequently buy online coursework.  Thorough planning and prudent time management are essential for handling coursework, including theses, studies, tasks, and thesis. Due to ill-planning, a lot of students, regrettably, lack time, which pushes them to get coursework assistance in…

Like

The Tower of Hanoi is a classic puzzle that really tests logical thinking and recursion skills! Solving it in DataWeave adds an interesting programming twist, especially for those deepening their skills in functional programming. Challenges like these are great practice, especially for students working on complex coursework. For anyone struggling with assignments like this, don’t hesitate to seek assistance; services that do my coursework for me can provide valuable guidance for tackling these problems efficiently!

Like

axgyfu
Oct 30

For anyone interested in creating games, understanding the planning process is crucial. This post gives a solid overview of project workflows, from initial concepts to final launch. Following these game development stages can greatly improve a team's efficiency and organization, helping to keep track of progress and avoid common pitfalls. Beginners and experienced developers alike will benefit from insights on balancing creativity with practical steps. Highly recommended for building a strong foundation in game development.

Like

mysg147
mysg147
Apr 23, 2023

Actually in this challenge, we are just asking minimum number of moves required to solve it.

So just simple logic can solve this. But as I saw your Solution you have performed algorithm. There are couple of things in that a beginer like me can't get like @Lazy. And one more thing in other programming languages we avoid to use "do {} " but here you are using it alot why? any advantages.

Like
bottom of page