mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
added stringfunctions.py
This commit is contained in:
parent
24125e3b7d
commit
d3a9deca41
@ -1,4 +1,4 @@
|
||||
def is_upper(string):
|
||||
def is_upper(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are uppercase letters.
|
||||
|
||||
@ -10,7 +10,7 @@ def is_upper(string):
|
||||
"""
|
||||
return all(ord(char) >= 65 and ord(char) <= 90 for char in string)
|
||||
|
||||
def is_lower(string):
|
||||
def is_lower(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are lowercase letters.
|
||||
|
||||
@ -22,7 +22,7 @@ def is_lower(string):
|
||||
"""
|
||||
return all(ord(char) >= 97 and ord(char) <= 122 for char in string)
|
||||
|
||||
def is_alpha(string):
|
||||
def is_alpha(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are alphabetical (letters).
|
||||
|
||||
@ -34,7 +34,7 @@ def is_alpha(string):
|
||||
"""
|
||||
return all(char.isalpha() for char in string)
|
||||
|
||||
def is_alnum(string):
|
||||
def is_alnum(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are alphanumeric (letters or digits).
|
||||
|
||||
@ -46,7 +46,7 @@ def is_alnum(string):
|
||||
"""
|
||||
return all(char.isalnum() for char in string)
|
||||
|
||||
def is_decimal(string):
|
||||
def is_decimal(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are decimal digits.
|
||||
|
||||
@ -58,7 +58,7 @@ def is_decimal(string):
|
||||
"""
|
||||
return all(char.isdigit() for char in string)
|
||||
|
||||
def is_space(string):
|
||||
def is_space(string: str) -> bool:
|
||||
"""
|
||||
Check if all characters in the string are whitespace characters.
|
||||
|
||||
@ -70,7 +70,7 @@ def is_space(string):
|
||||
"""
|
||||
return all(char.isspace() for char in string)
|
||||
|
||||
def is_title(string):
|
||||
def is_title(string: str) -> bool:
|
||||
"""
|
||||
Check if the string is in title case (first letter of each word is capital).
|
||||
|
||||
@ -80,4 +80,22 @@ def is_title(string):
|
||||
Returns:
|
||||
bool: True if the string is in title case, False otherwise.
|
||||
"""
|
||||
return string.istitle()
|
||||
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!")
|
Loading…
x
Reference in New Issue
Block a user