-2

I have this code in python, when I print the last line, it is giving an output "11100101100". I'm expecting the output,"011100101100". Notice that the output starts with 1 and not 0. although the variable gamma_sum_list is a list containing 12 digits and its starts with 0. The function somehow deletes the first zero automatically. The following is the exact gamma_sum_list:

def convert(list)
   res = int("".join(map(str,list)))
   return res
print(convert(gamma_sum_list))

Input:

[0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]

Expected Output:

011100101100

Actual Output :

11100101100

3 Answers3

0
def convert(some_list):


   res = "".join(map(str,some_list))
   return res

gamma_sum_list = [0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]


print(convert(gamma_sum_list))

or

conv = lambda x: ''.join(map(str, x))
print(conv(gamma_sum_list))
shivankgtm
  • 1,207
  • 1
  • 9
  • 20
0

Your issue is caused by converting the result of the join operation to an integer. Integers do not have leading zeroes. If you remove the int function you'll get a string with the leading zero you're after.

gamma_sum_list = [0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]

def convert(my_list):
res = "".join(map(str,my_list))
return res

print(convert(gamma_sum_list))

Output:

011100101100
ljdyer
  • 1,946
  • 1
  • 3
  • 11
0

Consider that:

>>> "".join(list(map(str, [0, 1])))
'01'

How would you convert '01' to an integer? Well, its just 1.

>>> int("".join(list(map(str, [0, 1]))))
1

So you probably want to not convert the string to an int, just keep it as a str.

L.Grozinger
  • 2,280
  • 1
  • 11
  • 22