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) + ...
随机推荐
- javascript简要笔记
零. 数据 0. 变量 分为字符串,数字,undefined, null,对象 undefined类型是只声明了变量,但是没赋值 可以使用typeof()函数来查看变量类型 例子1 var ...
- Oracle执行SQL语句的过程
转载至:http://blog.csdn.net/aqszhuaihuai/article/details/7024551 当我们提交一条sql语句时,Oracle会做哪些操作呢? Oracle会为每 ...
- 解决Python向MySQL数据库插入中文数据时出现乱码
解决Python向MySQL数据库插入中文数据时出现乱码 先在MySQL命令行中输入如下语句查看结果: 只要character_set_client character_set_database ch ...
- C#-WebForm-网页中Form表单中给回车绑定按钮
WEB端: <form id="form1" runat="server" defaultbutton="btnSearch" ...
- Apache环境修改.htaccess文件实现子目录强制HTTPS访问
如果要在Apache环境下实现子目录强制HTTPS地址访问,该怎么实现呢?在此文章中将与大家一起分享如何在Apache环境下修改.htaccess文件来实现子目录强制HTTPS地址访问. 1.根目录域 ...
- C++ class和struct的区别
class和struct定义类唯一的区别就是默认的访问权限. 如果我们使用struct关键字,则定义在第一个访问说明符之前的成员是public的:相反,如果我们使用class关键字,组这些成员是pri ...
- easy-mock本地部署成功,访问报错:EADDRNOTAVAIL 0.0.0.0:7300 解决方案
easy-mock本地部署成功后,迫不及待的想看看是否能正常访问,执行命令 npm run dev 启动项目,访问 127.0.0.1:7300 ,结果郁闷的是报错:EADDRNOTAVAIL 0.0 ...
- TEMP_CHEMISTRY
1.\[CuSO_4\ and\ excess\ Ba(OH_2)\ :\ Cu^{2+}+SO_4^{2-}+Ba^{2+}+2OH^- \xrightarrow{\quad\quad} Cu(OH ...
- server端获得到client端的IP地址的格式
使用telnet,ping或其他client连接server端时,server端获得的client端的ip地址取决于client端使用的时ipv4还是ipv6地址. 例: client IPv4地址: ...
- shiro学习笔记_0500_授权
1,授权:给身份认证通过的人,授予他可以访问某些资源的权限. 2,权限粒度:分为粗粒度和细粒度. 粗粒度:例如对 user 的 crud,也就是通常所说的对表的操作. 细粒度:对表中记录的操作.如 只 ...