From d3096db67fc57435c46e844e754d3a043ae3beac Mon Sep 17 00:00:00 2001 From: "Nikash M.S." Date: Sun, 19 Oct 2025 12:57:50 +0530 Subject: [PATCH 1/4] Added ascii_to_char function with docstring and tests Contributing for Hacktoberfest --- conversions/ascii_to_char.py | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 conversions/ascii_to_char.py diff --git a/conversions/ascii_to_char.py b/conversions/ascii_to_char.py new file mode 100644 index 000000000000..c9bdbb84529b --- /dev/null +++ b/conversions/ascii_to_char.py @@ -0,0 +1,47 @@ +""" +Convert an ASCII integer to its corresponding character. + +Example: + >>> ascii_to_char(65) + 'A' + >>> ascii_to_char(97) + 'a' + >>> ascii_to_char(36) + '$' + >>> ascii_to_char(256) + Traceback (most recent call last): + ... + ValueError: ASCII code must be between 0 and 255 + +References: + https://en.wikipedia.org/wiki/ASCII +""" + +def ascii_to_char(ascii_value: int) -> str: + """ + Convert an ASCII integer (0-255) into its corresponding character. + + Args: + ascii_value (int): Integer representing an ASCII code (0-255). + + Returns: + str: The corresponding character for the ASCII value. + + Raises: + ValueError: If the input is not within 0-255 inclusive. + + >>> ascii_to_char(65) + 'A' + >>> ascii_to_char(128) + '\x80' + >>> ascii_to_char(300) + Traceback (most recent call last): + ... + ValueError: ASCII value must be in the range 0-255. + """ + + if not isinstance(ascii_value, int): + raise TypeError("Input must be an integer.") + if not (0 <= ascii_value <= 255): + raise ValueError("ASCII code must be between 0 and 255") + return chr(ascii_value) From 79610dd9ff72b70bbb4de1377d30643643e7cbb2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 07:40:01 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/ascii_to_char.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/ascii_to_char.py b/conversions/ascii_to_char.py index c9bdbb84529b..7b6971ec4ab1 100644 --- a/conversions/ascii_to_char.py +++ b/conversions/ascii_to_char.py @@ -17,6 +17,7 @@ https://en.wikipedia.org/wiki/ASCII """ + def ascii_to_char(ascii_value: int) -> str: """ Convert an ASCII integer (0-255) into its corresponding character. From c87afa0bdd6f43bfff4bab6a838267150cf866be Mon Sep 17 00:00:00 2001 From: "Nikash M.S." Date: Sun, 19 Oct 2025 13:38:54 +0530 Subject: [PATCH 3/4] fix: update ascii_to_char() to pass all tests with proper error handling --- conversions/ascii_to_char.py | 21 ++++++++-------- conversions/char_to_ascii.py | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 conversions/char_to_ascii.py diff --git a/conversions/ascii_to_char.py b/conversions/ascii_to_char.py index c9bdbb84529b..492e300ad7ce 100644 --- a/conversions/ascii_to_char.py +++ b/conversions/ascii_to_char.py @@ -1,5 +1,5 @@ """ -Convert an ASCII integer to its corresponding character. +Convert an ASCII integer (0-255) to its corresponding character. Example: >>> ascii_to_char(65) @@ -8,12 +8,12 @@ 'a' >>> ascii_to_char(36) '$' - >>> ascii_to_char(256) + >>> ascii_to_char(300) Traceback (most recent call last): ... - ValueError: ASCII code must be between 0 and 255 + ValueError: ASCII value must be in the range 0-255. -References: +Reference: https://en.wikipedia.org/wiki/ASCII """ @@ -25,23 +25,24 @@ def ascii_to_char(ascii_value: int) -> str: ascii_value (int): Integer representing an ASCII code (0-255). Returns: - str: The corresponding character for the ASCII value. + str: The corresponding character. Raises: ValueError: If the input is not within 0-255 inclusive. >>> ascii_to_char(65) 'A' - >>> ascii_to_char(128) - '\x80' + >>> ascii_to_char(97) + 'a' + >>> ascii_to_char(36) + '$' >>> ascii_to_char(300) Traceback (most recent call last): ... ValueError: ASCII value must be in the range 0-255. """ - if not isinstance(ascii_value, int): raise TypeError("Input must be an integer.") - if not (0 <= ascii_value <= 255): - raise ValueError("ASCII code must be between 0 and 255") + if not 0 <= ascii_value <= 255: + raise ValueError("ASCII value must be in the range 0-255.") return chr(ascii_value) diff --git a/conversions/char_to_ascii.py b/conversions/char_to_ascii.py new file mode 100644 index 000000000000..1a5925896f44 --- /dev/null +++ b/conversions/char_to_ascii.py @@ -0,0 +1,48 @@ +""" +Convert a single character to its ASCII integer value. + +Example: + >>> char_to_ascii('A') + 65 + >>> char_to_ascii('a') + 97 + >>> char_to_ascii('$') + 36 + >>> char_to_ascii('AB') + Traceback (most recent call last): + ... + ValueError: Input must be a single character + +References: + https://en.wikipedia.org/wiki/ASCII +""" + +def char_to_ascii(char: str) -> int: + """ + Convert a single character to its ASCII integer value. + + Args: + char (str): A single character string. + + Returns: + int: ASCII integer value corresponding to the character. + + Raises: + ValueError: If input is not a single character. + + >>> char_to_ascii('A') + 65 + >>> char_to_ascii('a') + 97 + >>> char_to_ascii('$') + 36 + >>> char_to_ascii('') + Traceback (most recent call last): + ... + ValueError: Input must be a single character + """ + if not isinstance(char, str): + raise TypeError("Input must be a string.") + if len(char) != 1: + raise ValueError("Input must be a single character") + return ord(char) From 065f5b64691d51484c78b9ad4aa8649da1567ad7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 08:09:29 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/char_to_ascii.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/char_to_ascii.py b/conversions/char_to_ascii.py index 1a5925896f44..b17542da6300 100644 --- a/conversions/char_to_ascii.py +++ b/conversions/char_to_ascii.py @@ -17,6 +17,7 @@ https://en.wikipedia.org/wiki/ASCII """ + def char_to_ascii(char: str) -> int: """ Convert a single character to its ASCII integer value.