Python练习题答案: 代表民主【难度:2级】

编程入门 行业动态 更新时间:2024-10-18 12:32:57

Python<a href=https://www.elefans.com/category/jswz/34/1768594.html style=练习题答案: 代表民主【难度: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级】

本文发布于:2024-02-25 19:50:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:练习题   民主   难度   答案   代表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!