top of page

DataWeave programming challenge #3: Count palindrome phrases using the Strings module



 

Other posts from this series:

 

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:

  1. false

  2. false

  3. true

  4. true

  5. false

  6. false

  7. true

  8. false

  9. true

  10. true

  11. true

  12. false

  13. true

  14. true

  15. false

  16. 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!


Clue #1

Clue #2

Clue #3

Clue #4

Clue #5

Clue #6

Clue #7



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 ✨








613 views27 comments

27 Comments


%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)

Like

Kelvin Fernandes
Kelvin Fernandes
Oct 23, 2023

%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 "")

Like

%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))

Like

shwetabh singh
shwetabh singh
May 06, 2023

Here is my approach:


Like
Alex Martinez
Alex Martinez
May 08, 2023
Replying to

love it. thank you for sharing!

Like

%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 ($$ + $)

Like
Alex Martinez
Alex Martinez
May 02, 2023
Replying to

sweet! thanks for sharing!

Like
bottom of page