26
Views
19
Comments
Solved
Cartesian Product Of N Lists of Lists
Question
Application Type
Reactive

Hello,

I have several Lists of Text Lists, the number of lists is variable and the text lists inside of this list is also variable. What i need to do is list all of possible combinations between the text lists of every list.

Example with 3 lists:

  • List 1: 
    • TextList :
      • "A"
      • "B"
      • "C"
  • List 2:
    • TextList:
      • "X"
      • "Y"
  • List3:
    • TextList:
      • "1"
      • "2"

Expected Result: 

  • List:
    • A,X,1
    • A,X,2
    • A,Y,1
    • A,Y,2
    • B,X,1
    • B,x,2
    • B,y,1
    • B,y,2
    • C,x,1
    • C,x,2
    • C,y,1
    • C,y,2

Already tried some for eachs, a recursive approach but still facing too many issues.

Thanks in advance.


2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

I'm not sure what you mean with recursivity, I don't think it is supported by Outsystems.

So here's something that works, and though it is satisfying to solve this kind op puzzle, I'm not sure if we aren't making things more difficult then they need to be.

If you just need to export the possible values for the dropdowns, not sure why you would need this kind of cartesian stuff.  Can you not just make some logic to go over each 'field', retrieve all possible values for that, and put that in the next column available in your excel, I'm not really getting it.

So first find all the distinc values of first column, then loop over these, find values of second column for each of them, and add them to the next column available in your excel.

Alternatively, if all of this is in your database, sql pivot might be something to look at too.

Dorine

QDR_CartesianForListOfListsV2.oml
2021-09-06 15-09-53
Dorine Boudry
 
MVP

If this is in working memory, not database, 3 nested forEach should do it for you.

What have you tried exactly, and what problem do you have ?

If in database, just joins without join conditions  can do this

Dorine

UserImage.jpg
Ruben Magalhães

Thanks Dorine for your answer.

It might be stored in database aswell but not sure if it has to be in Json yet.

well, i tried so many different things but right now i ended up with this.
Not quite right only does the first combination

2021-09-06 15-09-53
Dorine Boudry
 
MVP

ooh,

i kind of overlooked that you don't know how many lists you will have, sorry

so that's not as simple as nesting some loops or doing some joins.

Let me think on this.  Does it have to be on server side ?

2021-09-06 15-09-53
Dorine Boudry
 
MVP

also, 

besides solving this interesting hypothetical problem, why exactly do you need this, what is the use case.  Maybe you could approach from completely different angle.

UserImage.jpg
Ruben Magalhães

Basicly all of this combinations will be exported to an excel file, with dynamic number of columns, depends on the number of lists.
The use case for this is because we have dynamic forms that the user builds, and that forms can have dropdowns, basicly this lists are all of the lists for every dropdown.
based on the selected item for each dropdown we will have a combination, and based on that combination a calculation will be made.
Nesty i know...

UserImage.jpg
Ruben Magalhães

i also tried this table and then a cross join SQL but no success aswell.


Table Sample:

Query expected result: 

2021-09-06 15-09-53
Dorine Boudry
 
MVP

ok,

I'm afraid I'm not really understanding what exactly you want with this excel export, but I found a way of making the combinations, it is not sorted the way you would want, maybe, so that still needs some work

see attached oml, it basically calculates the number of combinations to make, and then goes this number of times over the whole outer list, keeping track of index to use in the inner list with an extra local list holding a counter and looping that integer to index into the inner list on each outer iteration.

Not sure about performance for large number of combinations, though

Dorine

QDR_CartesianForListOfLists.oml
UserImage.jpg
Ruben Magalhães

The order doesn't really matter for now.
Let me thank you for your help so far.
Analysing your example, seems to work fine for "Example1" but not for "Example2"
We see a combination for AX1 but not for AX2, and AX1 is repeated.
AY1 is not there and 2 times AY2.

But looks really good so far

2021-09-06 15-09-53
Dorine Boudry
 
MVP

ok,

back to the drawing board, I think this is really close, i'll have a look after coffee

UserImage.jpg
Ruben Magalhães

I feel im also close with recursivity, but not quite there yet 

2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

I'm not sure what you mean with recursivity, I don't think it is supported by Outsystems.

So here's something that works, and though it is satisfying to solve this kind op puzzle, I'm not sure if we aren't making things more difficult then they need to be.

If you just need to export the possible values for the dropdowns, not sure why you would need this kind of cartesian stuff.  Can you not just make some logic to go over each 'field', retrieve all possible values for that, and put that in the next column available in your excel, I'm not really getting it.

So first find all the distinc values of first column, then loop over these, find values of second column for each of them, and add them to the next column available in your excel.

Alternatively, if all of this is in your database, sql pivot might be something to look at too.

Dorine

QDR_CartesianForListOfListsV2.oml
UserImage.jpg
Ruben Magalhães

Seems to be working fine for those examples.
Will do some more tests and adapt to my reality to see, and mark as read.
This is just a requirement from the client, there are some more things behind the scene happening, and even me also dont have the big picture.

UserImage.jpg
Ruben Magalhães

I did not went into very deep testing , but did some changes to adapt to my reality and seems to work so far.

Im very thankful for your time and help!

Cheers!

Ps: consider doing a forge component with your solution;)

2021-09-06 15-09-53
Dorine Boudry
 
MVP

nah, not a forge component

as i said, it's a cute puzzle to solve, but I don't really see the business value / use case for this.


2018-06-05 16-54-03
Maria da Graça Peixoto

Hi! 

I know this is already solved but I found another solution that is cute too

Nice challenge. 

Graça

Cartesian.oap
UserImage.jpg
Ruben Magalhães

Hello,
Thank you, always good another point of view. The solution provided by Dorine is working amanzingly!

Althought, i was taking a peek into your solution but i cannot run it because of missing "testingservice"

2018-06-05 16-54-03
Maria da Graça Peixoto

Copies ...

Cartesian.oml
2018-06-05 16-54-03
Maria da Graça Peixoto

And the I also tested Ruben Magalhães idea of  recursion and it works.   

the only thing that must be avoided in he solution of this problem with lists  is the direct assignment from one list to other. It has always to be used the "AppendAll" or "Append" to pass the values from one level to another.

UserImage.jpg
Ruben Magalhães
Thank you Maria. For everyone interested I tested both versions of Dorine and Maria and both seems to work fine, up to you wich one you want to use. Thank you again Dorine and Maria for the support.
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.