forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from MIT-Emerging-Talent/15-challenge-convert-…
…binary-numbers-to-decimal Solution for challenge convert binary to a decimal number
- Loading branch information
Showing
8 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Challenge: Convert Binary Numbers to Decimal | ||
|
||
## Description | ||
|
||
The "Binary to Decimal" challenge involves converting a binary number (base 2) | ||
into its decimal (base 10) equivalent. | ||
|
||
Binary numbers consist of only two digits: 0 and 1, | ||
where each digit represents a power of 2. | ||
|
||
Write a function that takes a binary number as a string | ||
and returns its decimal equivalent as an integer. | ||
|
||
For example: | ||
|
||
- The binary number `1010` equals `10` in decimal. | ||
- The binary number `1111` equals `15` in decimal. | ||
|
||
## Example | ||
|
||
```python | ||
binary_to_decimal('1010') | ||
# Output: 10 (binary 1010 = decimal 10) | ||
|
||
binary_to_decimal('1111') | ||
# Output: 15 (binary 1111 = decimal 15) | ||
|
||
binary_to_decimal('1001') | ||
# Output: 9 (binary 1001 = decimal 9) | ||
``` | ||
|
||
[Github issue](https://github.com/MIT-Emerging-Talent/ET6-foundations-group-04/issues/15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for converting binary numbers to decimal equivalents. | ||
Module contents: | ||
- binary_to_decimal: Converts a binary string to its decimal equivalent | ||
Created on 28/12/2024 | ||
Author: Ramon Colmenares | ||
""" | ||
|
||
|
||
def binary_to_decimal(binary_str: str) -> int: | ||
"""Converts a binary string to its decimal equivalent. | ||
Parameters: | ||
binary_str: str, a binary number as a string | ||
Returns -> int: the decimal equivalent of the binary number | ||
Raises: | ||
ValueError: if the input is not a valid binary string | ||
Examples: | ||
>>> binary_to_decimal('1010') | ||
10 | ||
>>> binary_to_decimal('1111') | ||
15 | ||
>>> binary_to_decimal('1001') | ||
9 | ||
""" | ||
decimal_value = 0 | ||
for i, digit in enumerate(reversed(binary_str)): | ||
if digit not in {"0", "1"}: | ||
raise ValueError(f"Invalid binary digit '{digit}' in input") | ||
decimal_value += int(digit) * (2**i) | ||
return decimal_value |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for binary_to_decimal function. | ||
Test categories: | ||
- Standard cases: typical binary strings | ||
- Edge cases: single-bit binary strings, leading zeros | ||
- Defensive tests: invalid inputs, non-binary strings | ||
Created on 2024-12-06 | ||
Author: Your Name | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.challenge_15.binary_to_decimal import binary_to_decimal | ||
|
||
|
||
class TestBinaryToDecimal(unittest.TestCase): | ||
"""Test suite for the binary_to_decimal function.""" | ||
|
||
def test_simple_binary(self): | ||
""" | ||
It should return the correct decimal | ||
value for typical binary strings | ||
""" | ||
self.assertEqual(binary_to_decimal("1010"), 10) | ||
self.assertEqual(binary_to_decimal("1111"), 15) | ||
self.assertEqual(binary_to_decimal("1001"), 9) | ||
|
||
def test_single_bit_binary(self): | ||
""" | ||
It should return the correct decimal | ||
value for single-bit binary strings | ||
""" | ||
self.assertEqual(binary_to_decimal("0"), 0) | ||
self.assertEqual(binary_to_decimal("1"), 1) | ||
|
||
def test_leading_zeros(self): | ||
"""It should correctly handle binary strings with leading zeros""" | ||
self.assertEqual(binary_to_decimal("0000"), 0) | ||
self.assertEqual(binary_to_decimal("0001"), 1) | ||
self.assertEqual(binary_to_decimal("001010"), 10) | ||
|
||
def test_invalid_binary_string(self): | ||
"""It should raise a ValueError for invalid binary strings""" | ||
with self.assertRaises(ValueError): | ||
binary_to_decimal("2") | ||
with self.assertRaises(ValueError): | ||
binary_to_decimal("abc") | ||
with self.assertRaises(ValueError): | ||
binary_to_decimal("10.01") |