top of page
Writer's pictureAlex Martinez

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



 

Other posts from this series:

 

In this post:

 


This challenge is based on Codeacademy's Reverse Words challenge

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


Hello world
May the Fourth be with you
Hello world!
With you, be May the Fourth
With you, be May the Fourth!


Explanation of the problem


Create a DataWeave script that will reverse the order of each one of the words in a phrase to form a new phrase. Each phrase or sentence is separated by a new line. Each word is separated by a space. However, the punctuation signs (! and ,) will have to stay in the same place. Lower/upper cases can be kept as-is.


For example:

  • "Hello world!" becomes "world Hello!"

  • "With you, be May the Fourth!" becomes "Fourth the, May be you With!"



Expected output


In this case, the expected output would be:


[
  "world Hello",
  "you with be Fourth the May",
  "world Hello!",
  "Fourth the, May be you With",
  "Fourth the, May be you With!"
]

ℹ️ Note: You can keep the output as a JSON or as a text, whatever your preference is.



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



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 my solution. I'm sure you all can make it better! :) Mine is super long 😂


Solution #1


I also recorded myself coming up with the solution, but without explanations. Just some lo-fi music and a screen :) you can leave the video in the background while working! :D let me know if you find this useful.



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


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








1,147 views14 comments

14件のコメント


sabrina collins
sabrina collins
10月14日

The personal statement services offered by Personalstatementwriter buy personal statement made the process of writing my essay a snap. People paid a lot of attention to me and my cause. After reviewing the final statement, I can say that it was helpful and that it covered all of my objectives. I was elated by their prompt response and meticulous consideration of my needs. I can't emphasize this point enough if you value getting your questions addressed and personalized attention.

いいね!

decipherMiddleware
2023年8月16日

My approach 🤓


%dw 2.0 import * from dw::core::Strings output application/json fun extract(text)= do { var list= words(text) var punc= list map (if(isAlphanumeric($)) "" else $[-1]) var rev= list[-1 to 0] map ((if(isAlphanumeric($)) $ else $[0 to -2]) ++ punc[$$]) --- rev joinBy(" ") } --- payload splitBy("\n") map (line,key)-> extract(line)

いいね!
Alex Martinez
Alex Martinez
2023年8月30日
返信先

noice!! thank you for sharing!

いいね!

Felix Schnabel
Felix Schnabel
2023年4月18日

My solution: https://gist.github.com/Shadow-Devil/c579da4418b135bd67869437bf999653

My first approach was wrong, I just inserted the special chars at the exact index where they were before 😂 So "a, bc" would be "b,c a" what's wrong of course

いいね!
Felix Schnabel
Felix Schnabel
2023年4月18日
返信先

I've updated it 😄

いいね!

Felix Schnabel
Felix Schnabel
2023年4月18日
返信先

Damn, that solution is so nice. I was kinda thinking in that direction but couldn't write it down

いいね!

raju kr
raju kr
2023年4月18日

%dw 2.0 output application/json var convert=((payload as String) splitBy ("\n")) map ((item, index) -> (item) ) --- convert map ((item, index) -> (item splitBy (" ") )[-1 to 0] joinBy (" ") )

いいね!
Alex Martinez
Alex Martinez
2023年4月18日
返信先

This is not the right answer :( Make sure you check the expected output!

いいね!
bottom of page