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

  • SEARCH
  • Home
  • 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 695973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:00:54+00:00 2026-05-14T03:00:54+00:00

As part of the last assignment in a beginner python programing class, I have

  • 0

As part of the last assignment in a beginner python programing class, I have been assigned a traveling sales man problem. I settled on a recursive function to find each permutation and the sum of the distances between the destinations, however, I am have a lot of problems with references. Arrays in different instances of the Permute and Main functions of TSP seem to be pointing to the same reference.

from math import sqrt
    class TSP:
        def __init__(self):
            self.CartisianCoordinates = [['A',[1,1]], ['B',[2,2]], ['C',[2,1]], ['D',[1,2]], ['E',[3,3]]]
            self.Array = []
            self.Max = 0
            self.StoredList = ['',0]
        def Distance(self, i1, i2):
            x1 = self.CartisianCoordinates[i1][1][0]
            y1 = self.CartisianCoordinates[i1][1][1]
            x2 = self.CartisianCoordinates[i2][1][0]
            y2 = self.CartisianCoordinates[i2][1][1]
            return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))

    def Evaluate(self):
        temparray = []
        Data = []
        for i in range(len(self.CartisianCoordinates)):
            Data.append([])
        for i1 in range(len(self.CartisianCoordinates)):
            for i2 in range(len(self.CartisianCoordinates)):
                if i1 != i2:
                    temparray.append(self.Distance(i1, i2))
                else:
                    temparray.append('X')
            Data[i1] = temparray
            temparray = []
        self.Array = Data
        self.Max = len(Data)
    def Permute(self,varray,index,vcarry,mcarry): #Problem Class
        array = varray[:]
        carry = vcarry[:]
        for i in range(self.Max):
            print ('ARRAY:', array)
            print (index,i,carry,array[index][i])
            if array[index][i] != 'X':
                carry[0] += self.CartisianCoordinates[i][0]
                carry[1] += array[index][i]
                if len(carry) != self.Max:
                    temparray = array[:]
                    for j in range(self.Max):temparray[j][i] = 'X'
                    index = i
                    mcarry += self.Permute(temparray,index,carry,mcarry)
                else:
                    return mcarry
        print ('pass',mcarry)
        return mcarry
    def Main(self):
        out = []
        self.Evaluate()
        for i in range(self.Max):
            array = self.Array[:] #array appears to maintain the same reference after each copy, resulting in an incorrect array being passed to Permute after the first iteration.
            print (self.Array[:])
            for j in range(self.Max):array[j][i] = 'X'
            print('I:', i, array)
            out.append(self.Permute(array,i,[str(self.CartisianCoordinates[i][0]),0],[]))
        return out


SalesPerson = TSP()
print(SalesPerson.Main())

It would be greatly appreciated if you could provide me with help in solving the reference problems I am having. Thank you.

  • 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-14T03:00:55+00:00Added an answer on May 14, 2026 at 3:00 am

    Slicing a list (by using [:] as you do) does not create a deep copy — it creates a shallow copy. That means that if the list contains references to other lists, the copy will contain the same references — not references to new lists. Put another way, only the list itself is copied, not its elements or its elements’ elements.

    What you want here is a deep copy. Instead of

    array = self.Array[:]
    

    try

    array = copy.deepcopy(self.Array)
    

    for which you’ll need import copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of a new product I have been assigned to work on involves server-side
Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge
I have been trying for the last week to find a way to make
I have a string whose last part(suffix) needs to be changed several times and
I have a url and I want to extract the last part of my
I have the below, which has worked fine until I added the last part:
I have a service where the last part of the path is optional, the
This is what I've been trying, but something is wrong with the last part,
I'm on the last part of this assignment any help would be greatful thank
I have an app which is almost finished and the last part is where

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.