0

My code should get an array with zero or 1 values, if any element is 0, it should switch it to 1 and also switch the status of its divisors. The problem is that the second if seems not to work and it does not change the 1 status to 0. I don't get why, can anyone help me?

data = input()
L = list(data.split())
L2 = np.array(L,int)
L3 = np.zeros(len(L2),int)
for i in range(len(L2)-1, -1, -1):
    if ( L2[i] == 0) :
        L2[i] = 1
        L3[i] = 1

        for j in range(0, i) : 
            if ((i+1)%(j+1) == 0 & L2[j] == 0) :
                L2[j] = 1

            if ((i+1)%(j+1) == 0 & L2[j] == 1) :
                L2[j] =0  #this does not work



print(*L3)
2
  • 1
    & is a bitwise operator in python you should use and keyword in your case. Commented Nov 19, 2019 at 5:43
  • Can you share some example input and output? Please see: minimal reproducible example. Also, using so many loops when working with NumPy is a sign that something is wrong. Commented Nov 19, 2019 at 5:59

1 Answer 1

1

change:

  if ((i+1)%(j+1) == 0 & L2[j] == 1) :

to

 if ((i+1)%(j+1) == 0 and L2[j] == 1) :

the first one is a math operation. Also for debugging these type of issues i usually add a print item in the if statement to make sure it's working. I hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.