欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python练习题答案: *分配【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

程序员文章站 2022-05-30 23:39:48
...

*分配【难度:2级】:

答案1:

def socialist_distribution(population, minimum):
    if minimum > sum(population)//len(population):
        return []
    while min(population) < minimum:
        population[population.index(min(population))] += 1
        population[population.index(max(population))] -= 1
    return population
​

答案2:

def socialist_distribution(population, minimum):
    if len(population) * minimum > sum(population):
        return []
    
    while min(population) < minimum:
        i_max = population.index(max(population))
        i_min = population.index(min(population))
        population[i_max] -= 1
        population[i_min] += 1
    
    return population​

答案3:

def socialist_distribution(population, minimum):
    if sum(population) < minimum * len(population):
        return []
    population = population[:]
    for _ in range(sum(max(minimum - p, 0) for p in population)):
        population[min(range(len(population)), key=population.__getitem__)] += 1
        population[max(range(len(population)), key=population.__getitem__)] -= 1
    return population​

答案4:

def socialist_distribution(population, minimum):
    if sum(population) < len(population) * minimum:
        return []
    while 1:
        poor = rich = 0
        for idx, need in enumerate(population):
            if need < population[poor]: poor = idx
            if need > population[rich]: rich = idx
        if population[poor] >= minimum:
            return population
        population[rich] -= 1
        population[poor] += 1

答案5:

socialist_distribution=lambda p, m: [] if sum(p)<m*len(p) else p if all(m<=e for e in p) else (lambda mini, maxi: socialist_distribution(p[:mini]+[p[mini]+1]+p[mini+1:maxi]+[p[maxi]-1]+p[maxi+1:], m) if mini<maxi else socialist_distribution(p[:maxi]+[p[maxi]-1]+p[maxi+1:mini]+[p[mini]+1]+p[mini+1:], m))(p.index(min(p)),p.index(max(p)))

答案6:

def socialist_distribution(population, minimum):
    if sum(population) < (minimum * len(population)):
        return []
    if min(population) >= minimum:
        return population
    population[population.index(max(population))] -= 1
    population[population.index(min(population))] += 1
    return socialist_distribution(population,minimum)

答案7:

def socialist_distribution(population, minimum):
    if sum(population)/len(population) < minimum: return []
    p = population[:]
    m = minimum
    while min(p) < m:
        p[p.index(min(p))] += 1
        p[p.index(max(p))] -= 1
    return p​

答案8:

def socialist_distribution(population, minimum):
    for i in range(len(population)):
        if population[i]>=minimum:
            continue
        while population[i]<minimum:
            m = max(population)
            if m <= minimum:
                return []
            population[population.index(m)] -= 1
            population[i] += 1
    return population
                ​

答案9:

def socialist_distribution(population, minimum):
    if sum(population)/len(population) < minimum:
        return []
    while min(population) < minimum:
        max_i = population.index(max(population))
        min_i = population.index(min(population))
        population[max_i] -= 1
        population[min_i] += 1
    return population​

答案10:

def socialist_distribution(population, minimum):
    if sum(population) < len(population) * minimum:
        return []
    poorest = [(i, w) for i, w in enumerate(population) if w < minimum]
    shortfall = sum(minimum - p[1] for p in poorest)
    for p in poorest:
        population[p[0]] = minimum
    for i in range(shortfall):
        maxWealth = max(population)
        population[population.index(maxWealth)] -= 1
    return population​