练习题答案: 代表民主【难度:2级】"/>
Python练习题答案: 代表民主【难度:2级】
代表民主【难度:2级】:
答案1:
def representation(zone_pop, rep_req):total_rep = sum(zone_pop)zones = [round(z * rep_req / total_rep) or 1 for z in zone_pop]total = sum(zones)while total != rep_req:el, inc = (min(zones), 1) if total < rep_req else (max(zones), -1)i = zones.index(el)zones[i] += inctotal += increturn zones
答案2:
def representation(zone,rep):s = sum(zone)t = [ max(x/s*rep, 1) for x in zone ]tr = [ round(x) for x in t ]delta, mi, ma = rep - sum(tr), min(tr), max(tr)i = 0while delta > 0:i = next(i for i,x in enumerate(tr) if x == mi)tr[i] += 1delta -= 1while delta < 0: i = next(i for i,x in enumerate(tr) if x == ma)tr[i] -= 1delta += 1return tr
答案3:
def representation(zone_pop, rep_req):r = rep_req / sum(zone_pop)a = [max(int(round(r * p)), 1) for p in zone_pop]while sum(a) > rep_req:a[a.index(max(a))] -= 1while sum(a) < rep_req:a[a.index(min(a))] += 1return a
答案4:
from operator import truediv
def representation(zone_pop,rep_req):total=sum(zone_pop)if len(zone_pop)==rep_req:return [1]*rep_reqr=[]for zone in zone_pop:num = truediv(zone, total) * rep_reqr.append(round(num) if num % 1 > 0.5 else int(num) or 1)while sum(r)<rep_req:mn=min(r)r[r.index(mn)]+=1while sum(r)>rep_req:mx=max(r)r[r.index(mx)]-=1return r
答案5:
def representation(zone_pop, rep_req):tot_pop = sum(zone_pop)zone_pop = [round(x*rep_req/tot_pop) or 1 for x in zone_pop]while sum(zone_pop) < rep_req:zone_pop[zone_pop.index(min(zone_pop))] += 1while sum(zone_pop) > rep_req:zone_pop[zone_pop.index(max(zone_pop))] -= 1return zone_pop
答案6:
def representation(zone_pop, rep_req):tot_pop = sum(zone_pop)zone_pop = [round(x*rep_req/tot_pop) for x in zone_pop]zone_pop = [x if x>0 else 1 for x in zone_pop]while sum(zone_pop) < rep_req:zone_pop[zone_pop.index(min(zone_pop))] += 1while sum(zone_pop) > rep_req:zone_pop[zone_pop.index(max(zone_pop))] -= 1return zone_pop
答案7:
# from operator import truediv # Python 2def representation(zone_pop, rep_req):rep_total = 0result = []population_total = sum(zone_pop)for population in zone_pop:# rep = truediv(population, population_total) * rep_req # Python 2rep = (population / population_total) * rep_reqcurrent = round(rep) or 1# current = round(rep) if rep % 1 > 0.5 else int(rep) or 1 # Python 2rep_total += currentresult.append(current)diff = rep_total - rep_req# for _ in xrange(abs(int(diff))): # Python 2for _ in range(abs(int(diff))):if diff < 0:result[result.index(min(result))] += 1diff += 1else:result[result.index(max(result))] -= 1diff -= 1return result
答案8:
def representation(z,r):tot = sum(z)res = [ max(1,round(x*r/tot)) for x in z] while sum(res)!=r:while sum(res)<r:res[res.index(min(res))]+=1while sum(res)>r:res[res.index(max(res))]-=1 return res
答案9:
def representation(zone_pop, rep_req):total_pop = sum(zone_pop)reps = [x if x > 0 else 1 for x in [round(zone * rep_req / total_pop) for zone in zone_pop]]while sum(reps) < rep_req:reps[reps.index(min(reps))] += 1while sum(reps) > rep_req:reps[reps.index(max(reps))] -= 1return reps
答案10:
def level(values, identify, action):"""Use identify to find a target value in values and then apply the actionto the first instance of the target value"""target = identify(values)for i, v in enumerate(values):if v == target:values[i] = action(values[i])breakdef representation(zone_pop, rep_req):# Calculate constiutents each representative will representtot = sum(zone_pop)constituents = float(tot) / rep_reqif constituents <= 1:# Degenerate case - everyone is a representative for themselvesreturn zone_popelse:# Do a best rounded allocationresult = [int(round(zone / constituents)) or 1 for zone in zone_pop]# Correct for under-commitmentwhile sum(result) < rep_req:level(result, min, lambda x: x+1)# Correct for over-commitmentwhile sum(result) > rep_req:level(result, max, lambda x: x-1)return result
景越Python基础训练营QQ群
欢迎各位同学加群讨论,一起学习,共同成长!
更多推荐
Python练习题答案: 代表民主【难度:2级】
发布评论