Quote Originally Posted by vivek.1993 View Post
The solution works!!
Of course it works. Are you really surprised it does?

Quote Originally Posted by vivek.1993 View Post
But, is it possible to make the code work more dynamically? Like I ask the user to input the values of m and n, and also the sets A and B and display the onto functions for those sets?
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-N number (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 integer curr into a candidate solution in bref, with each position in bref mapping to the corresponding element in A. Since only those that map each element in B are real solutions, the used array elements will be set nonzero when the corresponding element in B is used in the candidate solution. The loop and test in lines 37-43 verify that each element in B is used at least once; otherwise the integer curr does not refer to a valid solution. Lines 48-50 print the solution.

Good luck.