dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68b7cadffed0e75a517da673 | Challenge 52: Binary to Decimal | 29 | challenge-52 |
--description--
Given a string representing a binary number, return its decimal equivalent as a number.
A binary number uses only the digits 0 and 1 to represent any number. To convert binary to decimal, multiply each digit by a power of 2 and add them together. Start by multiplying the rightmost digit by 2^0, the next digit to the left by 2^1, and so on. Once all digits have been multiplied by a power of 2, add the result together.
For example, the binary number 101 equals 5 in decimal because:
1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5
--hints--
to_decimal("101") should return 5.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_decimal("101"), 5)`)
}})
to_decimal("1010") should return 10.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_decimal("1010"), 10)`)
}})
to_decimal("10010") should return 18.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_decimal("10010"), 18)`)
}})
to_decimal("1010101") should return 85.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_decimal("1010101"), 85)`)
}})
--seed--
--seed-contents--
def to_decimal(binary):
return binary
--solutions--
def to_decimal(binary):
decimal = 0
for i, bit_char in enumerate(reversed(binary)):
bit = int(bit_char)
decimal += bit * (2 ** i)
return decimal