Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I append these entries to the list?
    primarykey
    data
    text
    <p>I have written a pair of methods in python:</p> <ol> <li><code>number_combinations()</code> which obtains all possible combinations of 3 numbers</li> <li><code>checkAllValuesAreDifferent()</code> which checks that all the values in the list are different</li> </ol> <p>-</p> <pre><code>temp = [] def number_combinations(listOfElements,index): if index==3: #if checkAllValuesAreDifferent(listOfElements): #print "Going to append the following list to the sat tuple list " print listOfElements #print "Temp is ",temp temp.append(listOfElements) print "Temp is ",temp print "exit from function " return listOfElements else: for value in range(3): listOfElements[index]=value #print "LIST OF ELEMENTS IS ",listOfElements if checkAllValuesAreDifferent(listOfElements,index,value)==True: #print "RECURSIVE CALL " (number_combinations(listOfElements,index+1)) #print "THE LIST IS ",temp #print "APPENDING TO SAT TUPLES " #print "BACKTRACK HAPPEND ",listOfElements #print "AND INDEX IS ",index #recursive call to next level return #backtrack since no number found def checkAllValuesAreDifferent(list_of,index,value): if index==0: return True else: for i in range(index): #print "Entered loop with index ",index if(list_of[i]==value): return False return True </code></pre> <p>When I just try printing the listOfElements, I get the right answer i.e all sets of 3 digits with each distinct. However, when I try appending to the list temp, I end up getting duplicate entries. So temp should be : <code>[0,1,2],[0,2,1][1,0,2][1,2,0][2,0,1][2,1,0]</code> (which is the result of printing alone).However,temp turns out to be <code>[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]</code></p> <p>I cannot figure out the problem with this. Could anyone give me some idea as to how to successfully append the right lists being generated to the temp list.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload