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
| #!/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. | |
| """ |
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
| 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] |
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
| 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. | |
| """ |
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
| #!/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)) |
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
| #!/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)) |
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
| #!/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') |
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
| #!/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. | |
| """ |
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
| #!/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 |
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
| #!/usr/bin/env python3 | |
| from typing import Any, Callable, Generator, Iterable, List | |
| def my_map(func: Callable[[Any, ...], Any], *iterables: List[Iterable[Any]]) -> Generator[Any, None, None]: | |
| """ | |
| Mimics the built-in map function, accepting any number of iterables | |
| and yielding values returned by the given function. | |
| """ | |
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
| #!/usr/bin/env python3 | |
| from typing import Optional, List, Generator | |
| def prime_gen(limit: Optional[int]=None) -> Generator[int, None, None]: | |
| """Generates prime numbers with an optional upper limit""" | |
| primes = [2] | |
| n = 3 | |
| while limit is None or limit >= n: | |
| for prime in primes: |