top of page

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

Writer's picture: Alex MartinezAlex Martinez


 

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!




Input


Consider the following input payload (can be of txt format):


3
5
-6
300


Explanation of the problem


  1. Create a tail-recursive function to get the factorial of each positive number from the payload.

  2. Sum the results.

  3. Retrieve the digits/characters located at positions 20-25.

  4. Make sure to return a number and not a string.

  5. Numbers 0 or less won't be calculated.


For example:

  • The factorial of 3 is 6 (3 x 2 x 1)

  • The factorial of 5 is 120 (5 x 4 x 3 x 2 x 1)

  • The factorial of -6 won't be calculated so the result will be 0

  • And so on

  • Once the results are summed (6 + 120 + 0...), extract the digits at indexes 20 to 25.

  • Return this last 6-digit number.



Expected output


In this case, the expected output would be:


537046




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



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 my solutions!


Solution #1


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


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








573 views13 comments

13件のコメント


For those using dw::core::Arrays slice it excludes the last index, so select 20-26

いいね!

%dw 2.0 output application/json fun solve(x)= x reduce(item, acc) -> ( (if(item > 0) acc + factorial(item) else acc)) fun factorial(x)= 2 to x reduce(item, acc) -> (acc*item) --- (solve(payload splitBy('\n')) splitBy(''))[20 to 25] joinBy('')

いいね!

Nirmala Vairaganthan
Nirmala Vairaganthan
2023年5月10日

%dw 2.0 fun factorial(num)= num to 1 reduce ((item, accumulator) -> accumulator * item) fun pos(val) = val[20 to 25] --- pos(sum((payload splitBy "\n" ) map factorial($))) as Number

いいね!
Alex Martinez
Alex Martinez
2023年5月10日
返信先

Another person using reduce :D haha nice job!

いいね!

Disha Bhar
Disha Bhar
2023年5月10日

Tried this out. :)



いいね!
Alex Martinez
Alex Martinez
2023年5月10日
返信先

awesome!!

いいね!

Felix Schnabel
Felix Schnabel
2023年5月09日

https://gist.github.com/Shadow-Devil/38922025e3ab95f18bc21633eae5f27f

いいね!
Alex Martinez
Alex Martinez
2023年5月09日
返信先

nice!! thank you for sharing!

いいね!

Join our mailing list

Thanks for subscribing!

  • Youtube
  • GitHub
  • LinkedIn
bottom of page