DataWeave programming challenge #6: Using tail-recursion to get the factorial of a 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!
Consider the following input payload (can be of txt format):
3 5 -6 300
Explanation of the problem
Create a tail-recursive function to get the factorial of each positive number from the payload.
Sum the results.
Retrieve the digits/characters located at positions 20-25.
Make sure to return a number and not a string.
Numbers 0 or less won't be calculated.
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.
In this case, the expected output would be:
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!
Use lines() from the Strings module or splitBy() to process every line.
Make sure to use tail-recursiveness and not regular recursiveness. Otherwise, your script might not work.
Make sure to create an if statement before calling the function in recursion. Otherwise, you might get stuck in an infinite loop!