top of page

# DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number

Â

Other posts from this series:

Â

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!

## Input

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.

For example,

• 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 ðŸ˜‰)

## Expected output

In this case, the expected output would be:

`29`

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

Clue #5

Clue #6

Clue #7

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 some solutions we are providing so you can compare your result with us.

Solution #1

Solution #2

Solution #3

Solution #4

Feel free to comment your code below for others to see! ðŸ˜„

Did you like this challenge?? Let us know what you think!! ðŸ‘‡

Subscribe to receive notifications as soon as new content is published âœ¨

Tags:

## 20 commentaires

Kevin Medina

%dwÂ 2.0

importÂ *Â fromÂ dw::core::Arrays

outputÂ application/json

funÂ getMaxNumber(nums) =

max(nums splitBy "\n\n"Â map ((values, index) ->Â

Â Â Â Â values splitBy "\n"Â

Â Â Â Â sumBy \$))

---

J'aime

%dw 2.0 output application/json fun returnmax(a : Number,b : Number) : Number=if(a>b)a else b fun findMax(Data: String) : Number = findMax(Segregate(Data)) fun findMax(L : Array<Number>) :Number = L reduce (\$\$ returnmax \$) default 0 fun findMax(X:Any) : Number = 0 fun Segregate(Data : String) : Array<Number>= Segregate(Data splitBy "\n\n") fun Segregate(Data : Array) : Array<Number> = Data map sum(\$ splitBy "\n") --- findMax(payload)

J'aime

%dw 2.0 var pay = (read(payload, "text/plain") splitBy('\n\n')) map (\$ splitBy('\n')) output application/json --- max(pay map sum(\$))

J'aime

Disha Bhar
10 mai 2023

Hi, tried solving using random input.

J'aime
Alex Martinez
10 mai 2023

nice!

J'aime

%dw 2.0 output application/json --- max(payload splitBy ("\n\n") map ((\$ splitBy "\n" reduce \$ + \$\$) as Number))

J'aime
Alex Martinez
10 mai 2023