Skip to main content
deleted 60 characters in body
Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71

In general, your idea is notnot bad.

The best solution, as far as I read, would be anthe algorithm called Divide by 2 that uses a stackstack to keep track of the digits for the binary result.

  1. Why did you do this: dec_new = dec_number ? You could've just use dec_number. There's no need of assigning its value to another variable.
  2. This: dec_new = dec_new // 2 could be as well as the above line of your code rewritten as: dec_new //= 2
  3. The indentation should contain 4 spaces, not 2.
  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 and if it is odd append a 1.

I'll just timeit these in a couple of minutes to test the performance of each.

In general, your idea is not bad.

The best solution, as far as I read, would be an algorithm called Divide by 2 that uses a stack to keep track of the digits for the binary result.

  1. Why did you do this: dec_new = dec_number ? You could've just use dec_number. There's no need of assigning its value to another variable.
  2. This: dec_new = dec_new // 2 could be as well as above rewritten as: dec_new //= 2
  3. The indentation should contain 4 spaces, not 2.
  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 if it is odd append a 1.

I'll just timeit these in a couple of minutes to test the performance of each.

In general, your idea is not bad.

The best solution, as far as I read, would be the algorithm Divide by 2 that uses a stack to keep track of the digits for the binary result.

  1. Why did you do this: dec_new = dec_number ? You could've just use dec_number. There's no need of assigning its value to another variable.
  2. This: dec_new = dec_new // 2 could be as well as the above line of your code rewritten as: dec_new //= 2
  3. The indentation should contain 4 spaces, not 2.
  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 and if it is odd append a 1.
added 24 characters in body
Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71

A version that better utilizes the memory would be:

def dec_to_bin(n):
    bits = []

    bits.append(str(0 if n%2 == 0 else 1))
    while n > 1:
        n = n // 2
        bits.append(str(0 if n%2 == 0 else 1))

    bits.reverse()
    return ''.join(bits)

What I did:

  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 if it is odd append a 1.

Other ways of doing it:

###Using recursion:

def dec_to_bin(x):
    return dec_to_bin(x/2) + [x%2] if x > 1 else [x]

The above solution returns the result as a list which you can later on .join() then apply int() to it.

Another idea that came to my mind is as simple as:

u = format(62, "08b")
>> 00111110

The best solution, as far as I readas far as I read, would be an algorithm called Divide by 2 that uses a stack to keep track of the digits for the binary result.

A version that better (than your solution) utilizes the memory would be:

def dec_to_bin(n):
    bits = []

    bits.append(str(0 if n%2 == 0 else 1))
    while n > 1:
        n = n // 2
        bits.append(str(0 if n%2 == 0 else 1))

    bits.reverse()
    return ''.join(bits)

What I did:

  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 if it is odd append a 1.

Other ways of doing it:

###Using recursion:

def dec_to_bin(x):
    return dec_to_bin(x/2) + [x%2] if x > 1 else [x]

The above solution returns the result as a list which you can later on .join() then apply int() to it.

Another idea that came to my mind is as simple as:

u = format(62, "08b")
>> 00111110

A version that better utilizes the memory would be:

def dec_to_bin(n):
    bits = []

    bits.append(str(0 if n%2 == 0 else 1))
    while n > 1:
        n = n // 2
        bits.append(str(0 if n%2 == 0 else 1))

    bits.reverse()
    return ''.join(bits)

What I did:

  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 if it is odd append a 1.

Other ways of doing it:

###Using recursion:

def dec_to_bin(x):
    return dec_to_bin(x/2) + [x%2] if x > 1 else [x]

The above solution returns the result as a list which you can later on .join() then apply int() to it.

Another idea that came to my mind is as simple as:

u = format(62, "08b")
>> 00111110

The best solution, as far as I read would be an algorithm called Divide by 2 that uses a stack to keep track of the digits for the binary result.

The best solution, as far as I read, would be an algorithm called Divide by 2 that uses a stack to keep track of the digits for the binary result.

A version that better (than your solution) utilizes the memory would be:

def dec_to_bin(n):
    bits = []

    bits.append(str(0 if n%2 == 0 else 1))
    while n > 1:
        n = n // 2
        bits.append(str(0 if n%2 == 0 else 1))

    bits.reverse()
    return ''.join(bits)

What I did:

  • floor divide all the numbers by two repeatedly until we reach 1
  • going in reverse order, create bits of this array of numbers, if it is even, append a 0 if it is odd append a 1.

Other ways of doing it:

###Using recursion:

def dec_to_bin(x):
    return dec_to_bin(x/2) + [x%2] if x > 1 else [x]

The above solution returns the result as a list which you can later on .join() then apply int() to it.

Another idea that came to my mind is as simple as:

u = format(62, "08b")
>> 00111110
deleted 53 characters in body
Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71

###Comments regarding your code:

  1. Why did you do this: dec_new = dec_number ? You could've just use dec_number. There's no need of assigning its value to another variable.
  2. This: dec_new = dec_new // 2 could be as well as above rewritten as: dec_new //= 2
  3. The indentation should contain 4 spaces, not 2.

I'll just timeit these in a couple of minutes to test the performance of each.

I'll just timeit these in a couple of minutes to test the performance of each.

###Comments regarding your code:

  1. Why did you do this: dec_new = dec_number ? You could've just use dec_number. There's no need of assigning its value to another variable.
  2. This: dec_new = dec_new // 2 could be as well as above rewritten as: dec_new //= 2
  3. The indentation should contain 4 spaces, not 2.

I'll just timeit these in a couple of minutes to test the performance of each.

deleted 53 characters in body
Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71
Loading
Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71
Loading