Skip to content

Instantly share code, notes, and snippets.

@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 / 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
"""
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):
"""
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):
fruits_dict: dict = dict()
citrusfruit: set = {"oranges", "lemons", "limes", "satsumas", "nectarines"}
treefruit: set = {"apples", "pears", "cherries", "plums", "peaches", "plums",
"cherries", "oranges", "lemons", "limes"}
stonefruit: set = {"cherries", "plums", "peaches", "nectarines"}
for fruit in citrusfruit.union(stonefruit).union(treefruit):
fruits_dict.setdefault(fruit, [])
"""
A module illsutrating storing a key-value pair in a dictionary.
"""
import collections
import json
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title", "author"])
books_as_list: list = list()
books_as_dict: dict = dict()
"""
A module illsutrating storing a key-value pair in a dictionary.
"""
import collections
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title", "author"])
books_as_list: list = list()
books_as_dict: dict = dict()
books_as_list.append(
"""
A module illsutrating storing a key-value pair in a dictionary.
"""
import collections
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title", "author"])
books_as_list: list = list()
books_as_dict: dict = dict()
books_as_list.append(
@raeq
raeq / dictionary_00.py
Created May 7, 2020 16:00
Dictionary Example
"""
A module illsutrating storing a key-value pair in a dictionary.
"""
import collections
Book: collections.namedtuple = collections.namedtuple("Book", ["isbn", "title"])
books_as_list: list = list()
books_as_dict: dict = dict()
books_as_list.append(