반응형
Scenario
Several people are standing in a row divided into two teams.
The first person goes into team 1, the second goes into team 2, the third goes into team 1, and so on.
Task
Given an array of positive integers (the weights of the people), return a new array/tuple of two integers, where the first one is the total weight of team 1, and the second one is the total weight of team 2.
Notes
- Array size is at least 1.
- All numbers will be positive.
Input >> Output Examples
rowWeights([13, 27, 49]) ==> return (62, 27)
Explanation:
The first element 62 is the total weight of team 1, and the second element 27 is the total weight of team 2.
rowWeights([50, 60, 70, 80]) ==> return (120, 140)
Explanation:
The first element 120 is the total weight of team 1, and the second element 140 is the total weight of team 2.
rowWeights([80]) ==> return (80, 0)
Explanation:
The first element 80 is the total weight of team 1, and the second element 0 is the total weight of team 2.
Solution:
def row_weights(array):
team1 = sum([i for i in array[::2]])
return (team1, sum(array)-team1)
Other Solution:
def row_weights(array):
return sum(array[::2]), sum(array[1::2])
def row_weights(array):
odd = 0
even = 0
for i in range(len(array)):
if i%2 == 0:
odd += array[i]
else:
even += array[i]
return odd, even
row_weights = lambda arr: (sum(arr[::2]), sum(arr[1::2]))
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
5 without numbers !! (0) | 2022.08.19 |
---|---|
If you can't sleep, just count sheep!! (0) | 2022.08.17 |
Playing with digits (0) | 2022.08.15 |
Give me a Diamond (0) | 2022.08.14 |
Consecutive strings (0) | 2022.08.13 |