1

I have a list of binary number and the bit need to be flipped from 0 to 1 or 1 to 0 for the prime index only. The list of binary number is actually near to 100 bit length. I have a code for example 8 bit long but it does not show the expected output:

def prime_index (input):
p = list(input)
s = ""

# Loop to check if
# index prime or not
for i in range (2, len(p) + 1):
    if isPrime(i):
        s = s + input[i-1]
        flip = {'0': '1', '1':'0'}
        flip=(flip[input[i]])
        print (flip)
print (s)
input="11111111"
prime_index(input)

I get the output like this:

0
0
0
0
1111

The result should show like this, where the bit flipped for the index of [2, 3, 5, 7]:

output:11001010

Hopefully someone may help me. Thank you.

Afir
  • 483
  • 3
  • 15

1 Answers1

0

that is because you are doing s = s + input[i-1] before you flip the input and you dont append the s when the index is not prime. try this.

def prime_index (input):
  p = list(input)
  s = ""

  # Loop to check if
  # index prime or not
  for i in range (0, len(p) - 1):
    if isPrime(i):
        flip = {'0': '1', '1':'0'}
        flip=(flip[input[i]])
        print (flip)
        s = s + flip
    else:
        s = s + input[i]
  print (s)

input="11111111"
prime_index(input)
Rafid
  • 641
  • 1
  • 8
  • 20