W
W
whoareyoutofuckinglecture2018-10-16 22:49:56
Python
whoareyoutofuckinglecture, 2018-10-16 22:49:56

Python3. Why doesn't the code work?

Hello!
There is a function:

def binary_symmetry(sieve):
    a = []
    for num in sieve:
        x = format(num, 'b')  # переводим num в двоичную систему
        if len(x) % 2 == 0:  # если длина двоичного числа - чётное число
            half = (len(x) // 2)  # то half равняется половине длины этого числа
            one = ('1' * half)
            zeroes = ('0' * half)
            if len(x) == 2 and (x[0] == '1' and x[1] == '0'):  #  ВОТ ЭТО УСЛОВИЕ РАБОТАЕТ
                a.append('{}:{}'.format(num, x))
            elif x[0:half] == one and x[half:] == zeroes:  #  А ЭТО НЕ РАБОТАЕТ :( 
                a.append('{}:{}'.format(num, x))
    return a

sieve is a list that contains prime numbers.
With this function, I'm trying to solve an educational problem of finding prime numbers, in which in binary form the first half will be ones, and the second half will be zeros.
For some reason, the condition elif x[0:half] == one and x[half:] == zeroes does not work:
What is my mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AWEme, 2018-10-16
@whoareyoutofuckinglecture

>>> int('10', 2)
2
>>> int('1100', 2)
12
>>> int('111000', 2)
56
>>> int('11110000', 2)
240
>>> int('1111100000', 2)
992

Judging by the conditions in your code, the resulting list will include the numbers I gave above and similar ones, and there are no simple ones (except for two).

D
Dimonchik, 2018-10-16
@dimonchik2013

for d in [x[0:half],one,x[half:],zeroes]
    print(d, type(d))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question