Skip to main content
added 710 characters in body
Source Link
Tim Roberts
  • 55.2k
  • 4
  • 28
  • 41

I don't like doing people's homework for them, but in this case I think the example says more than an explanation.

You do this conversion one character at a time, from left to right. At each step, you shift the result left by one, and if the digit is '1', you add it in.

number = 1011
decimal = 0
for c in str(number):
    decimal = decimal * 2 + (c=='1')
print(decimal)

If that's too clever, replace (c=='1') with int(c).

FOLLOWUP

This is what I've been trying to tell you. The code you have will actually do the correct conversion, but it only works if you have exactly 7 digits. All you need to do is change the code so you CHECK the number of digits before you try one:

number = 1010111

num = str(number)
result = int(num[-1]) * 1
if len(num) > 1:
    result += int(num[-2]) * 2
if len(num) > 2:
    result += int(num[-3]) * 4
if len(num) > 3:
    result += int(num[-4]) * 8
if len(num) > 4:
    result += int(num[-5]) * 16
if len(num) > 5:
    result += int(num[-6]) * 32
if len(num) > 6:
    result += int(num[-7]) * 64
if len(num) > 7:
    result += int(num[-8]) * 128

print(result)

I don't like doing people's homework for them, but in this case I think the example says more than an explanation.

You do this conversion one character at a time, from left to right. At each step, you shift the result left by one, and if the digit is '1', you add it in.

number = 1011
decimal = 0
for c in str(number):
    decimal = decimal * 2 + (c=='1')
print(decimal)

If that's too clever, replace (c=='1') with int(c).

I don't like doing people's homework for them, but in this case I think the example says more than an explanation.

You do this conversion one character at a time, from left to right. At each step, you shift the result left by one, and if the digit is '1', you add it in.

number = 1011
decimal = 0
for c in str(number):
    decimal = decimal * 2 + (c=='1')
print(decimal)

If that's too clever, replace (c=='1') with int(c).

FOLLOWUP

This is what I've been trying to tell you. The code you have will actually do the correct conversion, but it only works if you have exactly 7 digits. All you need to do is change the code so you CHECK the number of digits before you try one:

number = 1010111

num = str(number)
result = int(num[-1]) * 1
if len(num) > 1:
    result += int(num[-2]) * 2
if len(num) > 2:
    result += int(num[-3]) * 4
if len(num) > 3:
    result += int(num[-4]) * 8
if len(num) > 4:
    result += int(num[-5]) * 16
if len(num) > 5:
    result += int(num[-6]) * 32
if len(num) > 6:
    result += int(num[-7]) * 64
if len(num) > 7:
    result += int(num[-8]) * 128

print(result)
Source Link
Tim Roberts
  • 55.2k
  • 4
  • 28
  • 41

I don't like doing people's homework for them, but in this case I think the example says more than an explanation.

You do this conversion one character at a time, from left to right. At each step, you shift the result left by one, and if the digit is '1', you add it in.

number = 1011
decimal = 0
for c in str(number):
    decimal = decimal * 2 + (c=='1')
print(decimal)

If that's too clever, replace (c=='1') with int(c).