0

I'm having a weird problem with the following piece of code.

from math import sqrt
def Permute(array):
 result1 = []
 result2 = []
 if len(array) <= 1:
 return array
 for subarray in Permute(array[1:]):
 for i in range(len(array)):
 temp1 = subarray[:i]+array[0]+subarray[i:]
 temp2 = [0]
 for num in range(len(array)-1):
 temp2[0] += (sqrt(pow((temp1[num+1][1][0]-temp1[num][1][0]),2) + pow((temp1[num+1][1][1]-temp1[num][1][1]),2)))
 result1.append(temp1+temp2)
 return result1
a = [['A',[50,1]]]
b = [['B',[1,1]]]
c = [['C',[100,1]]]
array = [a,b,c]
result1 = Permute(array)
for i in range(len(result1)):
 print (result1[i])
print (len(result1))

What it does is find all the permutations of the points abc and then returns them along with the sum of the distances between each ordered point. It does this; however, it also seems to report a strange additional value, 99. I figure that the 99 is coming from the computation of the distance between point a and c but I don't understand why it is appearing in the final output as it does.

asked Apr 6, 2010 at 5:12
5
  • Could you post the output? And it seems result2 is pointless. Commented Apr 6, 2010 at 5:32
  • 2
    As a beginner, you seem to focus on code that is both somewhat arcane (possibly unnecessarily so) and for which you do not even seem to know the purpose... What's the point? Commented Apr 6, 2010 at 5:39
  • 1
    @mjv: The point is Protean wants to do it. Why ask the OP why they want to write the code they present unless it helps form an answer to their question? Everyone learns to program somehow, and this is what the OP is doing as a part of learning. Commented Apr 6, 2010 at 6:43
  • @vgm64: I'm a libertarian at heart and also quite aware that people learn different things in different ways! My remark to to Protean was merely a hint that the code snippet used here and in other questions of his/her are neither representative of particularly well crafted code nor appears to be commensurate with his/her current mastery of Python. As said, this is simply a hint, the type of hints which I believe makes SO an better place; while they sometimes seek a narrow answer to their specific question, OPs often appreciate suggestions of alternative approaches. Commented Apr 6, 2010 at 13:10
  • @mjv: Agreed. I've linked to Idiomatic Python on another one of Protean's posts without suggesting at all an answer to the question at hand =). @Protean, have you read IP today? Or rather, how many times? Get to it! Commented Apr 6, 2010 at 18:33

3 Answers 3

2

The problem is that you recursively call Permute(array[1:]), then use the recursive result to calculate temp1. Why is this a problem? Your function outputs an array of arrays, where the last subarray is temp2, the distance sum. So, every level of recursion, you will add more and more extra distances to your final result.

If you really want to calculate all the permutations and distances in the same function, then I suggest you return a tuple (permutation, distance). You can then use the first part of the tuple when assigning temp1, so that you don't accidentally add in extra distances. See this page if you aren't familiar with tuples.

Bill the Lizard
408k213 gold badges579 silver badges892 bronze badges
answered Apr 6, 2010 at 6:01
Sign up to request clarification or add additional context in comments.

Comments

0

I agree with what Justin Ardini said, but I would also suggest that you learn to use a Python debugger like pdb. Then you can go through functions like this and figure out what is going on for yourself. You'll learn a lot more that way.

answered Apr 6, 2010 at 6:09

Comments

0

I think what you're trying to do is:

from math import sqrt
def Permute(array):
 result1 = []
 result2 = []
 if len(array) <= 1:
 for subarray in array:
 for i in range(len(array)):
 temp1 = subarray[:i]+array[0]+subarray[i:]
 temp2 = [0]
 for num in range(len(array)-1):
 temp2[0] += (sqrt(pow((temp1[num+1][1][0]-temp1[num][1][0]),2) + pow((temp1[num+1][1][1]-temp1[num][1][1]),2)))
 result1.append(temp1+temp2)
 return result1
a = [['A',[50,1]]]
b = [['B',[1,1]]]
c = [['C',[100,1]]]
array = [a,b,c]
result1 = Permute(array)
for i in range(len(result1)):
 print (result1[i])
print (len(result1))

I changed:

if len(array) <= 1:
 return array
for subarray in Permute(array[1:]):

to:

if len(array) <= 1:
 for subarray in array:
answered Apr 6, 2010 at 8:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.