forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #71 from MIT-Emerging-Talent/hex_to_binary
hex_to_binary challenge
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
This module provides a utility function to convert a hexadecimal string | ||
to its binary representation. | ||
Functions: | ||
- hex_to_binary: Converts a hexadecimal string to its binary equivalent. | ||
Author: Marc Darazi | ||
""" | ||
|
||
|
||
def hex_to_binary(hex_string): | ||
""" | ||
Convert a hexadecimal string to its binary representation. | ||
Parameters: | ||
hex_string (str): A string representing a hexadecimal number. | ||
It can include a leading '0x' prefix or not. | ||
Returns: | ||
str: A binary string representation of the given hexadecimal number, | ||
with leading zeros preserved. | ||
Raises: | ||
ValueError: If the input string is not a valid hexadecimal number. | ||
Example: | ||
>>> hex_to_binary("1A") | ||
'11010' | ||
>>> hex_to_binary("0xFF") | ||
'11111111' | ||
>>> hex_to_binary("0x0") | ||
'0' | ||
""" | ||
# Remove '0x' prefix if present | ||
if hex_string.startswith("0x"): | ||
hex_string = hex_string[2:] | ||
|
||
try: | ||
# Convert to integer, then to binary | ||
binary_string = bin(int(hex_string, 16))[2:] # Remove '0b' prefix | ||
return binary_string | ||
except ValueError: | ||
raise ValueError("Invalid hexadecimal string") |
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,60 @@ | ||
""" | ||
This module provides a utility function to convert a hexadecimal string | ||
to its binary representation. | ||
Functions: | ||
- hex_to_binary: Converts a hexadecimal string to its binary equivalent. | ||
Author: Marc Darazi | ||
""" | ||
|
||
import unittest | ||
from ..hex_to_binary import hex_to_binary | ||
|
||
|
||
class TestHexToBinary(unittest.TestCase): | ||
""" | ||
Unit tests for the hex_to_binary function. | ||
These tests validate that the hex_to_binary function correctly converts | ||
hexadecimal strings to binary strings, handling both cases with and | ||
without the '0x' prefix and invalid inputs. | ||
""" | ||
|
||
def test_valid_hex_without_prefix(self): | ||
""" | ||
Test converting a valid hexadecimal string without the '0x' prefix. | ||
""" | ||
self.assertEqual(hex_to_binary("1A"), "11010") | ||
|
||
def test_valid_hex_with_prefix(self): | ||
""" | ||
Test converting a valid hexadecimal string with the '0x' prefix. | ||
""" | ||
self.assertEqual(hex_to_binary("0xFF"), "11111111") | ||
|
||
def test_single_digit_hex(self): | ||
""" | ||
Test converting a single digit hexadecimal string. | ||
""" | ||
self.assertEqual(hex_to_binary("9"), "1001") | ||
|
||
def test_hex_with_lowercase_letters(self): | ||
""" | ||
Test converting a hexadecimal string with lowercase letters. | ||
""" | ||
self.assertEqual(hex_to_binary("a"), "1010") | ||
|
||
def test_invalid_hex_with_non_hex_characters(self): | ||
""" | ||
Test an invalid hexadecimal string with non-hex characters. | ||
""" | ||
with self.assertRaises(ValueError): | ||
hex_to_binary("G1") | ||
|
||
def test_empty_string(self): | ||
""" | ||
Test an empty string as input, which should raise a ValueError. | ||
""" | ||
with self.assertRaises(ValueError): | ||
hex_to_binary("") |