sum

How to sum negative and positive values separately when using groupby in pandas?

独自空忆成欢 提交于 2020-02-23 11:32:06
问题 How to sum positive and negative values differently in pandas and put them let's say in positive and negative columns? I have this dataframe like below: df = pandas.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C' : np.random.randn(8), 'D' : np.random.randn(8)}) Output is as below: df A B C D 0 foo one 0.374156 0.319699 1 bar one -0.356339 -0.629649 2 foo two -0.390243 -1.387909 3 bar three -0

Date interval sum and subtraction in Java

安稳与你 提交于 2020-02-23 10:14:02
问题 I'm looking for a library or helper class in Java that would allow me to perform date interval sum and subtractions. For example, lets's say I have the following date intervals: A = ["2015-01-01 00:00", "2015-01-20 00:00"] B = ["2015-01-05 00:00", "2015-01-10 00:00"] C = ["2015-01-11 00:00", "2015-01-14 00:00"] D = ["2015-01-19 00:00", "2015-01-25 00:00"] 1 A 20 |----------------------------------| |---------| |----------| |------------| 5 B 10 11 C 14 19 D 25 And let's say I'd like to

Date interval sum and subtraction in Java

旧城冷巷雨未停 提交于 2020-02-23 10:12:30
问题 I'm looking for a library or helper class in Java that would allow me to perform date interval sum and subtractions. For example, lets's say I have the following date intervals: A = ["2015-01-01 00:00", "2015-01-20 00:00"] B = ["2015-01-05 00:00", "2015-01-10 00:00"] C = ["2015-01-11 00:00", "2015-01-14 00:00"] D = ["2015-01-19 00:00", "2015-01-25 00:00"] 1 A 20 |----------------------------------| |---------| |----------| |------------| 5 B 10 11 C 14 19 D 25 And let's say I'd like to

How to find sum of even numbers in a list using recursion?

心不动则不痛 提交于 2020-02-23 05:46:07
问题 def sum_evens_2d(xss): i = 0 counter = 0 while (i < len(xss)): if(xss[i]%2 == 0): counter += xss[i] i= i+1 else: i = i+1 return(counter) I am trying to find the sum of the evens in the list xss . My restrictions are that I can not use sum() , but recursion only. 回答1: Just tested this one out, it should work: def even_sum(a): if not a: return 0 n = 0 if a[n] % 2 == 0: return even_sum(a[1:]) + a[n] else: return even_sum(a[1:]) # will output 154 print even_sum([1, 2, 3, 4, 5, 6, 7, 8, 23, 55, 45

CF327E Axis Walking

不问归期 提交于 2020-02-22 22:45:44
题意翻译 给你一个长度为n(1<=n<=24)的正整数序列S,再有k(0<=k<=2)个正整数。 求有多少种S的排列方式使得其前缀和不会成为那k个数里的任意一个。 答案对1e9+7取模。 题解: n<=24,考虑状压DP 设dp[S]表示当前已选的集合为S,sum[S]为当前集合的数的和 sum很好得到, s u m [ i ] = s u m [ i sum[i]=sum[i s u m [ i ] = s u m [ i ^ l o w b i t ( i ) ] + s u m [ l o w b i t ( i ) ] lowbit(i)]+sum[lowbit(i)] l o w b i t ( i ) ] + s u m [ l o w b i t ( i ) ] dp[S]可以由 ∑ d p [ x ] ( x ∈ S ) \sum dp[x](x∈S) ∑ d p [ x ] ( x ∈ S ) 得到,而x可以通过枚举S中的1,然后异或即可得到 AC代码: # include <bits/stdc++.h> # include <ext/rope> using namespace std ; using namespace __gnu_cxx ; # define LL long long # define endl '\n' const int MAXN = 1

F. Tree with Maximum Cost(换根)

懵懂的女人 提交于 2020-02-22 16:13:13
F. Tree with Maximum Cost 学习博客: https://blog.csdn.net/LJD201724114126/article/details/85240762?utm_source=app 题解: 换根 我们先设sum[u] 等于 以u为根的子树的∑a i (注意没有*距离),再计算以1为根的贡献值res。 这时我们要从父亲节点u向儿子节点v换根,故需要将res的值转化为以v为根的value 首先我们要先 res -= sum[v] ,因为换根之前,以v为根的子树(包括v)到原根u的深度比换根之后,到新根v的深度少了一,故减去 然后我们 sum[u] -= sum[v] ,因为原来sum[u]表示的是以u为根的值(包括了以v为根),而这时是以v为根的,故sum[u]要减去sum[v], res += sum[u] ,因为换根之前,以u为根的子树(包括u)到原根u的深度比换根之后,到新根v的深度多了一,故加上 最后 sum[v]+=sum[u] ,因为v要成为整个树的根,而原来sum[v]不包括以u为根的节点值,故要加上。 换完之后我们再换回来。 这里巧妙的应用了换根,就是先求出任意一个根的结果,然后我们dfs,将相邻的根换一换,求出结果。 AC_Code 1 #include <bits/stdc++.h> 2 using namespace std;

寻找和为定值的两个数

烈酒焚心 提交于 2020-02-22 07:40:49
题目: 输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。 解析:如果数组是 无序 的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断 a[i]+a[j]?=sum,如果某一刻a[i]+a[j]>sum,则要想办法让sum的值减小,所以此刻i不动,j--,如果某一刻 a[i]+a[j]<sum,则要想办法让sum的值增大,所以此刻i++,j不动。所以,数组无序的时候,时间复杂度最终为 O(n*logn+n)=O(n*logn),若原数组是 有序 的,则不需要事先的排序,直接O(n)搞定,且空间复杂度还是O(1),此思路是相对于上述 所有思路的一种改进 。(如果有序,直接两个指针两端扫描,时间O(N),如果无序,先排序后两端扫描,时间O(N*logN+N)=O(N*logN),空间始终都为O(1)) 。 总结 : 不论原序列是有序还是无序,解决这类题有以下三种办法:1、二分(若无序,先排 序后二分),时间复杂度总为O(n*logn),空间复杂度为O(1);2、扫描一遍X-S[i] 映射到一个数组或构造hash表

C语言I博客作业09

折月煮酒 提交于 2020-02-22 04:31:51
这个作业属于哪个课程 C语言程序设计I 这个作业要求在哪里 作业链接 我在这个课程的目标是 熟练掌握C语言,能独立完成简单项目,毕业前想做出一个简单的游戏 这个作业在那个具体方面帮助我实现目标 熟悉break与continue的使用和区别 参考文献 作业评价标准 1、PTA实验作业 1.1换硬币 问题描述:将一笔零钱换成5分、2分和1分的硬币,要求每种硬币至少有一枚,有几种不同的换法? 1.1.1 数据处理 数据表达:定义了5个整型变量,a,b,c分别表示5分,2分,1分硬币的个数,sum表示待换零钱值,count表示换法个数。 数据处理: 伪代码如下 定义a,b,c,sum,count; 输入数据sum; for(a=sum/5;a>0;a--)五分硬币个数 { for(b=(sum-a 5)/2;b>0;b--)两分硬币个数 { for(c=(sum-a 5-b 2);c>0;c--)一分硬币个数 { if(5 a+2*b+c==sum)选出符合条件的方案 计数器 输出方案 } } } 输出方案个数 return0; 1.1.2 实验代码截图 1.1.3 造测试数据 输入数据 输出数据 说明 13 见图 样例,正确 88 见图 随机数,正确 1.1.4 PTA提交列表及说明 1、输出超限:for语句表达式二有逻辑错误,导致程序死循环,均改成>0后正确。 2、答案错误:忘记换行了

洛谷P4360[CEOI2004]锯木厂选址

孤街浪徒 提交于 2020-02-21 22:39:41
https://www.luogu.com.cn/problem/P4360 做两遍dp,dp[k][i]表示前i个位置放k个锯木厂的最小值 先把整个顺序反过来,那么0的位置就是山脚的锯木厂 设sum[i],f[i]是sum[i]w[i]前缀和,f2[i]是w[i]的前缀和 dp[1][i]=f[i]-f[k]-sum[k+1](f2[i]-f2[k])+dp[0][k] 展开成f2[k]sum[k+1]-f[k]+dp[0][k]=f2[i]sum[k+1]+dp[1][i]-f[i] k=f2[i]递增,sum[k+1]递增,希望截距dp[1][i]-f[i]尽可能小 画图分析得用单调队列维护一个下凸包 #include<bits/stdc++.h> using namespace std; const int maxl=2e4+10; typedef long long ll; int n; ll w[maxl],d[maxl],sum[maxl],f[maxl],f2[maxl]; ll dp[3][maxl]; struct node { ll x,y; }s[maxl]; inline void prework() { scanf("%d",&n); for(int i=n;i>=1;i--) scanf("%lld%lld",&w[i],&d[i]); for(int

《Lintcode签到》41. 最大子数组

不羁的心 提交于 2020-02-20 02:21:52
描述 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。 样例 样例1: 输入:[−2,2,−3,4,−1,2,1,−5,3] 输出:6 解释:符合要求的子数组为[4,−1,2,1],其最大和为 6。 样例2: 输入:[1,2,3,4] 输出:10 解释:符合要求的子数组为[1,2,3,4],其最大和为 10 我的代码: public int maxSubArray(int[] arr) { // write your code here Integer max_sum = Integer.MIN_VALUE; int sum = 0; for (int i = 0; i < arr.length; i++) { if(sum < 0) { sum = 0; } sum = sum + arr[i]; if(max_sum < sum) { max_sum = sum; } } return max_sum; } 最大连续数组:暴力:三个for 时间复杂度:n的立方 分治:递归的/2 时间复杂度:nlogn 分析法: 时间复杂度:n 来源: CSDN 作者: lmy9213 链接: https://blog.csdn.net/lmy9213/article/details/104397852