Background: I am trying out doing a fun little monthly computational math puzzle.
I am at a stage that I have a list comprised of binary numbers and I am looking to write a python program that takes all the numbers in the list and appends a zero behind each digit and makes a new list.
1--> 10, 101---> 100010, 10---->1000.
CodePudding user response:
string approach
This is easily achieved using strings:
def add0(n):
s = str(n)
return '0'.join(list(s)) '0'
add0(101)
Output: 100010
numerical approach
You need to play with divmod
and powers of ten
def add0(n):
out = 0
exp = 0
while n>0:
n,r = divmod(n,10)
out = 10**(2*exp 1)*r
exp = 1
return out
add0(12345)
# 1020304050
CodePudding user response:
The easiest way for me was:
list = [1, 0]
new_list = []
for i in list:
new_list.append(i)
new_list.append(0)
print(list)
print(new_list)
CodePudding user response:
There are more efficient ways, but I just don't feel like thinking, so here it is, it adds 0 using for loop add it to list, then join all the elements from the list
while True:
ff = input("input disits: ")
ff2 = []
for i in ff:
ff2.append(i '0')
print(''.join(ff2))
Or another way is adding string
while True:
ff = input("input disits: ")
ff2 = ''
for i in ff:
ff2 = i '0'
print(ff2)
Want int answer? do print(int(''.join(ff2)))
or print(int(ff2))
or
while True:
print(int(''.join([i '0' for i in input("input disits: ")])))