DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number
In this post:
This challenge is based on Advent of Code 2022 day 1
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!
Consider the following input payload (can be of txt format):
1 2 3 4 5 0 15 20 5 1 3 25 6 4 4
Explanation of the problem
Create a DataWeave script to add all the numbers separated by a paragraph.
the first number would be 3 (2 + 1),
the second number would be 12 (3 + 4 + 5 + 0),
the third number would be 15, and so on.
After that, retrieve the highest number.
(Using code, not manually 😉)
In this case, the expected output would be:
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!
Use the splitBy() function
Instead of splitting by "\n" try splitting by "\n\n" at first - to separate by paragraph
Use map() to go through each paragraph
Use splitBy() again to separate each of the paragraph's lines
Use sum() to add all the numbers in each paragraph