Skip to content

Instantly share code, notes, and snippets.

"""
Zipping two lists to a dictionary as an example of dict comprehension.
"""
import typing
def dict_from_list(keys: [typing.Hashable], values: [])-> typing.Dict:
"""
Zips two lists. Returns: dict
"""
if len(keys) != len(values):
"""
A simple class example.
"""
class Book:
"""
A simple book class.
"""
def __init__(self, isbn: str, title: str, author: str):
"""
A simple class example.
"""
class Book:
"""
A simple book class.
"""
def __init__(self, isbn: str, title: str, author: str):
@raeq
raeq / class_03.py
Last active May 16, 2020 19:30
Versioned Class
"""
A versioned class example.
"""
__version__: str = "0.1"
class Book:
"""
A sinple book class.
"""
counter: int = 0
@raeq
raeq / strings_00_substring.py
Created July 18, 2020 10:33
Find a needle in a haystack
def sub_00(haystack: str="", needle:str="") -> bool:
return needle in haystack
assert sub_00("the quick brown fox jumped over the lazy dog", "lazy") == True
assert sub_00("the quick brown fox jumped over the lazy dog", "lazys") == False
@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"
@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_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_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_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'"