Writin a Binary Function without bin function of python

This is the place for queries that don't fit in any of the other categories.

Writin a Binary Function without bin function of python

Hello,
I am trying to write a binary function, without using the bin function which is already in the python. I have written a code for it, but I am not understanding where the error is, it is not showing any error, but it is not giving me a correct answer.

Here is the code I wrote :
Code: Select all
`import randomdef convert2binary_form(number):    binary_form = [0] * number    binary_syntax = []    number_repeat = number    i = 1    while number_repeat != 0:        binary_syntax.append(i)        i = i * 2        number_repeat -= 1    binary_syntax.reverse()    if number in binary_syntax:        index = binary_syntax.index(number)        binary_form[index] = 1    if number in binary_syntax:        result = ''        final_binary_form = binary_form        for i in final_binary_form:            result = result + str(i)        return '0'+result+',' + str(number)    number_less_number = []    for num in binary_syntax:        if num < number:            number_less_number.append(num)    #first check if all the numbers add up to it    all_add = 0    for num in number_less_number:        all_add += num    if all_add == number:        s = 0        b = -1        while s < len(number_less_number):            binary_form[b] = 1            b -= 1            s +=1    if all_add == number:        result = ''        final_binary_form = binary_form        for i in final_binary_form:            result = result + str(i)        return '0'+result+',' + str(number)    # now check randomly    times = 1    limit = len(number_less_number) - times    while limit > 0:        lr = limit        while lr > 0:            track = 0            total = 0            collect_index = []            while track < lr:                choose_index = random.randint(0,lr)                choose_num = number_less_number[choose_index]                total += choose_num                collect_index.append(choose_index)                track += 1                if total == number:                    for each in collect_index:                        binary_form[each] = 1                    break            if total == number:                break            lr -= 1        if total == number:            break        times += 1        limit = len(number_less_number) - times                              final_binary_form = binary_form    #h = binary_form.index(1)    #hile ch < len(binary_form):    #   final_binary_form.append(binary_form[ch])    #  ch +=1    result = ''    for i in final_binary_form:        result = result + str(i)    return '0'+result+',' + str(number)                print convert2binary_form(1)print convert2binary_form(2)print convert2binary_form(3)print convert2binary_form(4)print convert2binary_form(5)print convert2binary_form(6)print convert2binary_form(7)print convert2binary_form(8)print convert2binary_form(9)print convert2binary_form(10)`

Here is the output I am having:
Once it shows :
Code: Select all
`01,1010,20011,300100,4010100,50000000,600000111,7000001000,80000000000,901010000000,10`

Then again it shows:
Code: Select all
`01,1010,20011,300100,4010100,50110000,600000111,7000001000,80000000000,900000000000,10`

It shows more different outputs.

Why am I having different output at different time? Please, could you tell me where the problem is. Sorry if this big question irritates you, I know my code is too big and might not look good, but I need help. Please give a reply.
Last edited by stranac on Sat Oct 19, 2013 6:27 pm, edited 1 time in total.
Reason: First post lock
rhemon

Posts: 1
Joined: Sat Oct 19, 2013 5:01 pm

Re: Writin a Binary Function without bin function of python

Well, you use random.randint(), which is where your randomness comes from. Why are you doing that? Are you disallowed from using bit operations, or modulus? What is the randomness supposed to be for?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA