i was helping my friend to do logic algorithm in python but i haven't come with best solution yet.
first of all, i have a list of array:
x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
and he wanted to categorize the x so the output become like this:
array([0,1,2,3,4]) # increasing value
array([4,3,2]) #decreasing value
array([2,3]) # increasing value
array([3,-2,-4,-7]) #decreasing value
array([-7,2]) # increasing value
array([2,2]) # remain_the_same_value
the rule is simple:
- if the value keep increasing (like example above: 0,1,2,3,4) they put in one array
- if the value keep decreasing (like example above: 3,-2,-4,-7) they put in one array
- but, if there is a sudden change in value pattern such as example above: from the increasing value (0,1,2,3,4) suddenly the next value is decreasing. the new array will be made and put the last increasing value which is (4) and monitor the next value, whether it is decreasing value or not. If yes, they will be put in one array. example :array([4,3,2])
- if the the value is remain the same (like example above, from 2 to 2). they will be put in one array.
this is what i come so far, but still far from the solution
#categorize which types of input
if len(x) > 2 :
for i in range(len(x)) :
if (x[i+1]-x[i]) > 0 and i+i < len(x) : # for increasing x value
elif (x[i+1]-x[i]) < 0 and i+i < len(x) : # for decreasing x value
elif (x[i+1]-x[i]) == 0 and i+i < len(x) : # for foward direction of vehicle
else :
print 'ERROR : check the input coordinates once again!'
best regards,
Glenn
array([3,-2]) #decreasing value array([-2,-4,-7]) #decreasing valueThey are both decreasing so why are they different?