On Saturday, 11 September 2021 at 19:57:26 UTC, jfondren wrote:
On Saturday, 11 September 2021 at 19:37:42 UTC, Vino wrote:
Hi All,

Request your help on the below to print the below array as "Required output", Was able to get these values "[1,2],[2,3],[3,4],[4,5]" by using list.slide(2), need your help to get values "1,3],[1,4],[1,5],[2,4],[2,5],[3,5]"

auto list[] = [1,2,3,4,5]

Required output
[1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5]

From,
Vino

I don't see the pattern just from the required output. If you have an English description of what you want, it might be easier.

Anyway, take a look at

```d
unittest {
    import std.algorithm : cartesianProduct, filter, map, sort;
    import std.range : drop;
    import std.array : array;

    auto list = [1,2,3,4,5];
auto required = [[1,2],[2,3],[3,4],[4,5],[1,3],[1,4],[1,5],[2,4],[2,5],[3,5]];
    auto pairs = list.cartesianProduct(list.drop(1))
        .filter!"a[0] < a[1]"
        .map!array
        .array;
assert(pairs == [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]);
    assert(required.sort.array == pairs);
}
```

Hi,

Thank you very much, let me try to explain the actual requirement, the actual requirement is to find all the permutations of a given array, I modified your code and it gives me the exact output , but i need help to remove the array which has the same value as below

Example:
void main() {
import std.algorithm : cartesianProduct, filter, map, sort;
import std.range : drop;
import std.array : array;
import std.stdio : writeln;

    auto list = [1,2,3,4,5];
    auto pairs = list.cartesianProduct(list)
        .map!array
        .array;
     writeln(pairs);
}

Output
[
[1, 1], /* shodule not print this array */
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[2, 1],
[2, 2], /* should not print this array */
[2, 3],
[2, 4],
[2, 5],
[3, 1],
[3, 2],
[3, 3], /* should not print this array */
[3, 4],
[3, 5],
[4, 1],
[4, 2],
[4, 3],
[4, 4], /* should not print this array */
[4, 5],
[5, 1],
[5, 2],
[5, 3],
[5, 4],
[5, 5] /* should not print this array */
]

From,
Vino

Reply via email to