简体   繁体   中英

python excersice: lists sums

i have this excesice:

*Given a list L, we indicate with the generic element of L and with the element in a symmetrical position to. Write it down a function examine_list which receives a list L of positive integers of even length and returns a boolean. In in particular, the function returns True if and only if, for each element of L, the sum of the values of and is greater the sum of the values of the elements positioned between and. Note that, when and are adjacent, the sum of the values of the elements positioned between and can be assumed equal to zero. Example: If L = [12, 9, 7, 2, 1, 1, 3, 12] then the function returns True, because:

  • 12 + 12> 9 + 7 + 2 + 1 + 1 + 3;
  • 9 + 3> 7 + 2 + 1 + 1;
  • 7 + 1> 2 + 1;
  • 2 + 1> 0.*

My code is this:

def sum_list(l):
    list_sum = []
    pst_num = 0
    ult_num = -1
    for num in range(len(l)//2):
        list_sum.append(l[pst_num]+l[ult_num])
        pst_num +=1
        ult_num -=1
    return list_sum


def examine_list(l):
    somme_xd = sum_list(l)
    list_without_first_nums = []
    first = 1
    last = -1
    for n in range(len(l)//2):
        list_without_first_nums.append(l[first:last])
        first += 1
        last -= 1
 
    st_sum = 0
    count = 0
    for lists in range(len(list_without_first_nums)):
        for nums in range(len(list_without_first_nums[lists])):
            if somme_xd[st_sum] >= sum(list_without_first_nums[lists][nums]):
                st_sum += 1
                count += 1
                if count == len(somme_xd):
                    return True
            else:
                return False
                        
L = [12, 9, 7, 2, 1, 1, 3, 12]
examine_list(L)

I have created the sum_list who create a list of sums of the array. my problem is the 2nd function: sum gives me always thid error:

Traceback (most recent call last):
  File "C:\Users\ALESSA~1\AppData\Local\Temp\tempCodeRunnerFile.python", line 35, in <module>    
    examine_list(L)
  File "C:\Users\ALESSA~1\AppData\Local\Temp\tempCodeRunnerFile.python", line 26, in examine_list
    if somme_xd[st_sum] >= sum(list_without_first_nums[lists][nums]):
TypeError: 'int' object is not iterable

You could use recursion for this:

def examine_list(L):
    return not L or L[0]+L[-1]>sum(L[1:-1]) and examine_list(L[1:-1])

L = [12, 9, 7, 2, 1, 1, 3, 12]
print(examine_list(L)) # True

Or a comprehension with the all() function:

def examine_list(L):
    return all(L[i]+L[-1-i]>sum(L[i+1:-1-i]) for i in range(len(L)//2))

To avoid repeatedly adding subranges of the list (which is somewhat inefficient), you can do it in a loop that starts out with the total and progressively decreases it with the leftmost/rightmost items as it progresses inwards:

def examine_list(L):
    total = sum(L)
    for a,b in zip(L,reversed(L)):     # progress inwards from both sides
        total  -= a+b                  # reduce total to get sum in between
        if a+b <= total: return False  # not x+x' > in-between sum
        if total == 0: break           # reached middle, stop
    return True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM