Skip to content

Commit

Permalink
Merge pull request #19 from MIT-Emerging-Talent/15-challenge-convert-…
Browse files Browse the repository at this point in the history
…binary-numbers-to-decimal

Solution for challenge convert binary to a decimal number
  • Loading branch information
likechrisss authored Dec 29, 2024
2 parents 880841e + 291c9fb commit 48bcc78
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": true,
"source.organizeImports.ruff": true
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
}
}
11 changes: 6 additions & 5 deletions collaboration/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ ______________________________________________________________________

## Communication Schedule

| Day | How | The topic of discussion | | --- | :-: | ----------------------- |
| | | |
| Day | How | The topic of discussion |
|------|:----:|-------------------------|
| | | |

## Communication Channels

Expand All @@ -31,9 +32,9 @@ ______________________________________________________________________

### Availability for calling/messaging

| Day | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | |
------ | :----: | :-----: | :-------: | :------: | :----: | :------: | :----: |
| _name_ | | | | | | | |
| Day | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday|
|----------|:------:|:-------:|:---------:|:--------:|:------:|:--------:|:----:|
|Ramon| | | | | | | |

### How many hours everyone has per day

Expand Down
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/binary_to_decimal.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.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")

0 comments on commit 48bcc78

Please sign in to comment.