r"C:\Users\JohnDoe\Desktop\tt.txt"
"C:\\Users\\JohnDoe\\Desktop\\tt.txt"
from collections import Counter
with open('input-comb.txt', 'r') as file:
words = file.read().split()
for item in Counter(words).items():
print(*item)
import re
def decode(encoded):
def repl(m): return m.group(2) * int(m.group(1))
reg = re.compile('\((\d+)(\w+)\)')
while True:
decoded = reg.sub(repl, encoded)
if decoded == encoded:
return decoded
encoded = decoded
assert decode('(2(3ac)a)') == 'acacacaacacaca'
assert decode('(3(2aba))') == 'abaabaabaabaabaaba'
assert decode('(3a)') == 'aaa' # уменьшение длины
$ sudo free -h
from collections import deque
def encrypt(word, k):
code = deque(word)
code.rotate(-k)
code.reverse()
return ''.join(code)
assert encrypt('ШКОЛА', 3) == 'ОКШАЛ'
import numpy as np
from collections import deque
from itertools import combinations
# data = np.array([1487, 1847, 4817, 4871, 7481, 7841, 8147, 8741])
np.random.seed(42)
data = np.random.randint(0, 100000, 1000)
data.sort()
# Интуитивный подход (baseline), O(n^3)
def find_evenly_spaced_v1(data):
for a, b, c in combinations(data, 3):
if b - a == c - b:
yield a, b, c
# Эмпирическая оптимизация, O(n^2)
def find_evenly_spaced_v2(data):
dataset = set(data)
for a, c in combinations(data, 2):
b, mod = divmod(a + c, 2)
if (mod == 0) and (b in dataset):
yield a, b, c
# Векторный вариант
def find_evenly_spaced_v3(data):
grid = data.reshape(-1, 1) + data.reshape(1, -1)
divs, mods = np.divmod(grid, 2)
mask = np.tri(*grid.shape, -1, dtype='bool') & np.isin(divs, data) & ~ mods.astype('bool')
for c, a in zip(*np.where(mask)):
b = divs[a, c]
a, c = data[[a, c]]
yield a, b, c