def days_till_warming(T):
counts = []
for i, curr_temp in enumerate(T):
for j, next_temp in enumerate(T[i+1:], i + 1):
if next_temp > curr_temp:
delta = j - i
break
else:
delta = 0
counts.append(delta)
return counts
import cv2
import numpy as np
from PIL import Image
from itertools import product
N = 42
W = 200
H = 200
def distance(p1, p2):
y1, x1 = p1
y2, x2 = p2
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
canvas = np.zeros((H, W, 3), dtype=np.uint8)
centers = np.unravel_index(np.random.randint(0, W * H, N), canvas.shape[:2])
canvas[centers] = np.random.randint(0, 256, (N, 3))
centers = list(zip(*centers))
for x in range(0, W):
for y in range(0, H):
cy, cx = min(centers, key=lambda center: distance(center, (y, x)))
canvas[y, x] = canvas[cy, cx]
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
if int(x) == x:
...
assert int(1.0) == 1.0
if type(x) is int:
...
data = [1.0, 1.23, 2.0, 2.71, 3.0, 3.14]
for x in filter(float.is_integer, data):
print(x)
from time import time, sleep
from operator import attrgetter
class Article:
def __init__(self, timestamp, text):
self.timestamp, self.text = timestamp, text
def post(self):
print(f"planned={self.timestamp:.0f}, posted={time():.0f}, text={self.text}")
class Scheduler:
def __init__(self, *articles):
self.articles = sorted(articles, key=attrgetter("timestamp"))
def execute(self):
for article in self.articles:
sleep(max(article.timestamp - time(), 0))
article.post()
if __name__ == "__main__":
now = time()
Scheduler(
Article(now + 7, "post3"),
Article(now + 2, "post1"),
Article(now + 3, "post2"),
).execute()
xcache = dict()
while cache:
key, val = cache.iteritems().next()
xcache[cache.pop(key)] = key
Такой способ в 3 раза величивает время исполнения скрипта (с 60 до 180 секунд на моём ноутбуке) по сравнению с традиционным методом инвертирования "в лоб". Есть ли способ лучше?xcache = dict()
cpop = cache.pop
while cache:
key = cache.iterkeys().next()
xcache[cpop(key)] = key
del cache
xcache = dict()
cpop = cache.popitem
while cache:
key, val = cpop()
xcache[val] = key
del cache
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from itertools import product
def process(existing, required):
for opcount in xrange(existing+required):
for combo in product('dcv', repeat=opcount):
buffer = 0
smiles = existing
data_y = [existing]
for char in combo:
if char == 'd':
smiles -= 1
elif char == 'v':
smiles += buffer
else:
buffer = smiles
data_y.append(smiles)
if smiles == required:
data_x = xrange(opcount+1)
plt.plot(data_x, data_y, linewidth=1)
print required, data_y
return ''.join(combo)
return '-'
for required in xrange(2, 101):
plt.clf()
plt.grid(True)
plt.title('Target: %s' % required)
plt.autoscale(enable=True, axis='both', tight=False)
for existing in xrange(1, (required//2)+1):
result = process(existing, required)
plt.savefig('{:02d}.png'.format(required), dpi=96, bbox_inches='tight')
def find_word(line):
for step in xrange(1, len(line)):
for start in xrange(step):
if len(set(line[(start or None)::step])) != 1:
break
else:
return line[:step]
return line
print find_word('HELLOHELLOHELLOHEL')
print find_word('HHHELLOHHHELLOHHHELLOHHH')
print find_word('_HHELLOHHELLOHHELLOHHELLOHHELLOHHE')
HELLO
HHHELLO
_HHELLOHHELLOHHELLOHHELLOHHELLOHHE
# -*- coding: utf-8 -*-
from random import randint
from itertools import combinations
#data = [53, 50, 74, 4, 11, 68, 65, 50, 25, 84, 65, 63, 14, 54, 49, 17, 65, 48, 74, 66]
data = [randint(1,100) for i in xrange(25)]
#data = [990,1,2,3,1000]
data.sort(reverse=True)
limit = float(sum(data))/2
# Pre-calculate min and max possible number of elements in a group
bounds = [0, 0]
for i in (0,1):
maxsum = 0
for n, item in enumerate(data[::(1,-1)[i]], 1):
cursum = maxsum + item
if cursum <= limit:
maxsum = cursum
else:
bounds[i] = n - int(n > 1)
break
nmin, nmax = bounds
# Main calculations
maxsum, cycles = 0, 0
for n in xrange(nmin, nmax+1):
# The following check could be efficient with integer numbers, not float
if maxsum == limit:
# One of exact solutions has been found
break
else:
# Iterate through all possible combinations:
# combinations('ABCD', 2) --> AB AC AD BC BD CD
for vector in combinations(data, n):
cycles += 1
cursum = sum(vector)
if maxsum < cursum <= limit:
maxsum = cursum
solution = vector
# The following check could be efficient with integer numbers, not float
if maxsum == limit:
# One of exact solutions has been found
break
elif cursum <= maxsum:
# No reason to continue because data is sorted in descending order
# and all of the following sums in this loop will be even smaller
break
# Print result
print 'data =', data
print 'nmin =', nmin
print 'nmax =', nmax
print 'solution =', solution
print 'cycles =', cycles
print 'limit =', limit
print 'sum =', maxsum