Some dataweave challenge

 



  1. To check if a given number is a palindrome.

A palindromic number is a number (such as 16461) that remains the same when its digits are reversed.

%dw 2.0
output application/json

fun isPalindrome(number) =
number as String == number[-1 to 0] as String

// Number to check
var num = 12321

---
{
"Number": num,
"IsPalindrome": isPalindrome(num)
}

Output :

{
"Number": 12321,
"IsPalindrome": true
}

2. To check if a given number is an Armstrong number.

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

%dw 2.0
output application/json

fun isArmstrongNumber(n) =
((n as String splitBy "") map (($ as Number) pow sizeOf(n))) reduce ($ + $$)

// Example number to check
var num = 1634
---
{
"Number": num,
"IsArmstrongNumber": (num == isArmstrongNumber(num))
}

Output:


{
"Number": 1634,
"IsArmstrongNumber": true
}

3. To count vowels in a string.

Code to count vowels in a string.

%dw 2.0
output application/json
var vowel = ['A','E','I','O','U','a','e','i','o','u']


fun countVowels(s) =
sizeOf((s splitBy "") filter (vowel contains $))

var inputString = "Hello, World!"

---
{
"InputString": inputString,
"VowelCount": countVowels(inputString)
}

Output:

{
"InputString": "Hello, World!",
"VowelCount": 3
}

4. To determine if a given number is a perfect square.

We can determine if a given number is a perfect square by checking if its square root is an integer.

%dw 2.0
output application/json

fun isPerfectSquare(num) = (num pow (0.5 as Number)) pow 2

var inputNumber = 256

---
{
"InputNumber": inputNumber,
"IsPerfectSquare": (inputNumber== isPerfectSquare(inputNumber))
}

Output:

{
"InputNumber": 256,
"IsPerfectSquare": true
}

5. To calculate the factorial of a number.

Using recursion we can calculate the factorial of a given number.

%dw 2.0
output application/json

fun factorial(n) =
if (n <= 1) 1
else n * factorial(n - 1)

var inputNumber = 5

---
{
"InputNumber": inputNumber,
"Factorial": factorial(inputNumber)
}

Output:

{
"InputNumber": 5,
"Factorial": 120
}

6. To print Fibonacci series up ton numbers.

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1.

%dw 2.0
output application/json

fun fibonacci(n) =
if (n <= 1) n
else fibonacci(n - 1) + fibonacci(n - 2)

fun generateFibonacciSeries(limit) =
(0 to limit) map fibonacci($)

var inputLimit = 10

---
{
"InputLimit": inputLimit,
"FibonacciSeries": generateFibonacciSeries(inputLimit)
}

Output:

{
"InputLimit": 10,
"FibonacciSeries": [
0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55
]
}

7. To print all prime numbers up to numbers.

A prime number is a natural number greater than 1 which has only two factors, 1 & the number itself.

%dw 2.0

output application/json skipNullOn="everywhere"

fun isPrime(n)=((2 to n-1) map ((item, index) -> n mod item) contains 0)

fun generatePrimes(limit) =
(2 to limit) map (if(!isPrime($)) $ else null)

var inputLimit = 30

---
{
"InputLimit": inputLimit,
"PrimeNumbers": generatePrimes(inputLimit)
}

Output:

{
"InputLimit": 30,
"PrimeNumbers": [
3,
5,
7,
11,
13,
17,
19,
23,
29
]
}

8. To reverse each element of an array.

%dw 2.0
output application/json
var inputArray = [12345,"Mulesoft","1098",2987,"ABC"]
---
inputArray map(if(typeOf($)== Number)
(($ as String)[-1 to 0] as Number)
else if (typeOf($)== String)
($[-1 to 0])
else $)

Output:

[
54321,
"tfoseluM",
"8901",
7892,
"CBA"
]

9. To find the second largest number in an array.

%dw 2.0
output application/json

fun findSecondLargest(arr) = (arr orderBy -$)[1]

var inputArray = [5, 8, 2, 10, 7]

---
{
"InputArray": inputArray,
"SecondLargest": findSecondLargest(inputArray)
}

Output :

{
"InputArray": [
5,
8,
2,
10,
7
],
"SecondLargest": 8
}

10. To display the Longest String in an array list.

Given a list of strings in an array arr[] of size N, display the longest name contained in it. If there are multiple longest names print all of that.

%dw 2.0
output application/json
var namesList = ["Alice", "Bob", "Charlie", "David", "Eleanor"]

var maxLength = namesList maxBy ((name) -> sizeOf(name))

fun findLongestNames(names) = names filter ((name) -> sizeOf(name) == sizeOf(maxLength))

---
{
"NamesList": namesList,
"LongestNames": findLongestNames(namesList)
}

Output:

{
"NamesList": [
"Alice",
"Bob",
"Charlie",
"David",
"Eleanor"
],
"LongestNames": [
"Charlie",
"Eleanor"
]
}

11. To check if the number is BuzzNumber or not.

A buzz number isa special number that ends with the digit 7 or is divisible by 7.

%dw 2.0
output application/json

fun isBuzzNumber(num) =
((num mod 7) == 0) or (num endsWith "7")

var inputNumber = 56

---
{
"InputNumber": inputNumber,
"IsBuzzNumber": isBuzzNumber(inputNumber)
}

Output:

{
"InputNumber": 56,
"IsBuzzNumber": true
}

12. To determine whether the two strings given are anagrams.

If two strings have the same set of characters in a different order, they are anagrams.

%dw 2.0
output application/json

fun areAnagrams(str1, str2) =
(str1 splitBy "" orderBy $) == (str2 splitBy "" orderBy $)

var string1 = "listen"
var string2 = "silent"

---
{
"String1": string1,
"String2": string2,
"AreAnagrams": areAnagrams(string1, string2)
}

Output:

{
"String1": "listen",
"String2": "silent",
"AreAnagrams": true
}

13. Find the number of occurrences of a character in a String.

%dw 2.0
output application/json

fun countOccurrences(str, character) =
sizeOf((str splitBy character)) - 1

var inputString = "Hello, Mulesoft Community!"
var characterToCount = "o"

---
{
"InputString": inputString,
"CharacterToCount": characterToCount,
"OccurrencesCount": countOccurrences(inputString, characterToCount)
}

Output:

{
"InputString": "Hello, Mulesoft Community!",
"CharacterToCount": "o",
"OccurrencesCount": 3
}

14. To remove all occurrences of a given character from the input string.

%dw 2.0
output application/json

fun removeCharacter(str, character) =
str replace character with ""

var inputString = "Hello, World!"
var characterToRemove = "o"

---
{
"InputString": inputString,
"CharacterToRemove": characterToRemove,
"StringWithoutCharacter": removeCharacter(inputString, characterToRemove)
}

Output:

{
"InputString": "Hello, World!",
"CharacterToRemove": "o",
"StringWithoutCharacter": "Hell, Wrld!"
}

15. To print below number pattern.


"1",
"1 2",
"1 2 3",
"1 2 3 4",
"1 2 3 4 5",
"1 2 3 4 5 6"

DataWeave’s default behavior in outputting JSON includes escaping characters like newlines to maintain JSON structure. To display the pattern as intended, you can use the write() function in DataWeave that allows printing directly to the console. Adjust the rows variable to change the number of rows in the pattern.

%dw 2.0
output application/java

fun printPattern(n) =
(1 to n) map ((row) ->
((1 to row) map ((col) -> col) joinBy " ")
)

// Generate the pattern with 6 rows
var rows = 6

---
write(printPattern(rows))

Output:

[
"1",
"1 2",
"1 2 3",
"1 2 3 4",
"1 2 3 4 5",
"1 2 3 4 5 6"
]





Comments