通过数字解码组合表算法在Python 3 [英] decoding via number combinations algorithm in python 3

查看:131
本文介绍了通过数字解码组合表算法在Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定,所以这里的问题。

让我们说:

  1代表鲍勃·
2指杰里
3是指汤姆
4指亨利
 

的上述两个数字的总和,任意组合是一个状态/情绪类型,是程序怎么会连接codeD:

  7(4 + 3)表示愤怒
5(3 + 2)menas悲伤
3(2 + 1)指狂
4(3 + 1)指快乐
等等...
 

我怎么可能创建一个去code函数,使得它接受的附加(EN codeD)值,如7,5,3,4,等,并计算出的组合,并返回一个人的名字重新presenting的constitue组合中的两个数字。注意到,1号不能被重复以获得情绪结果,这意味着4具有为3 + 1,并可能并不2 + 2。所以我们可以承担这个例子中,只有一个对各状态/情绪code可能的组合。现在的问题是,你如何在Python 3实现这样code?会是什么算法或逻辑为这样的问题。你如何寻求或检查两个数字的组合?我想我应该只运行一个循环,保持在两数相加的时间,直到结果和状态/情绪code匹配。将这项工作?但这种方法很快就会过时IF组合的数量增加(如加入,而不是2 4数相加)。做这种方式会占用大量的时间,并有可能是低效的。

我很抱歉,我知道这个问题是非常混乱,但请大家多多包涵。 让我们尝试一起想办法。

解决方案

使用二进制

如果你想有款项是唯一的,然后分配每个可能的人这个数字是2,这些数字将唯一标识号是在总和中使用的任意组合的总和力量。

1,2,4,8,16,...

,而不是提供正确性的详细证明,我提供关于这一个直观的论点:任意数量的可重新psented在基座2 $ P $,它总是2的幂的正好一个组合的总和

此解决方案可能不是最佳的。它具有现实的局限性(32或64个不同的人的标识,除非你使用某种BigInt有),但根据您的需要,它可能工作。具有最小的可能值,二是比不过其他的基数比较好。

示例

(编辑)

下面是一个简单的代码片段,演示了如何可以得C的总和$ C $。返回的值是权力的指数2 count_persons 可以任意大,因为可能的范围内 N 遍历(就象一个简单的例子)。

 #!的/ usr / bin中/ python3

count_persons = 64

对于n的范围(20,30):
    匹配=列表(滤波器(拉姆达I:(正> I标记)及为0x1,范围(1,count_persons)))
    打印({0} {1}。格式(N,匹配))
 

输出:

  20:[2,4]
21:[2,4]
22:[1,2,4]
23:[1,2,4]
24:[3,4]
25:[3,4]
26:[1,3,4]
27:[1,3,4]
28:[2,3,4]
29:[2,3,4]
 

ok so here is the problem.

let's say:

1 means Bob
2 means Jerry
3 means Tom
4 means Henry

any summation combination of two of aforementioned numbers is a status/ mood type which is how the program will be encoded:

7 (4+3) means Angry
5 (3+2) menas Sad
3 (2+1) means Mad
4 (3+1) means Happy
and so on...

how may i create a decode function such that it accepts one of the added (encoded) values, such as 7, 5, 3, 4, etc and figures out the combination and return the names of the people representing the two numbers that constitue the combination. take note that one number cannot be repeated to get mood result, meaning 4 has to be 3+1 and may not be 2+2. so we can assume for this example, that there is only one possible combination for each status/ mood code. now the problem is, how do you implement such code in python 3? what would be the algorithm or logic for such a problem. how do you seek or check for combination of two numbers? i'm thinking i should just run a loop that keeps on adding two numbers at a time until the result matches with the status/ mood code. will that work? BUT THIS METHOD WILL SOON BECOME OBSOLETE IF THE NUMBER OF COMBINATIONS IS INCREASED (as in adding 4 numbers together instead of 2). doing it this way will take up a lot of time and will possibly be inefficient.

i apologize, i know this questions is extremely confusing but please bear with me. let's try and work something out.

解决方案

Use Binary

If you want to have sums that are unique, then assign each possible "Person" a number that's a power of 2. The sum of any combination of these numbers will uniquely identify which numbers were used in the sum.

1, 2, 4, 8, 16, ...

Rather than offer a detailed proof of correctness, I offer an intuitive argument about this: any number can be represented in base 2, and it is always a sum of exactly one combination of powers of 2.

This solution may not be optimal. It has realistic limitations (32 or 64 different "person" identifiers, unless you use some sort of BigInt), but depending on your needs, it might work. Having the smallest possible values, binary is better than any other radix though.

Example

(Edited)

Here's a quick snippet that demonstrates how you could decode the sum. The returned values are the exponents of the powers of 2. count_persons could be arbitrarily large, as could the range of n iterated over (just as a quick example).

#!/usr/bin/python3

count_persons = 64

for n in range(20,30):
    matches = list(filter(lambda i: (n>>i) & 0x1, range(1,count_persons)))
    print('{0}: {1}'.format(n,matches))

Output:

20: [2, 4]
21: [2, 4]
22: [1, 2, 4]
23: [1, 2, 4]
24: [3, 4]
25: [3, 4]
26: [1, 3, 4]
27: [1, 3, 4]
28: [2, 3, 4]
29: [2, 3, 4]

这篇关于通过数字解码组合表算法在Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆