forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 43-distinct-subsequences
- Loading branch information
Showing
2 changed files
with
139 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,57 @@ | ||
#!/usr/bin/env python3 | ||
# --#coding: utf-8 -- | ||
""" | ||
Description: This file contains the ASCII code conversion functionality. | ||
Write a function that takes a string as input and returns a list of ASCII | ||
codes of the characters in the input string. | ||
date : 2024-12-31 | ||
@Author : Zeinab Mohmmed | ||
""" | ||
|
||
|
||
def ascii_code(text: str) -> list: | ||
""" | ||
Convert a string to a list of ASCII codes. | ||
parameters: | ||
text: a string of characters | ||
Returns: | ||
a list of integers, where each integer is the ASCII code of the character | ||
at the corresponding index in the input string | ||
Raises: | ||
TypeError: if the input is not a string | ||
e.g. ascii_code(123) | ||
Example: | ||
>>> ascii_code('abc') | ||
[97, 98, 99] | ||
>>> ascii_code('!') | ||
[33] | ||
>>> ascii_code('') | ||
[] | ||
>>> ascii_code(' ') | ||
[32] | ||
>>> ascii_code('0') | ||
[48] | ||
""" | ||
# Check if the input is not a string | ||
if not isinstance(text, str): | ||
raise TypeError("Input must be a string") | ||
# Check if the input is an empty string | ||
if text == "": | ||
return [] | ||
# Convert the string to a list of ASCII codes | ||
ascii_values = [] | ||
# Iterate over the characters in the input string | ||
for i in text: | ||
ascii_values.append(ord(i)) | ||
return ascii_values |
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,82 @@ | ||
#!/usr/bin/env python3 | ||
# --#coding: utf-8 -- | ||
|
||
""" | ||
DESCRIPTION: Tests for the ASCII code conversion functionality. | ||
This module contains a test suite for the ASCII code conversion functionality. | ||
Tests cover standard ASCII characters, whitespace characters, special characters, | ||
numeric characters, and word inputs. It also includes tests for handling invalid | ||
inputs such as empty strings, numbers, lists of numbers, and boolean values. | ||
Date: 2024-12-31 | ||
Author: Zeinab Mohammed | ||
""" | ||
|
||
# Import the unittest module | ||
import unittest | ||
|
||
# Import the function to be tested | ||
from solutions.ascii_code import ascii_code | ||
|
||
|
||
# Define a test suite for the ASCII code conversion functionality: | ||
class TestAsciiCode(unittest.TestCase): | ||
"""Test suite for ASCII code conversion functionality.""" | ||
|
||
def test_basic_capital_chars(self): | ||
"""Test conversion of capital characters.""" | ||
self.assertEqual(ascii_code("C"), [67]) | ||
|
||
def test_small_chars(self): | ||
"""Test conversion of small characters.""" | ||
self.assertEqual(ascii_code("c"), [99]) | ||
|
||
def test_edge_cases(self): | ||
"""Test conversion of edge cases.""" | ||
self.assertEqual(ascii_code("A"), [65]) | ||
|
||
def test_special_chars(self): | ||
"""Test conversion of special characters.""" | ||
self.assertEqual(ascii_code("@"), [64]) | ||
|
||
def test_numbers(self): | ||
"""Test conversion of numeric characters.""" | ||
self.assertEqual(ascii_code("0"), [48]) | ||
|
||
def test_word_input(self): | ||
"""Test handling of word inputs.""" | ||
self.assertEqual(ascii_code("Hello"), [72, 101, 108, 108, 111]) | ||
|
||
def test_whitespace(self): | ||
"""Test conversion of whitespace characters.""" | ||
self.assertEqual(ascii_code(" "), [32]) # space | ||
|
||
def test_empty_input(self): | ||
"""Test handling of empty string input.""" | ||
self.assertEqual(ascii_code(""), []) | ||
|
||
def test_None_input(self): | ||
"""Test handling of None input.""" | ||
with self.assertRaises(TypeError): | ||
ascii_code(None) | ||
|
||
def test_number_input(self): | ||
"""Test handling of number input.""" | ||
with self.assertRaises(TypeError): | ||
ascii_code(123) | ||
|
||
def test_boolean_input(self): | ||
"""Test handling of boolean input.""" | ||
with self.assertRaises(TypeError): | ||
ascii_code(True) | ||
|
||
def test_invalid_input(self): | ||
"""Test handling of float input.""" | ||
with self.assertRaises(TypeError): | ||
ascii_code(12.3) | ||
|
||
|
||
# Run the test suite | ||
if __name__ == "__main__": | ||
unittest.main() |