Question
Still Rollin
You are given an integer n (the target sum).
You can roll a 6-sided dice any number of times, and each roll gives a value from 1 to 6.
Your task is to return all possible ways to form the sum n using dice rolls.
Each way must be represented as a list of integers, and the sum of the integers in each list must be exactly n. The order of values matters, since each value represents a dice roll.
Example:
For n = 2, the valid ways are:
[[1, 1], [2]]
Input
User Task:
Since this will be a functional problem, you don't have to take input. You just have to complete the function diceWays() that takes n as an integer.
Custom Input:
The only input line has an integer KaTeX can only parse string typed expression.
Since this will be a functional problem, you don't have to take input. You just have to complete the function diceWays() that takes n as an integer.
Custom Input:
The only input line has an integer KaTeX can only parse string typed expression.
Output
Return all possible ways where sum is exactly KaTeX can only parse string typed expression.
Example
Sample Input:
3
Sample Output:
[[1, 1, 1], [1, 2], [2, 1], [3]]
Explanation:
To form sum = 3, the valid sequences of dice throws are:
1 + 1 + 1 → keeps adding 1 three times
1 + 2 → throw 1, then 2
2 + 1 → throw 2, then 1
3 → one throw with value 3
These are all possible combinations using values from 1 to 6, whose total equals 3.
Sample Input:
4
Sample Output:
[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2], [1, 3], [3, 1], [4]]
Explanation:
All sequences of dice throws (each 1 to 6) whose total sum is exactly 4:
1 + 1 + 1 + 1
1 + 1 + 2
1 + 2 + 1
2 + 1 + 1
2 + 2
1 + 3
3 + 1
4
3
Sample Output:
[[1, 1, 1], [1, 2], [2, 1], [3]]
Explanation:
To form sum = 3, the valid sequences of dice throws are:
1 + 1 + 1 → keeps adding 1 three times
1 + 2 → throw 1, then 2
2 + 1 → throw 2, then 1
3 → one throw with value 3
These are all possible combinations using values from 1 to 6, whose total equals 3.
Sample Input:
4
Sample Output:
[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2], [1, 3], [3, 1], [4]]
Explanation:
All sequences of dice throws (each 1 to 6) whose total sum is exactly 4:
1 + 1 + 1 + 1
1 + 1 + 2
1 + 2 + 1
2 + 1 + 1
2 + 2
1 + 3
3 + 1
4