I have been working on “Complete Search with Recursion” in Bronze.
I am having trouble understanding it though.
Note I do use python (and this is contributing to my confusion, as the resources like the textbook are a lot less helpful if you don’t know what the programs mean (but all I can do in C++ is std::cin, std::cout and assign integer variables )).
Anyway, so with generating subsets recursively: I understood what they did with the apple problem. Essentially at every new index of the list with the apple weights, they plug it back into the code until finally they have tested all the possibilities, and then at each step find the minimum.
But I can’t figure out how you would recursively make subsets of an array in python.
I know at each index of the given array, you would decide whether or not to include it. Here’s my attempt though:
def func(subList, k, setList): #For each k decide whether or not to include it if k == len(subList): return setList else: setList.append([k, func(subList, k+1, setList)]) setList.append(func(subList, k+1, setList)) print(func([1,2,3], 0,))
This doesn’t work though. I think I’m having some trouble wrapping my head around recursion.
As for bitmasks… I don’t understand them at all…
Can someone please explain what they are doing there?
In the program shown, it loops through n, which means that the nth bit is turned on or something??? And then the & operator returns what value? Just any value? It says “positive value.”
I really don’t understand these .
Thank you all for taking the time to read this long post. I want to make sure I understand this stuff. Sorry if I’m being stupid or something too .
Thanks a lot!