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 be
alternatingSums(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])

最新文章

  1. 「译」JUnit 5 系列:扩展模型(Extension Model)
  2. 查看最点CPU的语句
  3. 笔记007:对象——RegExp正则表达式对象
  4. 获取本机IP_考虑多网卡的情况
  5. java7新特性 java8新特性
  6. word模版另存为网页(*.htm,*.html),转为jsp页面并加入数据后导出成word
  7. ural 1020 Rope
  8. Asp.Net调用Office组件操作时的DCOM配置 (转)
  9. synchronized和lock比对
  10. Springboot与Thymeleaf模板引擎整合基础教程(附源码)
  11. grep 、find 、tree 新发现
  12. 07_Linux目录文件操作命令4解压缩,文件查找_我的Linux之路
  13. Spring Boot配置大全
  14. Python之ftp服务器
  15. Tensorflow-gpu版本安装
  16. Rocket Typist for Mac(增强型文本快速输入工具)破解版安装
  17. java-方法重写的注意事项
  18. MFC进程的创建销毁、线程的创建与交互
  19. Python 3 的安装
  20. 摘要JSR168 PORLET标准手册汉化整理

热门文章

  1. Hadoop完全分布式搭建
  2. Chrome控制台打印输出彩色调试信息
  3. 我把阿里云centos gcc从4.4.7升级到4.8.2的经历
  4. 总结day5 ---- ,字典的学习,增删改查,以及字典的嵌套, 赋值运算
  5. 架构师养成记--37.简单shell编程
  6. RunningCassandraInEclipse(转载)
  7. Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)
  8. 关闭MAC特效
  9. SASS的安装和转换为CSS的方法
  10. Table '.\gts\eventdata#P#p0' is marked as crashed and last (automatic?) repair failed