Home>

### python - i want to divide a binary number into two, store it in a list, and represent it in a different decimal number

The decimal number received from the user is converted into a decimal number and divided into two.
I want to store it in a list and represent it in a different decimal number.

For example, when 424896 is converted to binary, it becomes as follows.
1100111101111000000
If 1 or 0 is 2 consecutive times, this number
Decode them to 1 or 0 (eg 11 → 1, 00 → 0).
Continue it until a single 0 (separator role) comes, and if you hit it (eg 000, 11011),
Decode until the end again.
If i store the above two antilogarithms in this way, you get [1011, 11000].
From here, if you convert it to a decimal number, the output will be
It will be [11, 24]. Also, numbers ending in a single 1 or 0, such as 7 (111) and 24 (11000), are
Does not decode.

Corresponding source code
``````def get_list (bit):
l = []
bit = str (bit)
bit_sliced ​​= bit [:: 2] # 11 → 1, 00 → 0
while bit == bit [len (bit) // 2]: # Determining the center
l.append (int (bit_sliced))
break
l [1] = ","
l.append (int (bit_sliced))
return l

n = 1100111101111000000
def get_list (bit):
l = []
l2 = []
bit = str (bit)
bit_s = bit [: len (bit) ///2]
bit2_s = bit [:: 2]
l.append (int (bit_s))
l2.append (int (bit2_s))
return l2 + l
print (get_list (n))
out: out:
None
[1011011000, 110011110]``````
What I tried

First, I changed the type from Int to str and tried various things. The while one will be None.
I also found 000 in .split (",") and regular expression re.match, and tried replace, but it didn't work.
I'm still new to python, but thank you.

I'm sorry, the code that was written was difficult, so I wrote it in a different form. for your information.
I added it because I overlooked that it is ignored if the end ends with a single 0 or 1 (= an odd number of 0s or 1s are lined up at the end).
By the way, what if there is a single 1 other than the end ...? For example, "10011".

``````def sep0 (n):
ns = str (n) .replace ("101", "1,1"). split (",")
result = []
for i, t in enumerate (ns):
if judge_odd (t):
continue
result.append (t)
return result
def judge_odd (txt):
if len (txt.replace ("10", ", 0"). split (",") [-1])% 2! = 0:
return True
elif len (txt.replace ("01", ", 1"). split (",") [-1])% 2! = 0:
return True
else: else:
return False
def doublet_to_single (txt):
result = ""
i = 0
while i<len (txt)-1:
if txt [i] == txt [i + 1]:
result + = txt [i]
i + = 1
i + = 1
return result
n = "11101100111101111000000"
sep = sep0 (n)
print (sep)
# ['11001111', '1111000000']
single = [doublet_to_single (i) for i in sep]
print (single)
# ['1011', '11000']
dec = [int (r, 2) for r in single]
print (dec)
# [11, 24]``````

It seems that you should look at each of them as shown below and process them separately.

``````def conv (s):
ret = []
one =''
while True:
c = s [: 2]
st = 2
if c == '01': # Break in the middle
ret.append (one)
one =''
st = 1
elif c == '00': # 0
one + = '0'
elif c == '11': # 1
one + = '1'
elif c =='': # Exactly at the end
if len (one)>0:
ret.append (one)
break
else: # unintended tail
break
s = s [st:]
return ret
s = bin (424896) [2:]
print (conv (s)) # ['1011', '11000']
for s in ['', '0', '1', '00', '11', '111', '11000']:
ret = conv (s)
print (s, ret)``````

Trends