Skip to content

Instantly share code, notes, and snippets.

@sabal202
sabal202 / integrate_func.py
Last active April 2, 2024 15:11
Функция получения интеграла
import math
def integrate(f, a, b, *, n_jobs=1, n_iter=10000000):
acc = 0
step = (b - a) / n_iter
for i in range(n_iter):
acc += f(a + i * step) * step
return acc
@sabal202
sabal202 / 01.1-cpu-sync.py
Created February 17, 2022 17:56
Examples from first acync lecture
from cpu_foobar import superfoo, ultrabar
import time
if __name__ == '__main__':
start = time.time()
foo = superfoo(100)
bar = ultrabar(100)
print(f"Time: {time.time() - start:.4f}s")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sabal202
sabal202 / lstm.py
Created December 17, 2020 14:02
LSTM class for train and validate multi-step multivariate models
import time
import os
import datetime
from collections import namedtuple
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.preprocessing
import torch
@sabal202
sabal202 / make_ipynb_from_py.py
Last active November 18, 2019 18:40
Script converts .py file with delimiters '# %%' and '# %% md' to .ipynb file
"""Converts .py file with delimiters '# %%' and '# %% md' to .ipynb file
Run as: python make_ipynb_from_py.py my_script.py my_notebook.ipynb
"""
import sys
import nbformat
from nbformat.v4 import new_notebook, new_code_cell, new_markdown_cell
def create_cell(t, code_lines):
listing = '\n'.join(code_lines).rstrip().lstrip()
@sabal202
sabal202 / Trie.py
Created October 28, 2019 16:25
Trie on Python
import sys
class Trie:
class Node:
def __init__(self, symbol, is_end=False):
self.leafs = {}
self.symbol = symbol
self.word = ''
self.is_end = is_end
@sabal202
sabal202 / cartesian_tree.py
Created October 21, 2019 18:18
Cartesian Tree (Treap) on Python
# Version 0.1 working, but not so pretty as I want
from random import uniform
def size(T): return 0 if T is None else T.size
class Treap():
def __init__(self, k):
@sabal202
sabal202 / deco_with_optional_arg.py
Last active October 10, 2019 10:39
Как сделать, чтобы можно было вызывать декоратор со скобками и без
import types
import sys
import functools
DEBUG = True
def trace(func=None, *, stream=sys.stdout):
if not DEBUG:
return func
@sabal202
sabal202 / scopes.py
Last active September 23, 2019 13:09
function local scopes test
# Если в теле функции есть присваивание (smth = ..., smth += ...) или определение функции (def smth...) или класса (class smth...) - такое имя считается локальным
# Если есть объявление global smth - имя глобальное, ищется на самом верхнем уровне (текущего модуля/скрипта)
# Если есть объявление nonlocal smth - имя заимствовано из охватывающей функции (и только из функции, и только на 1 уровень выше)
# Иначе имя ищется по правилу legb - local -> enclosing -> global -> built-in.
# G, F, B - по имени функции или global
# A - конфликтующие имена (ambiguous)
G, A1, A2, A3, A4, A5, A6 = ['g··']*7
def foo():
@sabal202
sabal202 / minion_game.py
Created September 18, 2019 19:44
Kevin and Stuart want to play the 'The Minion Game'.
def minion_game(string):
s = string
n = len(s)
stuart = kevin = 0
for i in range(n):
if s[i] in 'AEIOU':
kevin += n - i
else:
stuart += n - i
if kevin == stuart: