This file contains hidden or 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
import difflib | |
def similarity(left: str = "", right: str = "") -> float: | |
seq = difflib.SequenceMatcher(None, left, right) | |
return seq.ratio() | |
assert similarity("I like bananas", "I like bananarama") >= 0.80 |
This file contains hidden or 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
def concatenate(*argv)->str: | |
return ''.join(argv) | |
assert concatenate("a", "b", "c") == "abc" |
This file contains hidden or 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
def find_char_locations(original: str = "", character: str = "") -> list: | |
return [index for index, char in enumerate(original) if char == character] | |
assert find_char_locations("The jolly green giant.", "e") == [2, 12, 13] |
This file contains hidden or 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
import itertools | |
def interleave(left: str = "", right: str = "") -> str: | |
return "".join( | |
[i + j for i, j in itertools.zip_longest(left, right, fillvalue="")]) | |
assert interleave("ABCD", "01") == "A0B1CD" |
This file contains hidden or 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
def remove_by_index(original: str = "", index: int = -1) -> tuple: | |
if len(original) >= index and index >= 0: | |
return (original[:index] + original[index + 1:], original[index]) | |
else: | |
return (original, "") | |
assert remove_by_index("0123456789", 5) == ("012346789", "5") |
This file contains hidden or 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
def remove_unwanted(original: str = "", | |
unwanted: str = "", | |
replacement: str = "") -> str: | |
return original.replace(unwanted, replacement) | |
assert remove_unwanted(original="M'The Real String'", | |
unwanted="M") == "'The Real String'" |
This file contains hidden or 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
def to_leetspeak(normal_speak:str="") -> str: | |
leet_mapping = str.maketrans("iseoau", "1530^Ü") | |
return normal_speak.translate(leet_mapping).title().swapcase() | |
assert to_leetspeak("the quick brown fox jumped over the lazy dogs") == \ | |
"tH3 qÜ1cK bR0wN f0x jÜMP3d 0v3r tH3 l^zY d0g5" |
This file contains hidden or 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
def to_uppercase(input_string:str) -> str: | |
return input_string.upper() | |
def to_lowercase(input_string:str) -> str: | |
return input_string.lower() | |
def to_sentencecase(input_string:str) -> str: | |
return input_string.capitalize() | |
def to_titlecase(input_string:str) -> str: |
This file contains hidden or 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
def are_equal(first_comparator: str = "", second_comparator: str = "") -> bool: | |
return first_comparator == second_comparator | |
assert are_equal("thing one", "thing two") is False | |
assert are_equal("a thing", "a " + "thing") is True |
This file contains hidden or 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
def string_reverse(forward: str = "") -> str: | |
return forward[::-1] | |
assert string_reverse("hello") == "olleh" | |
assert string_reverse("goodbye") != "goodbye" |