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

- Mar 21, 2023
- 3 min read
Updated: Aug 31, 2023
Other posts from this series:
DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number
DataWeave programming challenge #2: Rock Paper Scissors game score system
DataWeave programming challenge #3: Count palindrome phrases using the Strings module
DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle
DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation
DataWeave programming challenge #6: Using tail-recursion to get the factorial of a number
DataWeave programming challenge #7: Modify certain values from a JSON structure
DataWeave programming challenge #8: Sum all digits to get a 1-digit number
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
Read the wikipedia page to have more context from the algorithm perspective.
Clue #2
From the wikipedia page, take special attention to this part:
For an even number of disks:
make the legal move between pegs A and B (in either direction),
make the legal move between pegs A and C (in either direction),
make the legal move between pegs B and C (in either direction),
repeat until complete.
For an odd number of disks:
make the legal move between pegs A and C (in either direction),
make the legal move between pegs A and B (in either direction),
make the legal move between pegs B and C (in either direction),
repeat until complete.
Clue #3
If the previous clue wasn't what you needed, take a look at these other rules from the wikipedia page:
Number the disks 1 through n (largest to smallest).
If n is odd, the first move is from peg A to peg C.
If n is even, the first move is from peg A to peg B.
Now, add these constraints:
No odd disk may be placed directly on an odd disk.
No even disk may be placed directly on an even disk.
There will sometimes be two possible pegs: one will have disks, and the other will be empty. Place the disk on the non-empty peg.
Never move a disk twice in succession.
Clue #4
If you see other solutions (in Python, C++, Java, etc) remember that those don't necessarily apply to DataWeave. Try to come up with your own solution instead.
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
%dw 2.0
import update from dw::util::Values
import drop, take from dw::core::Arrays
output application/json
var towerNames:Array<String> = namesOf(payload.towers)
var targetTower:String = payload.targetTower
var initialTower:String = payload.towers
pluck (if (!isEmpty($)) $$ else "")
filter ($ != "") then $[0] as String
var auxTower:String = (towerNames -- [targetTower, initialTower])[0]
fun moveDisk(towers:Object, fromTower:String, toTower:String) = do {
var from = towers[fromTower]
var to = towers[toTower]
---
towers
update log("Moved from", fromTower) with (from drop 1)
update log("to",toTower) with (from take 1)[0] >> to
}
fun swapDisks(towers:Object, tower1:String, tower2:String) = do {
var disk1 = towers[tower1][0] default (payload.disks + 1)
var disk2 = towers[tower2][0] default (payload.disks + 1)
---
if (disk1 < disk2)
moveDisk(towers, tower1, tower2)
else
moveDisk(towers, tower2, tower1)
}
fun play(game:Object, moveNumber:Number = 1) = do {
var isEvenGame = isEven(game.disks)
var isOddGame = isOdd(game.disks)
@Lazy
var newTowers =
if (
(isEvenGame and moveNumber == 1)
or (isOddGame and moveNumber == 2)
) swapDisks(game.towers, initialTower, auxTower)
else if (
(isEvenGame and moveNumber == 2)
or (isOddGame and moveNumber == 1)
) swapDisks(game.towers, initialTower, targetTower)
else swapDisks(game.towers, auxTower, targetTower)
@Lazy
var newMoveNumber = if (moveNumber == 3) 1 else moveNumber + 1
var newGame = game
update "towers" with newTowers
update "moves" with game.moves + 1
---
if (sizeOf(game.towers[targetTower]) == game.disks)
game
else
play(newGame, newMoveNumber)
}
---
play(payload)
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 ✨



Hồi sáng nay trong khi mình đang đọc các bình luận trao đổi trên mạng, mình thấy bu 88 được chèn vào và được mọi người nhắc tới nhiều. Mình bấm xem cho biết, để xem cách trình bày và cấu trúc nội dung. Lướt nhanh thì thấy tổng thể khá gọn gàng, tạo cảm giác đáng tin cậy. Với mình, chỉ cần nội dung gọn gàng như vậy là đủ để mình nắm bắt thông tin cơ bản.
Khi đọc các bài giới thiệu nền tảng giải trí, tôi thường thích những bài viết ngắn gọn để có thể xem nhanh trên điện thoại. Bài viết này có bố cục khá dễ theo dõi khi phần nhắc đến https://xx88x.cn.com/ được đặt ở giữa nội dung. Điều đó giúp mạch bài trở nên tự nhiên hơn và không tạo cảm giác quảng cáo quá sớm. Nội dung tập trung vào trải nghiệm chung của người dùng với giao diện dễ sử dụng và nhiều danh mục quen thuộc như slot, game bài hay mini game. Nhìn chung cách diễn đạt khá nhẹ nhàng và dễ hiểu.
Hôm nọ, khi đang lướt web, mình thấy nhiều người bàn về trang OPEN88 COM như một nguồn tin thể thao trực tuyến khá ổn. Thế là mình cũng ghé vào xem thử cách họ sắp xếp thông tin và trình bày dữ liệu. Mình không đi sâu vào từng chi tiết, chỉ dành chút thời gian để quan sát các mục và cách họ thể hiện thông tin trên trang. Thấy rằng nếu bố cục rõ ràng và dữ liệu bảng xếp hạng bóng đá được trình bày gọn gàng thì việc đọc lướt cũng không bị rối mắt; với mình như thế là đủ để lấy những thông tin cần thiết.
Every region in Vietnam has its own weather patterns, so having localized forecasts is extremely helpful. That’s one reason I like https://thoitiethomnay.org/. Users can quickly select their province and access updated weather information along with details about visibility, humidity, wind speed, and sunrise or sunset times. The website also shares informative weather-related content. Most importantly, thoitiethomnay.org is a weather platform dedicated to serving people throughout Vietnam.
I often read some experience-sharing articles. Compared to a few websites I used to visit before, I noticed Jiliace being mentioned, especially in sections related to online sports information, so I became curious and checked it out. My general impression is that if the information is organized clearly and is easy to view, then quick reading is quite convenient. For me, this might be a premium online entertainment brand.