Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:02:15+00:00 2026-05-31T01:02:15+00:00

I have written a pair of methods in python: number_combinations() which obtains all possible

  • 0

I have written a pair of methods in python:

  1. number_combinations() which obtains all possible combinations of 3 numbers
  2. checkAllValuesAreDifferent() which checks that all the values in the list are different

–

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

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 : [0,1,2],[0,2,1][1,0,2][1,2,0][2,0,1][2,1,0]
(which is the result of printing alone).However,temp turns out to be [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:02:16+00:00Added an answer on May 31, 2026 at 1:02 am

    Because you are passing the same listOfElements back to the function, and it will be modified in place.

    You can use the [:] idiom to shallow-copy a list:

    temp.append(listOfElements[:])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an AIR Application that downloads videos and documents from a server.
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it
I have written a code that solves MST using Prim method. I read that
I have a written a simple Client-Server pair, sending an Object to the server.
I have written a code in Python to create a transition probability matrix from
I have a class which uses both ADO.NET and LINQ to access a pair

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.