Python 查找相乘的数字以形成数字列表

Python 查找相乘的数字以形成数字列表,python,algorithm,Python,Algorithm,我有一个投票系统,人们对事物的评分从1到10。我可以得到总票数,但不能得到它们的分布。但是,我可以访问每个分数的总数(但不能访问它们的分数) 例如,假设两人投2票,三人投4票,那么我会得到[4,12]。从人的角度来说,这可能是为了计算出投票结果,但是因为有很多数据,我需要一个能够通过编程实现的算法。为了人道地解决这个问题,我会知道有5票,我会知道不是1票4票4票,就是2票2票3票4票。因为这不是最好的例子,我们无法判断它会是哪一个,所以在这种情况下,算法应该给出所有的解。在实际数据中,很少发生类

我有一个投票系统,人们对事物的评分从1到10。我可以得到总票数,但不能得到它们的分布。但是,我可以访问每个分数的总数(但不能访问它们的分数)

例如,假设两人投2票,三人投4票,那么我会得到[4,12]。从人的角度来说,这可能是为了计算出投票结果,但是因为有很多数据,我需要一个能够通过编程实现的算法。为了人道地解决这个问题,我会知道有5票,我会知道不是1票4票4票,就是2票2票3票4票。因为这不是最好的例子,我们无法判断它会是哪一个,所以在这种情况下,算法应该给出所有的解。在实际数据中,很少发生类似的情况

输入的形式为整数,表示投票总数,以及所有投票小计的列表。输出应该是一个包含PAR(x,y)的数组,其中有x个y投票。如果存在多个解决方案,则算法应在一个数组中返回所有解决方案

编辑: 以下是一些真实数据(17人投票):

答案(抱歉顺序不同):

我需要一个能够通过编程实现的算法

我先把你的总数分解一下

这将很快返回数字n的所有因子

那会让你在路上走得很好;显然,一个没有5分作为因子的列表总数并不是来自一个评级为5分的小组

步骤1)将总数列表中的每个数字计算在内

步骤2)创建字典,将所有可能的评级作为关键字;ie键1-10;每个都以空列表作为值

步骤3)检查每个因素列表是否有与每个评级键匹配的因素;如果是,则将其表示的总数附加到相应键的可能值列表中

from random import randint
from random import shuffle

VOTES = 200
RATINGS = 10

# Generate Test Data
ratings = {}
for i in range(1,(RATINGS+1)):
    ratings[i] = []
for i in range(VOTES):
    rate = randint(1,RATINGS)
    ratings[rate].append(rate)

subtotals = []
for key, values in ratings.iteritems():
   subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()

hidden_subtotals = {}
for key, values in ratings.iteritems():
   hidden_subtotals[key] = sum(values)

print '==================================='
print 'Generate Hidden Test Data'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals 
print '==================================='
print 'Present Problem'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), RATINGS)
print '==================================='

def factors(n):    
    f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
        1, int(n**0.5) + 1) if n % i == 0))))
    f.sort()
    return f

# Factor Each
subtotal_factors = {}
for i in subtotals:
    subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors

# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
   possible_ratings[i] = [
      z for z in subtotal_factors[i] if z<=RATINGS]
print 'process 2: %s' % possible_ratings

# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
    possible_ratings[i] = [
        z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings
来自随机导入randint
从随机导入洗牌
票数=200
评级=10
#生成测试数据
评级={}
对于范围(1)(额定值+1))内的i:
评级[i]=[]
对于范围内的i(票数):
额定值=额定值(1,额定值)
评级[比率]。追加(比率)
小计=[]
对于键,ratings.iteritems()中的值为:
小计。追加(总和(值))
小计=[如果i!=0,小计中的i代表i]
小计.排序()
隐藏的_小计={}
对于键,ratings.iteritems()中的值为:
隐藏的小计[键]=总和(值)
打印“====================================================”
打印“生成隐藏测试数据”
打印“未知实际投票数:%s”%s
打印“未知的实际小计:%s”%hidden\u小计
打印“====================================================”
打印“当前问题”
打印“已知投票总数:%s%”投票数
打印“已知小计:%s”%s小计
打印“已知总计:%s”%sum(小计)
打印“使用的分级数:%s,共%s%”(分级的小计)
打印“====================================================”
def系数(n):
f=列表(设置)(减少列表。uu添加范围内的i)([i,n//i])(
1,如果n%i==0),则int(n**0.5)+1)
f、 排序()
返回f
#各因素
因子小计={}
对于小计中的i:
因子小计[i]=(因子(i))
打印“进程1:%s”%subtotal\u因子
#删除大于最高评级的因素
可能的_评级={}
对于小计系数中的i:
可能的_评级[i]=[

如果z我现在已经解决了我的问题。我使用litepresence的代码作为基础,然后实现了他们描述的第四步。对于未来,我使用的代码是:

from random import randint
from random import shuffle
import itertools

NUM_RATINGS = 10
RATING = [10, 10, 10, 10, 10, 10, 9, 9, 8, 6, 6, 6, 5, 5, 4, 3, 1]
VOTES = len(RATING)


ratings = {}
for i in range(1, (NUM_RATINGS + 1)):
    ratings[i] = []
for i in RATING:
    ratings[i].append(i)


subtotals = []
for key, values in ratings.iteritems():
   subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()

hidden_subtotals = {}
for key, values in ratings.iteritems():
   hidden_subtotals[key] = sum(values)

print '==================================='
print 'Hidden Test Data:'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals 
print '==================================='
print 'Present Problem:'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), NUM_RATINGS)
print '==================================='

def factors(n):    
    f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
        1, int(n**0.5) + 1) if n % i == 0))))
    f.sort()
    return f

# Factor Each
subtotal_factors = {}
for i in subtotals:
    subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors

# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
   possible_ratings[i] = [
      z for z in subtotal_factors[i] if z<=NUM_RATINGS]
print 'process 2: %s' % possible_ratings

# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
    possible_ratings[i] = [
        z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings


end_res = {}
other = {}  # (count, [poss])
for i in possible_ratings.items():
    if len(i[1]) == 1:
        end_res[i[0]] = (i[1][0], i[1][0])
    else:
        other[i[0]] = (subtotals.count(i[0]), i[1])

combs = {}
for i in other.items():
    combs[i[0]] = (i[1][0], [])  # (count, [data])
    for a in i[1][1]:
        for b in i[1][1]:
            if (a, b) not in combs[i[0]]:
                if a * b == i[0]:
                    combs[i[0]][1].append((a, b))

lists = []
for i in combs.items():
    for j in range(i[1][0]):
        lists.append([])
        for n in i[1][1]:
            lists[-1].append((n[0], n[1], i[0]))

toprocess = itertools.product(*lists)
working = []

for test in toprocess:
    works = True
    seen = []
    tot = 0
    for i in test:
        if i[1] in seen:
            works = False
            break
        tot += i[0]
        if tot > VOTES:
            works = False
            break
        seen.append(i[1])
    if not works:
        continue
    else:
        working.append(test)

formattedWs = []
for w in working:
    w = list(w)
    notseen = [i + 1 for i in range(NUM_RATINGS)]
    for i in w:
        notseen.remove(i[1])
    for i in notseen:
        w.append((0, i))

    t = ""
    def f(x, y):
        return y[1] - x[1]

    w.sort(cmp=f)

    for i in w:
        t += "%d*%d + " % (i[0], i[1])
    t = t[:-3]
    formattedWs.append(t)

seen = []
for w in list(formattedWs):
    if w in seen:
        formattedWs.remove(w)
    else:
        seen.append(w)

print "==================================="
for n, w in enumerate(formattedWs):
    print "Solution #%d: %s" % (n + 1, w)
来自随机导入randint
从随机导入洗牌
进口itertools
数值=10
评级=[10,10,10,10,10,9,8,6,6,6,5,5,4,3,1]
投票数=len(评级)
评级={}
对于范围(1)内的i(NUM_评级+1)):
评级[i]=[]
对于评级中的i:
评级[i]。追加(i)
小计=[]
对于键,ratings.iteritems()中的值为:
小计。追加(总和(值))
小计=[如果i!=0,小计中的i代表i]
小计.排序()
隐藏的_小计={}
对于键,ratings.iteritems()中的值为:
隐藏的小计[键]=总和(值)
打印“====================================================”
打印“隐藏测试数据:”
打印“未知实际投票数:%s”%s
打印“未知的实际小计:%s”%hidden\u小计
打印“====================================================”
打印“当前问题:”
打印“已知投票总数:%s%”投票数
打印“已知小计:%s”%s小计
打印“已知总计:%s”%sum(小计)
打印“使用的分级数:%s,共%s%”(列(小计),分级数)
打印“====================================================”
def系数(n):
f=列表(设置)(减少列表。uu添加范围内的i)([i,n//i])(
1,如果n%i==0),则int(n**0.5)+1)
f、 排序()
返回f
#各因素
因子小计={}
对于小计中的i:
因子小计[i]=(因子(i))
打印“进程1:%s”%subtotal\u因子
#删除大于最高评级的因素
可能的_评级={}
对于小计系数中的i:
可能的_评级[i]=[
如果z投票,小计系数[i]中的z代表z:
工作=错误
打破
seen.append(i[1])
如果不起作用:
持续
其他:
working.append(测试)
formattedWs=[]
对于工作中的w:
w=列表(w)
notseen=[i+1表示范围内的i(NUM_评级)]
对于w中的i:
未看到。删除(i[1])
因为我没有看到:
w、 附加((0,i))
t=“”
定义f(x,y):
返回y[1]-x[1]
w、 排序(cmp=f)
对于w中的i:
t+=%d*%d+“%(i[0],i[1])
t=t[:-3]
formattedWs.append(t)
SEED=[]
对于列表中的w(格式化的WS):
如果看到w:
formattedWs.remove(w)
其他:
见附录(w)
打印“==========================================================”
对于枚举中的n,w(格式化的ws):
打印“解决方案#%d:%s”%(n+1,w)

当你有12人时,你怎么知道是3人投了“4”票还是4人投了“3”?你不会(这不是一个很好的选择)
def factors(n):    
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
from random import randint
from random import shuffle

VOTES = 200
RATINGS = 10

# Generate Test Data
ratings = {}
for i in range(1,(RATINGS+1)):
    ratings[i] = []
for i in range(VOTES):
    rate = randint(1,RATINGS)
    ratings[rate].append(rate)

subtotals = []
for key, values in ratings.iteritems():
   subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()

hidden_subtotals = {}
for key, values in ratings.iteritems():
   hidden_subtotals[key] = sum(values)

print '==================================='
print 'Generate Hidden Test Data'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals 
print '==================================='
print 'Present Problem'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), RATINGS)
print '==================================='

def factors(n):    
    f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
        1, int(n**0.5) + 1) if n % i == 0))))
    f.sort()
    return f

# Factor Each
subtotal_factors = {}
for i in subtotals:
    subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors

# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
   possible_ratings[i] = [
      z for z in subtotal_factors[i] if z<=RATINGS]
print 'process 2: %s' % possible_ratings

# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
    possible_ratings[i] = [
        z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings
from random import randint
from random import shuffle
import itertools

NUM_RATINGS = 10
RATING = [10, 10, 10, 10, 10, 10, 9, 9, 8, 6, 6, 6, 5, 5, 4, 3, 1]
VOTES = len(RATING)


ratings = {}
for i in range(1, (NUM_RATINGS + 1)):
    ratings[i] = []
for i in RATING:
    ratings[i].append(i)


subtotals = []
for key, values in ratings.iteritems():
   subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()

hidden_subtotals = {}
for key, values in ratings.iteritems():
   hidden_subtotals[key] = sum(values)

print '==================================='
print 'Hidden Test Data:'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals 
print '==================================='
print 'Present Problem:'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), NUM_RATINGS)
print '==================================='

def factors(n):    
    f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
        1, int(n**0.5) + 1) if n % i == 0))))
    f.sort()
    return f

# Factor Each
subtotal_factors = {}
for i in subtotals:
    subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors

# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
   possible_ratings[i] = [
      z for z in subtotal_factors[i] if z<=NUM_RATINGS]
print 'process 2: %s' % possible_ratings

# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
    possible_ratings[i] = [
        z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings


end_res = {}
other = {}  # (count, [poss])
for i in possible_ratings.items():
    if len(i[1]) == 1:
        end_res[i[0]] = (i[1][0], i[1][0])
    else:
        other[i[0]] = (subtotals.count(i[0]), i[1])

combs = {}
for i in other.items():
    combs[i[0]] = (i[1][0], [])  # (count, [data])
    for a in i[1][1]:
        for b in i[1][1]:
            if (a, b) not in combs[i[0]]:
                if a * b == i[0]:
                    combs[i[0]][1].append((a, b))

lists = []
for i in combs.items():
    for j in range(i[1][0]):
        lists.append([])
        for n in i[1][1]:
            lists[-1].append((n[0], n[1], i[0]))

toprocess = itertools.product(*lists)
working = []

for test in toprocess:
    works = True
    seen = []
    tot = 0
    for i in test:
        if i[1] in seen:
            works = False
            break
        tot += i[0]
        if tot > VOTES:
            works = False
            break
        seen.append(i[1])
    if not works:
        continue
    else:
        working.append(test)

formattedWs = []
for w in working:
    w = list(w)
    notseen = [i + 1 for i in range(NUM_RATINGS)]
    for i in w:
        notseen.remove(i[1])
    for i in notseen:
        w.append((0, i))

    t = ""
    def f(x, y):
        return y[1] - x[1]

    w.sort(cmp=f)

    for i in w:
        t += "%d*%d + " % (i[0], i[1])
    t = t[:-3]
    formattedWs.append(t)

seen = []
for w in list(formattedWs):
    if w in seen:
        formattedWs.remove(w)
    else:
        seen.append(w)

print "==================================="
for n, w in enumerate(formattedWs):
    print "Solution #%d: %s" % (n + 1, w)