From 882e0da2786085858c2eb292e264b89ee49766b9 Mon Sep 17 00:00:00 2001 From: RamonColmenares Date: Sat, 28 Dec 2024 18:46:47 -0500 Subject: [PATCH 1/2] Feat: add solution for 15 challenge, convert binary to a decimal number add tests aswell --- solutions/__init__.py | 1 - solutions/challenge_15/README.MD | 32 +++++++++++ solutions/challenge_15/__init__.py | 1 + solutions/challenge_15/solution.py | 38 +++++++++++++ solutions/tests/challenge_15/__init__.py | 0 .../challenge_15/test_binary_to_decimal.py | 53 +++++++++++++++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 solutions/challenge_15/README.MD create mode 100644 solutions/challenge_15/__init__.py create mode 100644 solutions/challenge_15/solution.py create mode 100644 solutions/tests/challenge_15/__init__.py create mode 100644 solutions/tests/challenge_15/test_binary_to_decimal.py diff --git a/solutions/__init__.py b/solutions/__init__.py index 8b1378917..e69de29bb 100644 --- a/solutions/__init__.py +++ b/solutions/__init__.py @@ -1 +0,0 @@ - diff --git a/solutions/challenge_15/README.MD b/solutions/challenge_15/README.MD new file mode 100644 index 000000000..048ecbb92 --- /dev/null +++ b/solutions/challenge_15/README.MD @@ -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) diff --git a/solutions/challenge_15/__init__.py b/solutions/challenge_15/__init__.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/solutions/challenge_15/__init__.py @@ -0,0 +1 @@ + diff --git a/solutions/challenge_15/solution.py b/solutions/challenge_15/solution.py new file mode 100644 index 000000000..47224a2c3 --- /dev/null +++ b/solutions/challenge_15/solution.py @@ -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 diff --git a/solutions/tests/challenge_15/__init__.py b/solutions/tests/challenge_15/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/challenge_15/test_binary_to_decimal.py b/solutions/tests/challenge_15/test_binary_to_decimal.py new file mode 100644 index 000000000..c1bf5a382 --- /dev/null +++ b/solutions/tests/challenge_15/test_binary_to_decimal.py @@ -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") From 291c9fbf4f41f1e9ba19dee13cfcce48e4d191ec Mon Sep 17 00:00:00 2001 From: RamonColmenares Date: Sat, 28 Dec 2024 22:32:57 -0500 Subject: [PATCH 2/2] change solution file name to binary_to_decimal --- .vscode/settings.json | 4 ++-- collaboration/communication.md | 11 ++++++----- .../{solution.py => binary_to_decimal.py} | 0 .../tests/challenge_15/test_binary_to_decimal.py | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) rename solutions/challenge_15/{solution.py => binary_to_decimal.py} (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..252022b48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } } } diff --git a/collaboration/communication.md b/collaboration/communication.md index 484652e0f..a808b4642 100644 --- a/collaboration/communication.md +++ b/collaboration/communication.md @@ -13,8 +13,9 @@ ______________________________________________________________________ ## Communication Schedule -| Day | How | The topic of discussion | | --- | :-: | ----------------------- | -| | | | +| Day | How | The topic of discussion | +|------|:----:|-------------------------| +| | | | ## Communication Channels @@ -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 diff --git a/solutions/challenge_15/solution.py b/solutions/challenge_15/binary_to_decimal.py similarity index 100% rename from solutions/challenge_15/solution.py rename to solutions/challenge_15/binary_to_decimal.py diff --git a/solutions/tests/challenge_15/test_binary_to_decimal.py b/solutions/tests/challenge_15/test_binary_to_decimal.py index c1bf5a382..996b644d0 100644 --- a/solutions/tests/challenge_15/test_binary_to_decimal.py +++ b/solutions/tests/challenge_15/test_binary_to_decimal.py @@ -14,7 +14,7 @@ import unittest -from solutions.challenge_15.solution import binary_to_decimal +from solutions.challenge_15.binary_to_decimal import binary_to_decimal class TestBinaryToDecimal(unittest.TestCase):