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

Postby rhemon » Sat Oct 19, 2013 5:09 pm

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 random
def 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,1
010,2
0011,3
00100,4
010100,5
0000000,6
00000111,7
000001000,8
0000000000,9
01010000000,10

Then again it shows:
Code: Select all
01,1
010,2
0011,3
00100,4
010100,5
0110000,6
00000111,7
000001000,8
0000000000,9
00000000000,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.
Thanks in advance.
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

Postby micseydel » Sun Oct 20, 2013 6:37 am

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?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests