Created
February 2, 2017 02:49
-
-
Save fitzk/5536b4d725605856b8b0265ee58e2747 to your computer and use it in GitHub Desktop.
Data Processing: Given an array of integers with fixed positions (ie, stocks), this script calculates trends based on positive, negative, or plateaued growth. This function excludes information about the relationship between trends, though some relationships can be easily inferred.
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
# Trend: holds a subset of nodes m from a larger series y where | |
# the relationship between node m0 ... mlen(m-1) is one of positive growth, negative growth, | |
# or plateaued growth as evalulated from left to right | |
class Trend: | |
def __init__(self, node): | |
self.direction = "trend setter" | |
self.nodes = list() | |
self.length = 1 | |
self.nodes.append(node) | |
def getDirection(self): | |
return self.direction | |
def getNodes(self): | |
return self.nodes | |
def printNodes(self): | |
if self.length > 1: | |
print(self.nodes) | |
else: | |
print(self.nodes[0]) | |
def printTrend(self): | |
print(self.direction) | |
self.printNodes() | |
def getFirstNode(self): | |
return self.nodes[0] | |
def getLastNode(self): | |
return self.nodes[len(self.nodes)-1] | |
def determineDirection(self, n, n1): | |
if(n < n1): | |
return "positive" | |
if(n > n1): | |
return "negative" | |
elif(n == n1): | |
return "plateau" | |
def testDirection(self, n1): | |
n = self.getLastNode() | |
d = self.determineDirection(n, n1) | |
if not self.direction == d: | |
return False | |
else: | |
return True | |
def addSecondNode(self, n1): | |
n = self.getFirstNode() | |
self.direction = self.determineDirection(n, n1) | |
self.length = self.length + 1 | |
self.nodes.append(n1) | |
return True | |
# adds the new node to the nodes series | |
# if it's relationship to last node in the | |
# series upholds the overall trend | |
# if the node is rejected, the function returns | |
# false, otherwise true | |
def addNode(self, n1): | |
if self.length == 1: | |
return self.addSecondNode(n1) | |
if self.testDirection(n1): | |
self.nodes.append(n1) | |
self.length = self.length + 1 | |
return True | |
else: | |
return False | |
# @name findTrends | |
# @returns list of Trend objects | |
# @description Given an array of integers with fixed positions (ie, stocks), this function calculates trends based on | |
# positive, negative, or plateaued growth. This function excludes information about the relationship between trends, though | |
# some relationships can be easily inferred. | |
# Two postive trends in a row indicate a negative relationship between the last node of trend A and the first node of | |
# trend B such that the value of A.lastnode > B.firstnode, thus relationships between trends are not accounted for here | |
# example | |
# ie, given the series [ 1, 2, 7, 9, 7, 10, 12, 4, 4, 3, 1 ] | |
# Trend A: [ 1, 2, 7, 9 ] positive growth | |
# Trend B: [ 7, 10, 12 ] positive growth | |
# Trend C: [ 4, 4 ] plateaued growth | |
# Trend D: [ 3, 1 ] negative growth | |
def findTrends(c): | |
trends = list() | |
while len(c) > 0: | |
node = c.pop(0) | |
trend = Trend(node) | |
while True: | |
if len(c) == 0: | |
break | |
else: | |
node1 = c.pop(0) | |
result = trend.addNode(node1) | |
if not result: | |
c.insert(0,node1) | |
break | |
trends.append(trend) | |
return trends | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment