Example


Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.

You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.


Example


For a = [50, 60, 60, 45, 70], the output should be
alternatingSums(a) = [180, 105].


Input/Output

  • [input] array.integer a

    Guaranteed constraints:
    1 ≤ a.length ≤ 10,
    45 ≤ a[i] ≤ 100.

  • [output] array.integer



Solution

def alternatingSums(a):

lst = [a[i] for i in range(len(a)) if i == 0 or i % 2 == 0]

lst2 = [a[i] for i in range(len(a)) if a[i] not in lst]


return [sum(lst), sum(lst2)]


*lst : 짝수번째에 해당하는 숫자를 저장하는 리스트입니다.

*lst2 : 홀수번째에 해당하는 숫자를 저장하는 리스트입니다.



'Programming > Algorithm' 카테고리의 다른 글

[Algorithm] areSimilar  (0) 2018.02.26
[Algorithm] addBoarder  (0) 2018.02.26
[Algorithm] reverseParentheses  (0) 2018.02.20
[Algorithm] sortByHeight  (0) 2018.02.09
[Algorithm] isLucky  (0) 2018.02.09

+ Recent posts