Alex Martinez

Mar 7, 20232 min

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

Updated: Aug 31, 2023


Other posts from this series:

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

  2. DataWeave programming challenge #2: Rock Paper Scissors game score system

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

  4. DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle

  5. DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation

  6. DataWeave programming challenge #6: Using tail-recursion to get the factorial of a number

  7. DataWeave programming challenge #7: Modify certain values from a JSON structure

  8. DataWeave programming challenge #8: Sum all digits to get a 1-digit number


In this post:

  • Input

  • Explanation of the problem

  • Expected output

  • Clues

  • Answer


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!

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.

Feel free to comment your code below for others to see! 😄

Subscribe to receive notifications as soon as new content is published ✨

    59827
    4