Code Signal_练习题_alternatingSums
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 2after the division is complete.
Example
For a = [50, 60, 60, 45, 70]
, the output should bealternatingSums(a) = [180, 105]
.
我的解答:
def alternatingSums(a):
sum1 = sum2 = 0
for i in a[0::2]:
sum1 = sum1 + i
for j in a[1::2]:
sum2 = sum2 + j
return sum1,sum2
膜拜大佬:
def alternatingSums(a):
return sum(a[::2]),sum(a[1::2])
Code Signal_练习题_alternatingSums的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- php性能优化三(PHP语言本身)
0.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...
- 3分钟解决MySQL 1032 主从错误(转)
转自 https://blog.51cto.com/suifu/1845457 3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了, ...
- mycat引起的insert后马上select不到数据的故障分析
由于有2个task表t_task和e_task,代码中Insert了t_task后马上select t_task然后把结果Insert到e_task,结果发现经常e_task会没有任何数据. 原因分析 ...
- URL的组成和含义
1.URL - Uniform Resource Locator 当您点击 HTML 页面中的某个链接时,对应的 <a>标签指向万维网上的一个地址. 统一资源定位器(URL)用于定位万维网 ...
- window.open完美替代window.showModalDialog
var url = "http//:www.baidu.com/" var name = "百度"; var iWidth = 1100;//弹窗宽度 var ...
- LOJ2229. 「BJOI2014」想法(随机化)
题目链接 https://loj.ac/problem/2229 题解 评分标准提示我们可以使用随机化算法. 首先,我们为每一道编号在 \([1, m]\) 以内的题目(这些题目也对应了 \(m\) ...
- (二)Audio子系统之new AudioRecord()
在上一篇文章<(一)Audio子系统之AudioRecord.getMinBufferSize>中已经介绍了AudioRecord如何获取最小缓冲区大小,接下来,继续分析AudioReco ...
- 用js制作简易计算器及猜随机数字游戏
<!doctype html><html><head> <meta charset="utf-8"> <title>JS ...
- Ngin 简单配置文件
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- (转)Db2 备份恢复性能问题诊断与调优
原文:https://www.ibm.com/developerworks/cn/analytics/library/ba-lo-backup-restore-performance-issue-ju ...