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:
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!
💡 Tip: For this challenge, we encourage you to make use of the Strings module's functions.
Input
Consider the following input payload (can be of txt format):
this is not a palindrome
blue
Mr. Owl ate my metal worm
Was it a car or a cat I saw?
you're doing great!
mug
Rats live on no evil star
what's taking so long?
kayak
Live on time, emit no evil
Step on no pets
boat
Anna
2020/02/02
15/03/2001
01/02/2010
Explanation of the problem
Each line of the input payload is considered a different phrase. Review each phrase and find those that are palindromes. Notice that punctuation, spaces, or special characters should not be considered to ensure a phrase is a palindrome.
For example,
Anna in reverse is anna - this is a palindrome.
2020/02/02 in reverse is 20200202 - this is a palindrome.
Mr. Owl ate my metal worm in reverse is mrowlatemymetalworm - this is a palindrome.
After that, retrieve the character count of the original phrase. Including punctuation, spaces, and special characters.
For example,
Anna is 4.
2020/02/02 is 10.
Mr. Owl ate my metal worm is 25.
Return the total sum of all the palindrome's character count.
Expected output
In this case, the expected output would be:
148
The result for each of the phrases (whether they're palindrome or not) should be:
false
false
true
true
false
false
true
false
true
true
true
false
true
true
false
true
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!
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 one of our solutions so you can compare your result with us.
Solution #1
Feel free to comment your code below for others to see! 😄
Subscribe to receive notifications as soon as new content is published ✨
%dw 2.0
import * from dw::core::Strings
import * from dw::core::Arrays
output application/json
fun getPalindromCount(text) = (lines(text) map ((sentence, index) -> do{
var cleanedSentence = lower(sentence mapString if (!isAlphanumeric($)) "" else $)
---
if(reverse(cleanedSentence) == cleanedSentence) sizeOf(sentence) else 0
}))sumBy $
---
getPalindromCount(payload)
%dw 2.0 import * from dw::core::Strings var payloadLines = lines(payload) var payloadLinesOnlyLetterAndNumbers = payloadLines map ( lower( $ replace /[^A-Za-z0-9]/ with "" ) ) var areTheyPalindromes = payloadLinesOnlyLetterAndNumbers map ( $ == reverse($) ) var onlyPalindromesLines = (areTheyPalindromes map ( if($) ( payloadLines[$$] ) else ( null ) )) filter $ != null output application/json --- /* Expected output: 148 */ sizeOf(onlyPalindromesLines joinBy "")
%dw 2.0 import * from dw::core::Strings fun isPalindrome(word) = word == reverse(word) fun formatWord(word) = lower(word replace /[^\w\d]/ with "") output application/json --- sum(lines(payload) map(if(isPalindrome(formatWord($))) sizeOf($) else 0))
Here is my approach:
%dw 2.0
output application/json
import * from dw::core::Strings
var cleanString = (aString) ->
lower (aString mapString if( !isAlphanumeric($)) "" else $)
---
lines(payload) map (
sizeOf(
if (cleanString($) == reverse(cleanString($))) $ else ""
)
)
reduce ($$ + $)