Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / strings_23_similarity.py
Created July 18, 2020 21:02
String similarity
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
@raeq
raeq / strings_4_join.py
Created July 18, 2020 20:45
Concatenate values to a string
def concatenate(*argv)->str:
return ''.join(argv)
assert concatenate("a", "b", "c") == "abc"
@raeq
raeq / strings_20_locations.py
Created July 18, 2020 20:27
Find character indexes in string
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]
@raeq
raeq / strings_18_interleave.py
Created July 18, 2020 20:06
Interleave two strings
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"
@raeq
raeq / strings_22_remove_at_index.py
Last active July 18, 2020 19:45
Remove a character from a string at index
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")
@raeq
raeq / strings_20_substring.py
Created July 18, 2020 19:27
Use the replace function
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'"
@raeq
raeq / strings_21_leetspeak.py
Created July 18, 2020 11:30
Normal text to leetspeak
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"
@raeq
raeq / strings_03_case.py
Created July 18, 2020 11:11
Change the case of a string
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:
@raeq
raeq / strings_02_equality.py
Created July 18, 2020 10:54
Arte strings equal
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
@raeq
raeq / strings_01_reverse.py
Created July 18, 2020 10:38
Reverse a String
def string_reverse(forward: str = "") -> str:
return forward[::-1]
assert string_reverse("hello") == "olleh"
assert string_reverse("goodbye") != "goodbye"