Quantcast
Channel: Count and summation of positive and negative number sequences - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by Sinan Kurmus for Count and summation of positive and negative number sequences

$
0
0

A simple python answer, ignores the 0 case:

x = [-0.01, 0.003, -0.002, 0.018,      0.002, 0.006, -0.012, 0.014,      -0.017, -0.007, 0.002, 0.002,      -0.004, 0.015, 0.002, -0.001,      -0.008, 0.01, -0.018, 0.046]count = 0sign_positive = x[0] > 0sign_count = []for n in x:    # the idea is to keep track of the sign and increment the     # count if it agrees with the current number we are looking at    if (n > 0 and sign_positive) or (n < 0 and not sign_positive):        count = count + 1    # if it does not, the count goes back to 1    else:        count = 1    # Whether we increased the count or not, we update whether the    # sign was positive or negative    sign_positive = n > 0    sign_count.append(count)# This is just to reproduce the output # (although I find the last repetition of the number unnecessary)    results = list(zip(x, sign_count))for i, result in enumerate(results):    print(f"{i: >2d} {result[0]: .3f} {result[1]: >2d} {result[0]: .3f}") 0 -0.010  1 -0.010 1  0.003  1  0.003 2 -0.002  1 -0.002 3  0.018  1  0.018 4  0.002  2  0.002 5  0.006  3  0.006 6 -0.012  1 -0.012 7  0.014  1  0.014 8 -0.017  1 -0.017 9 -0.007  2 -0.00710  0.002  1  0.00211  0.002  2  0.00212 -0.004  1 -0.00413  0.015  1  0.01514  0.002  2  0.00215 -0.001  1 -0.00116 -0.008  2 -0.00817  0.010  1  0.01018 -0.018  1 -0.01819  0.046  1  0.046

A little more sophisticated solution, also takes care of the 0 case:

# To test the 0 case I am changing two numbers to 0x = [-0.01, 0.003, -0.002, 0.018,      0.002, 0.006, -0.012, 0.014,     -0.017, -0.007, 0, 0,     -0.004, 0.015, 0.002, -0.001,     -0.008, 0.01, -0.018, 0.046]# The rest is similarcount = 0# This time we are using a nested ternary assignment # to account for the case of 0# This would be more readable as a function, # but what it does is simple# It returns None if n is 0, # True if it is larger than 0 # and False if it less than 0sign_positive = None if n == 0 else False if n < 0 else Truesign_count = []for n in x:    # We add the case of 0 by adding a third condition where    # sign_positive was None (meaning the previous    # number was 0) and the current number is 0.    if (n > 0 and sign_positive) or \       (n < 0 and not sign_positive) or \       (n == 0 and sign_positive == None):        count = count + 1    else:        count = 1    sign_positive = None if n == 0 else False if n < 0 else True    sign_count.append(count)results = list(zip(x, sign_count))for i, result in enumerate(results):    print(f"{i: >2d} {result[0]: .3f} {result[1]: >2d} {result[0]: .3f}") 0 -0.010  1 -0.010 1  0.003  1  0.003 2 -0.002  1 -0.002 3  0.018  1  0.018 4  0.002  2  0.002 5  0.006  3  0.006 6 -0.012  1 -0.012 7  0.014  1  0.014 8 -0.017  1 -0.017 9 -0.007  2 -0.00710  0.000  1  0.00011  0.000  2  0.00012 -0.004  3 -0.00413  0.015  1  0.01514  0.002  2  0.00215 -0.001  1 -0.00116 -0.008  2 -0.00817  0.010  1  0.01018 -0.018  1 -0.01819  0.046  1  0.046

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>