[英]How to find all combinations that equal a given sum?
In SQL I have a list of transaction types and a value. 在SQL中,我有一个事务类型列表和一个值。 Simple example below.
简单的例子如下。
|-----------|--------|
| Tran.Type | Value |
|-----------|--------|
| 04 | 10 |
|-----------|--------|
| 07 | 10 |
|-----------|--------|
| 04 | 5 |
|-----------|--------|
| 01 | 8 |
|-----------|--------|
|-----------------|
| Current Balance |
|-----------------|
| 13 |
|-----------------|
Goal is to sum values and get number 13. In this example is easy to find that type 07 is negative. 目标是对值求和并获得数字13.在此示例中很容易发现类型07为负数。 So 10-10+5+8=13
所以10-10 + 5 + 8 = 13
But when number of transaction gets to 20 & 10 different types is quite difficult. 但是当交易数量达到20和10种不同类型时非常困难。 Also values are positive & negative.
值也是正面和负面的。
So far I came up with this. 到目前为止,我想出了这个。
import itertools
numbers = list(map(int, input("Enter multiple values: ").split()))
expectedSum = int(input("Enter expected value: "))
result = [seq for i in range(len(numbers), 0, -1) for seq in
itertools.combinations(numbers, i) if sum(seq) == expectedSum]
print (result)<code>
Any idea how can I incorporate type to result section? 任何想法如何将类型合并到结果部分? and if that type is an opposite value or not?
如果该类型是相反的值?
There are a couple of ways to do this, by using CASE
in your SQL statement OR if you have a VERY large list then introduce a field which will contain +1 or -1 based on the Transaction Type, then when you are performing the values MULTIPLY the value and finally do a sum. 有几种方法可以做到这一点,在SQL语句中使用
CASE
,或者如果你有一个非常大的列表然后根据事务类型引入一个包含+1或-1的字段,那么当你执行这些值时MULTIPLY值,最后做一笔总和。 Example, is here; 例子,在这里;
ALTER TABLE Tran ADD COLUMN tran_multiple INT default 1;
-- update each value as req.
UPDATE Tran set tran_multiple=-1 WHERE Tran.id = xxxx;
SELECT sum (TranValue.Value * Tran.tran_multiple) as Val From TranValue
INNER JOIN Tran on TranValue.Type = Tran.Type
Here I am assuming that your table TranValue stores the values. 在这里,我假设您的表TranValue存储值。 Of course, you can use Group By clause
GROUP BY Tran.type
in your statement, along with Tran.type
in your SELECT field list (for listing by Tran.Type & Values). 当然,你可以使用Group By子句
GROUP BY Tran.type
在你的声明,随着Tran.type
在SELECT字段列表(由Tran.Type和价值观上市)。 Then writing a simple code should not be a problem. 然后编写一个简单的代码应该不是问题。
Though, I would request you to refrain from using VALUE
or VALUES
in your SQL as it is very similar to the RESERVED KEYWORDs
( just a suggestion ) 虽然,我会请求你不要在你的SQL中使用
VALUE
或VALUES
,因为它与RESERVED KEYWORDs
非常相似( 只是一个建议 )
I am not putting the CASE
example here since you didn't specify which Database you are using (syntax varies) and also since you claim that there are a large number of types. 我没有在这里放置
CASE
示例,因为您没有指定您正在使用的数据库(语法各不相同),并且因为您声称存在大量类型。
If you prefer coding instead of SQL, then the same can be done by defining a dictionary where each type holds +1 or -1 depending on the type and then when the user inputs the values you can multiply type and get it done. 如果你喜欢的编码,而不是SQL,则同样可以通过定义字典,其中每种类型的成立做了+1或-1取决于类型 ,然后当用户输入的值,你可以乘类型和完成它。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.