I hope I'm in the right section.(adsbygoogle = window.adsbygoogle || []).push({});

This part is not important but this is thought behind the program.

If you haven-mathematicians and each has a secret number. How many phone calls have to be made for all mathematicians to know all numbers? In this brute force approach I, hypothesize that you needmcalls and and try all combinations possible. When it works it'll print 1, if not it prints 0.

D represents what numbers each mathematician knows.

X represents the combination of phone calls chosen.

The bug is this:

D is a list of lists. X is a list.

The function at "def F(D,X):", makes A=D, makes some additions on A (eg: A+=A[j]), and then turns A into a list of sets. [set()...]

The loop at bottom repeats this process for all possible X. But somehow D is changed and turns into a list of sets, and since I can't add sets it stops! But why did D become a list of sets? I never tell D to change in the loop.

Code (Text):m=3

n=3

X=[]

Z=[]

D=[]

for i in range(0,2*m):

X.append(0)

Z.append(n-1)

for i in range(0,n):

D.append([i])

print 'X,Z,D=',X,Z,D

def F(D,X):

A=D

print 'D=',D #Here for debugging purposes:

print 'A=',A #Here for debugging purposes:

for i in range(0,m):

[COLOR="Red"]A[X[2*i]]+=[X[2*i+1]][/COLOR]

[COLOR="Red"]A[X[2*i+1]]=A[X[2*i]][/COLOR]

for i in range(0,n):

A[i]=set(A[i])

return A

def T(A):

s=0

for i in range(0,n-1):

if A[i]==A[i+1]:

s+=1

if s==n-1:

return 1

else:

return 0

i=2*m-1

while X!=Z:

if X[i]!=n-1:

if i==2*m-1:

print [COLOR="Red"]T(F(D,X))[/COLOR]

X[i]+=1

i=2*m-1

else:

if i==2*m-1:

print T(F(D,X))

X[i]=0

i-=1Output:

Code (Text):X,Z,D= [0, 0, 0, 0, 0, 0] [2, 2, 2, 2, 2, 2] [[0], [1], [2]]

D= [[0], [1], [2]]

A= [[0], [1], [2]]

0

D= [set([0]), set([1]), set([2])]

A= [set([0]), set([1]), set([2])]

Traceback (most recent call last):

File "blahblah.py", line 40, in <module>

print T(F(D,Z))

File "blahblah.py", line 19, in F

[COLOR="Red"]A[Z[2*i]]+=[Z[2*i+1]][/COLOR]

[COLOR="Red"]TypeError: unsupported operand type(s) for +=: 'set' and 'list'[/COLOR]

Press any key to continue . . .

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Python: Fun problem. Solving Program is Bugged. Why?

Loading...

Similar Threads for Python problem Solving |
---|

Python for vector files |

Practicing Python and R at the same time |

Python (VPython) Can someone help me with my orbit simulator? |

Python Some simple problem with my GUI? |

Python Socket problem in python |

**Physics Forums | Science Articles, Homework Help, Discussion**