please help me to reverse a function call order

Test123 test123 at gmail.com
Sat Jul 23 19:58:07 UTC 2022


On Saturday, 23 July 2022 at 19:50:51 UTC, Test123 wrote:
> I has this function, it print all possible combinations.
>
> ```d
> void doRepetition(const int n, const int m){
>     // combination total number is m,  element is repeatable.
>     assert(n >= 1 && n < 10);
>     assert(m >= 1 && m < 10);
>     enum N = 10;
>     ubyte[10] a;
>     void inc(int index, int r, int start, int end) {
>         if( index == r ) {
>             // get one unique combination result, not repeatable
>             for(int j =0; j < r; j++ ){
>                 printf("%d ", a[j]);
>             }
>             printf("\n");
>             return ;
>         }
>         for (int i = start; i < end; i++) {
>             a[index] = cast(ubyte)i;
>             inc(index + 1, r, i, end);
>         }
>     }
>     inc(0, m, 0, n);
> }
> ```
>
> `doRepetition(4, 3);` will print
> ```sh
> 0 0
> 0 1
> 0 2
> 1 1
> 1 2
> 2 2
> ```
>
>
> I need one function,  print the number in reversed order like 
> this:
>
> `doReverseRepetition(4, 3);` will print
> ```sh
> 2 2
> 1 2
> 1 1
> 0 2
> 0 1
> 0 0
> ```
>
> ont solution is to put the result in array, and foreach_reverse 
> it.  but this is slow and need a lot memory when N, M is 
> bigger. (9,9) will have 24310 restuls combinations. (I need 
> support bigger N,M late and need it fast)
>
> Any one can help me to write a function print the reversed 
> order results?

On Saturday, 23 July 2022 at 19:50:51 UTC, Test123 wrote:

The give example result is `doRepetition(3, 2);`.


`doRepetition(4, 3);` restuls is:

```sh
0 0 0
0 0 1
0 0 2
0 0 3
0 1 1
0 1 2
0 1 3
0 2 2
0 2 3
0 3 3
1 1 1
1 1 2
1 1 3
1 2 2
1 2 3
1 3 3
2 2 2
2 2 3
2 3 3
3 3 3
```

N is the unique number as results combination element.
result combination total emenent number is M.


More information about the Digitalmars-d-learn mailing list