본문 바로가기
python

[code signal / 코드 시그널] alternatingSums

by 래끼 2022. 5. 30.
728x90
반응형

 

 

✨ Description

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.

 

✨ 문제

여러 명이 일렬로 서 있는데 두 팀으로 나눠야 한다. 첫 번째 사람이 1팀으로 들어가고, 두 번째 사람이 2팀으로 들어가고, 세 번째 사람이 1팀으로 들어가고, 네 번째 사람이 2팀으로 들어가는 식으로 나눈다.
입력값으로는 사람들의 무게인 양의 정수 배열을 받게 된다.

반환값으로는 두 정수의 배열을 반환하는데 첫 번째 요소는 팀 1의 총 중량이고 두 번째 요소는 분할이 완료된 후 팀 2의 총 중량이다.

👉 즉, 배열에 있는 사람들을 팀1과 팀2에 번갈아서 분할한 후 각 팀의 총합을 배열로 반환하는 문제입니다.

 

✨ Example

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

 

 

✨ My Submission

def solution(a):
    return [sum(a[::2]),sum(a[1::2])]

 

728x90
반응형

댓글