Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / prime_factors.py
Created February 7, 2021 18:53
Prime factors of the number using recursion and walrus operator
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]
@vlad-bezden
vlad-bezden / prime_nums.py
Created February 6, 2021 14:00
An example of how to find prime numbers using Python itertools.takewhile function
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
@vlad-bezden
vlad-bezden / transform.py
Last active December 6, 2020 19:31
Transform Matrix 90 degrees
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)]
@vlad-bezden
vlad-bezden / list_shape.py
Created August 29, 2020 17:56
Calculate shape of the list of any dimension using recursion
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:
@vlad-bezden
vlad-bezden / foreach.py
Last active August 26, 2020 12:15
For Each example in Python
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
@vlad-bezden
vlad-bezden / clearRealPython.js
Last active September 12, 2021 11:34
Remove navigation bar and status bar snippet from RealPython web page for better readability
// 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()
@vlad-bezden
vlad-bezden / mocking_print.py
Created August 18, 2020 14:52
Mocking Python print() in Unit Tests
from unittest.mock import patch
def greet():
print("Hello World")
@patch("builtins.print")
def test_greet(mock_print):
greet()
@vlad-bezden
vlad-bezden / fizz_buzz.py
Created August 18, 2020 14:04
One liner solution for Fizz Buzz problem
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
@vlad-bezden
vlad-bezden / timer.py
Created July 17, 2020 15:04
Simple timer example using contextmanager. The key here is to yield lambda. Return just value will not work
from time import perf_counter
from contextlib import contextmanager
@contextmanager
def timer() -> float:
start = perf_counter()
yield lambda: perf_counter() - start
@vlad-bezden
vlad-bezden / Dockerfile
Created June 17, 2020 18:34
An example on how to install amazon aws-cli 2.x and nodejs from amazonlinux Linux image to the docker image
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