Created
January 30, 2015 16:30
-
-
Save jpoler/40d1c042d3195d19d2da to your computer and use it in GitHub Desktop.
Quora trends draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
from itertools import islice | |
import pdb | |
NON_INCREASING = -1 | |
NON_DECREASING = 1 | |
def summation(x): | |
return (x * (x -1))/2 | |
class Node(object): | |
def __init__(self, start, stop, direction): | |
self.start = start | |
self.stop = stop | |
self.direction = direction | |
def __repr__(self): | |
return "<Node -- start: {}, stop: {}, direction: {}".format(self.start, | |
self.stop, | |
self.direction) | |
class Manager(object): | |
def __init__(self, array): | |
self.array = array | |
self.positive_deque = deque() | |
self.negative_deque = deque() | |
def find_trend(self, prev, current, direction): | |
result = current - prev | |
def count_trends(self, k): | |
window_count = 0 | |
output = [] | |
positive_sum = 0 | |
negative_sum = 0 | |
positive_trend = 1 | |
negative_trend = 1 | |
prev = self.array[0] | |
for i, val in enumerate(islice(self.array, k)): print(i, val) | |
for i, val in enumerate(islice(self.array, k)): | |
if self.find_trend(prev, val, NON_DECREASING): | |
positive_trend += 1 | |
else: | |
self.positive_deque.append(Node(i-positive_trend, i, NON_DECREASING)) | |
positive_sum += summation(positive_trend) | |
positive_trend = 1 | |
if self.find_trend(prev, val, NON_INCREASING): | |
negative_trend += 1 | |
else: | |
self.negative_deque.append(Node(i-negative_trend, i, NON_INCREASING)) | |
negative_sum += summation(negative_trend) | |
negative_trend = 1 | |
prev = self.array[i] | |
output.append(positive_sum - negative_sum) | |
print("Window {}".format) | |
print(self.positive_deque) | |
print(self.negative_deque) | |
for i, val in enumerate(islice(self.array, k, len(self.array)), k): | |
print("Window {}".format(i | |
print(self.positive_deque) | |
print(self.negative_deque) | |
if self.find_trend(prev, val, NON_DECREASING): | |
if positive_trend == k: | |
pass | |
else: | |
positive_trend += 1 | |
if positive_trend == 2: | |
self.positive_deque.append(Node(i - 1, i, NON_DECREASING)) | |
else: | |
node = self.positive_deque[-1] | |
node.stop += 1 | |
if self.find_trend(prev, val, NON_INCREASING): | |
if negative_trend == k: | |
pass | |
else: | |
negative_trend += 1 | |
if negative_trend == 2: | |
self.negative_deque.append(Node(i - 1, i, NON_INCREASING)) | |
else: | |
node = self.negative_deque[-1] | |
node.stop += 1 | |
if self.positive_deque: | |
node = self.positive_deque[0] | |
if node.start < i - k: | |
node.start += 1 | |
if node.start == node.stop: | |
self.positive_deque.popleft() | |
if self.negative_deque: | |
node = self.negative_deque[0] | |
if node.start < i - k: | |
node.start += 1 | |
if node.start == node.stop: | |
self.negative_deque.popleft() | |
if __name__ == '__main__': | |
manager = Manager([1,2,3,1,1]) | |
print(manager.count_trends(3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment