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) + ...
随机推荐
- spring管理hibernate session的问题探究
我们再用spring管理hibernate的时候, 我们会继承HibernateDaoSupport 或者HibernateTemplate类. 我们不知道这两个类之间有什么关系. 也没有去关闭ses ...
- [Objective-C语言教程]复合对象(33)
在Objective-C中,可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类. 这些类对象是复合对象.你可能想知道什么是类集群,下面首先了解什么是类集群. 1. 类集群 类集群是基础框架广泛使 ...
- 我从Linux走来,选择了Windows
我从Linux走来,选择了Windows 几天前就想提笔写下这篇,理解很多人一定会对言论不苟同. 但是我相信您看完一个一年多的Linux用户写完的以后,一定也不会太反对 一.为什么我成为一名 Linu ...
- CentOS6.5下samba服务
为减少错误已提前关掉了SELinux,防火墙. 安装rpm包: samba-3.6.9-164.el6.x86_64.rpm 启动检测:samba服务可以正常启动!(证明RPM安装正常) 配置文件位置 ...
- python os用法详解
前言:在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块,所以今天整理下比 ...
- MNIST手写数字识别 Tensorflow实现
def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 1. strides在官方定义中是一 ...
- axiso 生产环境跨域配置(可用)
1.npm install axios 后 在main.js中import import Axios from 'axios'Vue.prototype.$http = Axios 2.请求配置 th ...
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...
- WPF 将TextBox更改为PasswordBox样式(文字显示方式为密码)
在TextBox样式中增加如下所诉: <Style x:Key="TxtPwd" TargetType="{x:Type TextBox}"> &l ...
- MySQL之mysql命令使用详解
MySQL Name mysql - the MySQL command-line tool Synopsis mysql [options] db_name Description mysql is ...