Of course it works. Are you really surprised it does?

Ah, I see you didn't understand the code after all. That was what I feared. The answer is yes, and it is in fact trivial: you would already know this, if you understood how the code works.

I'm not interested in helping you complete your homework. My interestest is limited to helping you understand and learn. You obviously dislike my approach, and would instead have preferred something that manipulated lists recursively, or something more in line with your expectations. I can only try to explain and show the technique that I've found to work; if you aren't interested in learning it, then I cannot help you.

Let me try one final time:

The key in the solution is to order the solutions (or like here, the superset containing all solutions). You number the solutions using a single integer. The trick is finding a way to describe each solution (or possible solution) using a single integer. Usually you simply interpret the integer as a base-Nnumber (or using varying base as needed), each digit specifying one feature of the solution. Optimally you can find a one-to-one mapping between integers and the solutions. Sometimes, like here, you'll need to verify each candidate solution, and skip non-solutions.

Lines 19-31 in my example convert integercurrinto a candidate solution inbref, with each position inbrefmapping to the corresponding element inA. Since only those that map each element inBare real solutions, theusedarray elements will be set nonzero when the corresponding element inBis used in the candidate solution. The loop and test in lines 37-43 verify that each element inBis used at least once; otherwise the integercurrdoes not refer to a valid solution. Lines 48-50 print the solution.

Good luck.