Sum of positive

2018-11-29  本文已影响0人  是不及呀
* *
链接 Sum of positive
难度 8kyu
状态
日期 2018-11-30

题意

题解1

def positive_sum(arr):
    # Your code here
    sum = 0
    len1 = len(arr)
    a = 0
    while a < len1:
        if arr[a] > 0:
            sum = sum + arr[a]
            a = a + 1
        else:
            a = a + 1
    return sum

题解2

def positive_sum(arr):
    # Your code here
    sum = 0
    length = len(arr)
    i = 0
    while i < length:
        if arr[i] > 0:
            sum += arr[i]
        i += 1
    return sum

题解3

def positive_sum(arr):
    # Your code here
    sum = 0
    length = len(arr)
    for i in range(0, length):
        if arr[i] > 0:
            sum += arr[i]
    return sum

题解4

def positive_sum(arr):
    # Your code here
    sum = 0
    for n in arr:
        if n > 0:
            sum += n
    return sum

题解5

def positive_sum(arr):
    # Your code here
    return sum([c for c in arr if c > 0])
上一篇 下一篇

猜你喜欢

热点阅读