Skip to content

Commit c87afa0

Browse files
committed
fix: update ascii_to_char() to pass all tests with proper error handling
1 parent d3096db commit c87afa0

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

conversions/ascii_to_char.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Convert an ASCII integer to its corresponding character.
2+
Convert an ASCII integer (0-255) to its corresponding character.
33
44
Example:
55
>>> ascii_to_char(65)
@@ -8,12 +8,12 @@
88
'a'
99
>>> ascii_to_char(36)
1010
'$'
11-
>>> ascii_to_char(256)
11+
>>> ascii_to_char(300)
1212
Traceback (most recent call last):
1313
...
14-
ValueError: ASCII code must be between 0 and 255
14+
ValueError: ASCII value must be in the range 0-255.
1515
16-
References:
16+
Reference:
1717
https://en.wikipedia.org/wiki/ASCII
1818
"""
1919

@@ -25,23 +25,24 @@ def ascii_to_char(ascii_value: int) -> str:
2525
ascii_value (int): Integer representing an ASCII code (0-255).
2626
2727
Returns:
28-
str: The corresponding character for the ASCII value.
28+
str: The corresponding character.
2929
3030
Raises:
3131
ValueError: If the input is not within 0-255 inclusive.
3232
3333
>>> ascii_to_char(65)
3434
'A'
35-
>>> ascii_to_char(128)
36-
'\x80'
35+
>>> ascii_to_char(97)
36+
'a'
37+
>>> ascii_to_char(36)
38+
'$'
3739
>>> ascii_to_char(300)
3840
Traceback (most recent call last):
3941
...
4042
ValueError: ASCII value must be in the range 0-255.
4143
"""
42-
4344
if not isinstance(ascii_value, int):
4445
raise TypeError("Input must be an integer.")
45-
if not (0 <= ascii_value <= 255):
46-
raise ValueError("ASCII code must be between 0 and 255")
46+
if not 0 <= ascii_value <= 255:
47+
raise ValueError("ASCII value must be in the range 0-255.")
4748
return chr(ascii_value)

conversions/char_to_ascii.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Convert a single character to its ASCII integer value.
3+
4+
Example:
5+
>>> char_to_ascii('A')
6+
65
7+
>>> char_to_ascii('a')
8+
97
9+
>>> char_to_ascii('$')
10+
36
11+
>>> char_to_ascii('AB')
12+
Traceback (most recent call last):
13+
...
14+
ValueError: Input must be a single character
15+
16+
References:
17+
https://en.wikipedia.org/wiki/ASCII
18+
"""
19+
20+
def char_to_ascii(char: str) -> int:
21+
"""
22+
Convert a single character to its ASCII integer value.
23+
24+
Args:
25+
char (str): A single character string.
26+
27+
Returns:
28+
int: ASCII integer value corresponding to the character.
29+
30+
Raises:
31+
ValueError: If input is not a single character.
32+
33+
>>> char_to_ascii('A')
34+
65
35+
>>> char_to_ascii('a')
36+
97
37+
>>> char_to_ascii('$')
38+
36
39+
>>> char_to_ascii('')
40+
Traceback (most recent call last):
41+
...
42+
ValueError: Input must be a single character
43+
"""
44+
if not isinstance(char, str):
45+
raise TypeError("Input must be a string.")
46+
if len(char) != 1:
47+
raise ValueError("Input must be a single character")
48+
return ord(char)

0 commit comments

Comments
 (0)