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
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 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
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
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 from_csv_line(line: str = "") -> list: | |
return line.split(",") | |
assert from_csv_line("a,b,c") == ["a", "b", "c"] |
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 from_list(line: list = []) -> str: | |
ret = ", ".join(e for e in line) | |
return ret | |
assert from_list(["a", "b", "c"]) == "a, b, c" | |
assert from_list(["one", "two", "three"]) == "one, two, three" |
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 file_to_list(filename: str = "") -> list: | |
with open(filename, "r") as f: | |
lines = list(f) | |
return lines | |
assert len(file_to_list("<PATH TO FILE>")) == LINE_COUNT |
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 is_null_or_blank(input_string: str = "") -> bool: | |
if input_string: | |
if input_string.strip(): | |
return False | |
return True | |
assert is_null_or_blank(None) == True | |
assert is_null_or_blank("") == 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 strip_it(input_string: str) -> tuple: | |
return (input_string.lstrip(), input_string.rstrip(), input_string.strip()) | |
left, right, full = strip_it(" A padded string ") | |
assert left == "A padded string " | |
assert right == " A padded string" | |
assert full == "A padded string" |