mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
added doctests to stringfunctions.py
This commit is contained in:
parent
c88a5a4f15
commit
02ff53c1e8
@ -7,10 +7,15 @@ def is_upper(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are uppercase letters, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_upper("HELLO")
|
||||
True
|
||||
>>> is_upper("Hello")
|
||||
False
|
||||
"""
|
||||
return all(ord(char) >= 65 and ord(char) <= 90 for char in string)
|
||||
|
||||
|
||||
def is_lower(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are lowercase letters.
|
||||
@ -20,10 +25,15 @@ def is_lower(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are lowercase letters, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_lower("hello")
|
||||
True
|
||||
>>> is_lower("Hello")
|
||||
False
|
||||
"""
|
||||
return all(ord(char) >= 97 and ord(char) <= 122 for char in string)
|
||||
|
||||
|
||||
def is_alpha(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are alphabetical (letters).
|
||||
@ -33,10 +43,15 @@ def is_alpha(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are letters, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_alpha("Hello")
|
||||
True
|
||||
>>> is_alpha("Hello123")
|
||||
False
|
||||
"""
|
||||
return all(char.isalpha() for char in string)
|
||||
|
||||
|
||||
def is_alnum(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are alphanumeric (letters or digits).
|
||||
@ -46,10 +61,15 @@ def is_alnum(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are letters or digits, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_alnum("Hello123")
|
||||
True
|
||||
>>> is_alnum("Hello 123")
|
||||
False
|
||||
"""
|
||||
return all(char.isalnum() for char in string)
|
||||
|
||||
|
||||
def is_decimal(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are decimal digits.
|
||||
@ -59,10 +79,15 @@ def is_decimal(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are decimal digits, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_decimal("12345")
|
||||
True
|
||||
>>> is_decimal("12.345")
|
||||
False
|
||||
"""
|
||||
return all(char.isdigit() for char in string)
|
||||
|
||||
|
||||
def is_space(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are whitespace characters.
|
||||
@ -72,10 +97,15 @@ def is_space(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if all characters in the string are whitespace characters, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_space(" \\t\\n")
|
||||
True
|
||||
>>> is_space("Hello")
|
||||
False
|
||||
"""
|
||||
return all(char.isspace() for char in string)
|
||||
|
||||
|
||||
def is_title(string: str) -> bool:
|
||||
"""
|
||||
Check if the string is in title case (first letter of each word is capital).
|
||||
@ -85,24 +115,15 @@ def is_title(string: str) -> bool:
|
||||
|
||||
Returns:
|
||||
bool: True if the string is in title case, False otherwise.
|
||||
|
||||
Examples:
|
||||
>>> is_title("This Is A Title Case String")
|
||||
True
|
||||
>>> is_title("This is Not a Title Case String")
|
||||
False
|
||||
"""
|
||||
return string.istitle()
|
||||
|
||||
|
||||
# Tests
|
||||
assert is_upper("HELLO")
|
||||
assert not is_upper("Hello")
|
||||
assert is_lower("hello")
|
||||
assert not is_lower("Hello")
|
||||
assert is_alpha("Hello")
|
||||
assert not is_alpha("Hello123")
|
||||
assert is_alnum("Hello123")
|
||||
assert not is_alnum("Hello 123")
|
||||
assert is_decimal("12345")
|
||||
assert not is_decimal("12.345")
|
||||
assert is_space(" \t\n")
|
||||
assert not is_space("Hello")
|
||||
assert is_title("This Is A Title Case String")
|
||||
assert not is_title("This is Not a Title Case String")
|
||||
|
||||
print("All tests passed!")
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
doctest.testmod()
|
Loading…
x
Reference in New Issue
Block a user