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

In summary, 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[i]+=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.m=3n=3X=[]Z=[]D=[]for i in range(0,2*m):for
  • #1
Swimmingly!
44
0
I hope I'm in the right section.
This part is not important but this is thought behind the program.
If you have n-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 need m calls 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:
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-=1

Output:
Code:
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 . . .
 
Technology news on Phys.org
  • #2
This is a common stumbling block. You need to be very clear about the difference between mutable and immutable objects in Python. The problem is, A = D leaves A not as a copy of D, but as a reference to the actual D object. They're now just two ways of referencing the same data.

You'll need to create a new list from the other. Here's a way to do it:

A = list(D)

I strongly suggest you find more materials on how mutable and immutable objects, and references, work in Python. That'll help prevent some serious pain later, trust me. :)
 
  • #3
I tried what you said and changed it to:
Code:
def F(D,X):
	A=list(D)
	print 'D=',D
	for i in range(0,m):

but the output is:

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

ie D is still changing. ie A is still a "reference to D".
I will however read about these "mutable and immutable objects and references". I learned python through copying and testing so sometimes I end up being ignorant about these things. Thanks a lot for giving me a lead on the problem.

Edit: BOTH the codes are INCOMPLETE. I don't need to post it all. I posted what matters.
 
Last edited:
  • #4
Swimmingly! said:
ie D is still changing. ie A is still a "reference to D".
I will however read about these "mutable and immutable objects and references". I learned python through copying and testing so sometimes I end up being ignorant about these things. Thanks a lot for giving me a lead on the problem.

Ah, I haven't had time to go through your code in great detail, but remember that the method I gave you makes a "shallow copy" of the list, and any mutable objects in that list will still be the original references from the original list. That is, a list of lists, copied in this way, will be a new list containing the lists that were contained in the original one.

By the way, another method of making a shallow copy, for future reference, is A = D[:].

Anyways, try a deep copy using the copy module, which will make full copy of everything:

Code:
import copy

A = copy.deepcopy(D)

I'll check back in tomorrow in case you're still stuck. Good luck, and you're welcome! :)
 
  • #5


I can provide a possible explanation for the bug in this program. It seems that the problem lies in the line "A[X[2*i+1]]=A[X[2*i]]" in the "F" function. This line of code is attempting to set one element of the list A equal to another element, but since lists are mutable objects in Python, this actually changes the original list. This means that when the loop is repeated for a different combination of X, the original list D has already been changed and is now a list of sets instead of a list of lists. This causes the program to crash because sets and lists cannot be added together.

To fix this bug, the code could be rewritten to create a copy of the list A before making any changes to it. This can be done by using the "copy" module in Python or simply by using the slicing operator to create a new list with the same elements as the original list. This way, the original list D will not be affected by any changes made to A.

In addition, it is important to thoroughly test and debug programs before using them to solve complex problems. This includes checking for potential issues with data types and making sure that all variables are being used appropriately. It is also helpful to comment code and use descriptive variable names to make it easier to understand and troubleshoot.
 

Related to Python: Fun problem. Solving Program is Bugged. Why?

1. How do I find and solve bugs in my Python program?

To find and solve bugs in your Python program, you can use a debugging tool such as pdb or ipdb. These tools allow you to step through your code line by line and see the values of variables at each step. You can also use print statements to print out the values of variables at different points in your code to help identify where the bug may be occurring.

2. Why is my Python program not running correctly?

There could be several reasons why your Python program is not running correctly. One common reason is that there is a bug in your code. Other reasons could include missing or incorrect dependencies, incorrect syntax, or incorrect algorithm design. It's important to carefully review your code and use debugging techniques to identify and fix any issues.

3. How can I improve the performance of my Python program?

To improve the performance of your Python program, you can use various optimization techniques such as using built-in functions instead of for loops, using data structures such as dictionaries and sets instead of lists, and avoiding unnecessary computations. You can also use a profiler tool to identify bottlenecks in your code and optimize them.

4. Can I write efficient and bug-free code in Python?

While it is possible to write efficient and bug-free code in Python, it ultimately depends on the programmer's skills and experience. Python is a high-level language that prioritizes readability and ease of use, so it may not be the most performant language for certain tasks. However, with proper optimization techniques and careful debugging, it is certainly possible to write efficient and bug-free code in Python.

5. Are there any common mistakes that can lead to bugs in Python programs?

Some common mistakes that can lead to bugs in Python programs include incorrect use of indentation, incorrect variable scoping, and incorrect data type conversions. Other mistakes could include not checking for edge cases or not properly handling errors. It's important to pay attention to details and thoroughly test your code to avoid these common mistakes.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
28
Views
1K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
Replies
5
Views
997
Back
Top