Skip to content

Instantly share code, notes, and snippets.

def day_of_week(date):
""" get weekday from date (as instance 2022-12-31 iso format) """
#
year = int(date[0:4])
month = int(date[5:7])
day = int(date[8:10])
offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
week = ['Sunday',
'Monday',
'Tuesday',
@blzzua
blzzua / _codewars python tip and tricks
Last active March 1, 2025 09:51
codewars python tip and tricks
We couldn’t find that file to show.
@blzzua
blzzua / estimate_timedelta.py
Created January 8, 2022 15:13
estimate_timedelta.py
def estimate_timedelta(td):
"""returns estimate timedelta in format 'YY year MM mon DD day'. consider plural form or absence y/m/d"""
if td.days > 0:
y, rem = divmod(td.days, 365)
m, d = divmod(rem, 31)
dict_ = {'year': y, 'mon': m, 'day':d }
return " ".join([f"{dict_[k]} {k}" + ("s" if dict_[k] > 1 else "") for k in dict_ if dict_[k] > 0 ])
else:
return str(td)
@blzzua
blzzua / verbal_arithmetic.py
Created August 24, 2021 08:41
verbal arithmetic
from itertools import permutations
from functools import reduce
import re
import operator
_DEBUG = False
_DEBUG_PRINT_PER_TRIES = 1000
def infix_postfix(command):
@blzzua
blzzua / londonwinecompetition.py
Created June 21, 2021 17:43
londonwinecompetition.py
import pandas as pd
import requests
from bs4 import BeautifulSoup
def get_html(url):
r = requests.get(url=url)
return r.text
@blzzua
blzzua / range extraction.py
Last active October 7, 2022 20:12
range extraction
from itertools import groupby
def solution(args):
# [-3,-2,-1,2,10,15,16,18,19,20] -> [[-3, -2, -1], [2], [10], [15, 16], [18, 19, 20]]
grps = ([v[1] for v in g] for _,g in groupby(enumerate(args), lambda p: p[1]-p[0]))
#[[-3, -2, -1], [2], [10], [15, 16], [18, 19, 20]] -> -3--1,2,10,15,16,18-20
return ','.join('{}{}{}'.format(g[0],'-'if len(g)>2 else',',g[-1])
if len(g)>1 else str(g[0]) for g in grps)
# to run this module from command line typing
#
# > python robot_navigation.py robot_data.json
#
# it reads the starting and the finishing points as well as
# the list of obstacles from robot_data.json file and then
# runs find_path() function that has to return the path
#
@blzzua
blzzua / solution.py
Last active September 6, 2020 17:16
O(n*ln(n)) solution for smaller()
# https://www.codewars.com/kata/56a1c074f87bc2201200002e/
# modified merge sort counting left > right cases
def countsort(arr, cntarr, cntdict):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
leftcount = cntarr[:mid]
right = arr[mid:]
rightcount = cntarr[mid:]
@blzzua
blzzua / PredictableNetworkInterfaceNames.txt
Last active August 17, 2020 11:24
Consistent Network Device Naming
regexp: https://regex101.com/r/iqfDq3/5
((eth|v)\d+|(en|sl|wl|ww|ib)(o|s\d+(f\d+)?d(\d+)?|(P\d+)?((p\d+)(s\d+)|(?<!s\d)(p\d+)|(s\d+)(?!p\d))((f\d+)?((u\d+){1,4})?(i\d)?|(d\d+)?)|x[0-9a-f]{12}))
/*
* Predictable network interface device names based on:
* - firmware/bios-provided index numbers for on-board devices
* - firmware-provided pci-express hotplug slot index number
* - physical/geographical location of the hardware
* - the interface's MAC address
from scipy import stats
import math
def confidence_interval(sd, mean, n, target_interval):
"""sd - выборочное среднее значение
mean - среднее выборки
n - размер выборки
target interval - 0..1, например 0.95, 0.99
"""
alpha = 1 - target_interval
z = abs(stats.norm.ppf(alpha/2))