Skip to content

Commit

Permalink
Feat: add solution for 15 challenge, convert binary to a decimal number
Browse files Browse the repository at this point in the history
add tests aswell
  • Loading branch information
RamonColmenares committed Dec 28, 2024
1 parent 880841e commit 882e0da
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
1 change: 0 additions & 1 deletion solutions/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

32 changes: 32 additions & 0 deletions solutions/challenge_15/README.MD
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)
1 change: 1 addition & 0 deletions solutions/challenge_15/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

38 changes: 38 additions & 0 deletions solutions/challenge_15/solution.py
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.
53 changes: 53 additions & 0 deletions solutions/tests/challenge_15/test_binary_to_decimal.py
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.solution 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")

0 comments on commit 882e0da

Please sign in to comment.