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
def prime_factors(n): | |
for i in range(2, int(n ** 0.5) + 1): | |
if (q_r := divmod(n, i))[1] == 0: | |
return [i] + factor_list(q_r[0]) | |
return [n] |
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 itertools import takewhile | |
from math import sqrt | |
def prime(n: int) -> bool: | |
tests = set(range(2, int(sqrt(n) + 1))) | |
non_factors = set(takewhile(lambda i: n % i != 0, tests)) | |
return tests == non_factors |
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 pprint import pprint as pp | |
def transpose(self, A: list[list[int]]) -> list[list[int]]: | |
# using list comprehension | |
return [[row[c] for row in A] for c in range(len(A[0]))] | |
def transpose(self, A: list[list[int]]) -> list[list[int]]: | |
# using zip | |
return [list(x) for x in zip(*A)] |
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, Tuple, Union | |
def shape(ndarray: Union[List, float]) -> Tuple[int, ...]: | |
if isinstance(ndarray, list): | |
# More dimensions, so make a recursive call | |
outermost_size = len(ndarray) | |
row_shape = shape(ndarray[0]) | |
return (outermost_size, *row_shape) | |
else: |
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, Iterable, Callable | |
def for_each(xs: Iterable[Any], do: Callable[[Iterable[Any]], None]) -> None: | |
it = iter(xs) | |
try: | |
while True: | |
do(next(it)) | |
except StopIteration: | |
pass |
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
// remove top navigation bar | |
document.querySelectorAll(".navbar").forEach(el => el.remove()) | |
// remove status bar (bottom bar) | |
document.querySelectorAll(".drip-tab-container").forEach(el => el.remove()) | |
// remove sign-in red bell | |
document.querySelectorAll(".onesignal-bell-container").forEach(el => el.remove()) | |
// remove column on the right (ads) | |
document.querySelectorAll(".col-md-7, .col-lg-4").forEach(el => el.remove()) | |
// remove chat button on the right-bottom corner | |
//document.getElementById("beacon-container-body").remove() |
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 unittest.mock import patch | |
def greet(): | |
print("Hello World") | |
@patch("builtins.print") | |
def test_greet(mock_print): | |
greet() |
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
def fizz_buzz(n: int) -> str: | |
return ("fizzbuzz", "buzz", "fizz", str(n))[(n % 15, n % 5, n % 3, 0).index(0)] | |
# test | |
for i in range(1, 31): | |
print(i, fizz_buzz(i)) | |
""" | |
output | |
1 1 |
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 time import perf_counter | |
from contextlib import contextmanager | |
@contextmanager | |
def timer() -> float: | |
start = perf_counter() | |
yield lambda: perf_counter() - start | |
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 amazonlinux | |
RUN yum -y update | |
RUN yum -y upgrade | |
RUN yum -y install curl | |
RUN yum -y install unzip | |
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
RUN unzip awscliv2.zip | |
RUN ./aws/install -i /usr/local/aws-cli -b /usr/local/bin |