Skip to content

Instantly share code, notes, and snippets.

View Diapolo10's full-sized avatar
🏠
Working from home

Lari Liuhamo Diapolo10

🏠
Working from home
View GitHub Profile
@Diapolo10
Diapolo10 / text_binary.py
Created February 20, 2021 20:40
Text-to-binary-to-text in Python
#!/usr/bin/env python3
def text_to_bin(text: str) -> str:
"""Takes a string, converts it to a space-separated string of binary numbers"""
result = []
for char in text:
unicode_value = ord(char)
binary = format(unicode_value, '#010b')
@Diapolo10
Diapolo10 / max.py
Created March 18, 2021 15:11
Python max-function example implementation
#!/usr/bin/env python3
from typing import Callable, Iterable, T
def my_max(iterable: Iterable[T], key: Callable[[T, ...], Any]=lambda x: x) -> T:
iterator = iter(iterable)
try:
highest = key(next(iterator))
@Diapolo10
Diapolo10 / min.py
Last active March 18, 2021 15:12
Python min-function example implementation
#!/usr/bin/env python3
from typing import Callable, Iterable, T
def my_min(iterable: Iterable[T], key: Callable[[T, ...], Any]=lambda x: x) -> T:
iterator = iter(iterable)
try:
lowest = key(next(iterator))
@Diapolo10
Diapolo10 / filter.py
Last active March 18, 2021 15:19
Python filter-function implementation
#!/usr/bin/env python3
from typing import Any, Callable, Generator, Iterable, T
def my_filter(func: Callable[[T], Any], iterable: Iterable[T]) -> Generator[T, None, None]:
"""Yields all values from the iterable for which the function returns a truthful value"""
for value in iterable:
if func(value):
yield value
@Diapolo10
Diapolo10 / prime_factors.py
Last active May 19, 2021 16:55
Python prime factor calculator
from typing import List
def prime_factors(num: int) -> List[int]:
"""
Searches for numbers that evenly divide the given
number and makes a list of these numbers, returning
the list. The product of the list is equivalent to
the original number as long as it's > 1, returning
an empty list otherwise.
"""
@Diapolo10
Diapolo10 / my_zip.py
Created December 15, 2021 15:25
Python zip-function example implementation
from typing import Any, Callable, Generator, Iterable, List
def my_zip(*iterables: List[Iterable[Any]]) -> Generator[List[Any], None, None]:
"""
Mimics the built-in zip function, accepting any number of iterables
and yielding values from all of them until one or more are exhausted.
"""
iterators = [iter(iterable) for iterable in iterables]
@Diapolo10
Diapolo10 / n_fib.py
Last active December 15, 2021 23:45
Order N Fibonacci sequence generator
from collections import deque
from typing import Generator
import warnings
def n_fibonacci(n: int = 2) -> Generator[int, None, None]:
"""
Returns a generator that yields the values of a Fibonacci sequence of a given 'base'.
When `n == 0`, the returned sequence simplifies to an infinite sequence of `0`.
@Diapolo10
Diapolo10 / reduce.py
Last active February 9, 2022 09:31
Python functools.reduce-function example implementation
#!/usr/bin/env python3
from typing import Callable, Iterable, T
def my_reduce(func: Callable[[T, T], T], iterable: Iterable[T]) -> T:
"""
Iterates over the given iterable and feeds the values
through the given function until the iterable is
exhausted, returning the final function value.
"""
@Diapolo10
Diapolo10 / example.py
Last active February 11, 2022 19:00
Python style guide example
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# The above two lines specify two things. The order must always be the same if both are used.
# The first line is a shebang, used by Unix-like systems to determine how to run a file.
# The second is a way to specify the encoding used by the file.
# Neither are necessary, especially the second one, since Python 3 is UTF-8 by default.
"""
@Diapolo10
Diapolo10 / my_enumerate.py
Last active March 2, 2022 10:50
Python enumerate-function example implementation
from typing import Generator, Iterable, T, Tuple
def my_enumerate(iterable: Iterable[T], start: int=0) -> Generator[Tuple[int, T], None, None]:
"""
Mimics the built-in enumerate-function, accepting any iterable,
and yielding incremented indices and values from it.
"""
idx = start
for value in iterable: