import pandas as pd
src = [
{'category': 'software', 'name': 'irssi', 'version': 5},
{'category': 'software', 'name': 'irssi', 'version': 4},
{'category': 'software', 'name': 'hexchat', 'version': 2},
{'category': 'software', 'name': 'hexchat', 'version': 3},
{'category': 'software', 'name': 'hexchat1', 'version': 1}
]
dst = (
pd.DataFrame(src)
.sort_values('version')
.drop_duplicates(['category', 'name'])
.to_dict('records')
)
import numpy as np
lst = [[1, 2], [3, 4], [5, 6]]
# Способ 1
list(map(sum, zip(*lst)))
# Способ 2
np.array(lst).sum(axis=0)
import requests
url = 'https://oeis.org/A000040/a000040.txt'
txt = 'Birds are animal with feathers. Because they can fly, they are the fastest animals on Earth'
def is_prime(number, primes={*requests.get(url).text.split()[1::2]}):
return str(number) in primes
for index, char in enumerate(txt, 1):
if is_prime(index):
print(char, end=' ')
import re
def simplify(expr):
match = re.match('^(.*?)(\()(.*)(\))(.*?)$', expr)
if match:
parts = list(match.groups())
parts[2] = simplify(parts[2])
simplified = ''.join(parts[::2])
if eval(simplified) == eval(expr):
return simplified
return ''.join(parts)
return expr
print(simplify('((((((((12+24)-56)*29)/52)+29)*90)/12)/62)'))
((12+24-56)*29/52+29)*90/12/62
import numpy as np
image = np.zeros((50, 100, 3), np.uint8) # чёрное
image = np.ones((50, 100, 3), np.uint8) * 255 # белое
subsets = [
'[~!@#$%^&*()_+{}":;\']+$',
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'абвгдеёжзийклмнопрстуфхцчшщъыьэюя',
'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
]
def password_is_strong(password, min_length=0):
return len(password) >= min_length and all(map(set(password).intersection, subsets))
print(password_is_strong('Питон!=Delphi', 8))
import numpy as np
from numba import njit
# С использованием numpy
def max_divisors_v1(n):
divs = np.ones(n + 1, np.uint8)
for step in range(2, n + 1):
divs[step::step] += 1
divs[0] = 0
num = divs.argmax()
return num, divs[num]
# Без numpy, чистый Python
def max_divisors_v2(n):
divs = [1] * (n + 1)
for step in range(2, n + 1):
for index in range(step, n + 1, step):
divs[index] += 1
divs[0] = 0
cnt = max(divs)
num = divs.index(cnt)
return num, cnt
# JIT-компиляция с декоратором @njit
max_divisors_v3 = njit(max_divisors_v2)
matchobj = re.search(...)
if matchobj:
crypto = matchobj.group()
import cv2
import numpy as np
data = """1 -9 -2 8 6 1
8 -1 -11 -7 6 4
10 12 -1 -9 -12 14
8 10 -3 -5 17 8
6 4 10 -13 -16 19"""
# matrix = np.random.randint(-128, 128, (1000, 1000), dtype=np.int32)
matrix = np.int32([line.split() for line in data.splitlines()])
def find_max_kernel(matrix, border=cv2.BORDER_ISOLATED):
max_area = 0
mask = np.float32(matrix < 0)
ones = np.ones_like(mask)
conv_x = np.zeros_like(mask)
conv_y = np.zeros_like(mask)
max_h, max_w = mask.shape
for h in range(1, max_h + 1):
cv2.filter2D(mask, -1, ones[:h, None, 0], conv_y, (0, 0), 0, border)
for w in range(1, max_w + 1):
area = h * w
if area > max_area:
cv2.filter2D(conv_y, -1, ones[None, 0, :w], conv_x, (0, 0), 0, border)
if conv_x.max() == area:
max_area, shape = area, (h, w)
else:
if w == 1:
max_h = h - 1
if h == 1:
max_w = w - 1
break
if h >= max_h:
break
cv2.filter2D(mask, -1, np.ones(shape, np.float32), conv_x, (0, 0), 0, border)
p1 = np.array(np.unravel_index(conv_x.argmax(), conv_x.shape))
p2 = p1 + shape - 1
return p1, p2
print(*find_max_kernel(matrix), sep='\n')