Python:每日一题 169
本帖最后由 冬雪雪冬 于 2018-3-24 20:58 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
这回我们来掷骰子,每轮掷3个骰子,多轮掷后,将骰子的点数记为一个列表,例如:
[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
现在要将每轮的掷骰子的情况由大到小排序,规则是,三个一样的最大,两个一样的次之,然后是都不一样的。三个一样的最大再以点数排序,两个一样的,先比较两个一样的点数,如果还相同,再比较第三个的点数,都不一样的按点数之后。
例如:(6,6,6)>(1,1,1)>(5,5,2)>(4,4,6)>(6,5,4)>(3,2,1)
最后给出按此规则排序的列表。
注:点数相同的排序次序不限,如(6,3,1)和(5,4,1)谁排在前面都行。 本帖最后由 第四时空 于 2018-3-22 21:54 编辑
修改{:10_266:}
from collections import Counter
if __name__ == '__main__':
li = [(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
li2 = sorted(li, key=lambda x: (-len(set(x)), sum(x) if len(set(x)) == len(x) else Counter(x).most_common(1)), reverse=True)
print(li2) 本帖最后由 新手·ing 于 2018-3-23 18:13 编辑
def fuck(lst):
lstd = sorted( == x == x], reverse=True)
lssd = sorted( == sorted(y) or sorted(y) == sorted(y)) and y not in lstd], key=lambda y: y if y.count(y) == 2 else y, reverse=True)
lsnn = sorted(, key=sum, reverse=True)
return lstd + lssd + lsnn 本帖最后由 塔利班 于 2018-3-22 22:01 编辑
def rank(x):
a=len(set(x))
if a==1:
return sum(x)*100
if a==2:
return sum(x)+18*(sum(x)-sum(set(x)))
if a==3:
return sum(x)
L=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), \
(5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
t=sorted(L,key=rank,reverse=True) 本帖最后由 塔利班 于 2018-3-25 19:26 编辑
塔利班 发表于 2018-3-22 21:29
又写了个又臭又长的lambda
L=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), \
(5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
s=sorted(L,key=lambda x :sum(x)+20**(3-len(set(x)))*(sum(x)-sum(set(x))),reverse=True) 本帖最后由 Chase_Kas 于 2018-3-24 22:09 编辑
加了一个反向排序的参数,刚好学到了魔法方法里面提到了异或,这里用上了美滋滋,使得反向排序异常的简单
突然想起对于列表list1,list1.reverse()不就可以反向了嘛。。被自己蠢到了23333
# 对每组点数进行评分,三个点数一样的分数+1000,两个一样的+100,全都不一样的没有加分。
# 这样各个组别的分数相差巨大,而且组内不同点数评分还有差异,这样每组点数的大小就很直观了(是不是我的脑回路比较清奇...)
def score(each):
if set(each) == 1:
return each + 1000
elifset(each) == 2:
return (each+6)**2 + (each if each == each else each) + 100
else:
return each + each + each
# 冒泡排序,引入reverse进行反向排序。
def mysorted(point_list, reverse=False):
for i in range(len(point_list) - 1):
for j in range(len(point_list) - 1 - i):
if (score(point_list) < score(point_list)) ^ reverse:
point_list, point_list = point_list, point_list
return point_list
a = [(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
print(mysorted(a))
print(mysorted(a, reverse=True)) import random
def make_points(number):
list1 = []
for i in range(number):
x = random.randint(1,6)
y = random.randint(1,6)
z = random.randint(1,6)
temp = []
temp.append(x)
temp.append(y)
temp.append(z)
temp = tuple(sorted(temp,reverse=True))
list1.append(temp)
return list1
def strong_weak(tuple1):
temp = set(tuple1)
if len(temp) == 1:
return 3
elif len(temp) == 2:
return 2
else:
return 1
def sort_list(list1):
result3 = []#盛放3个元素相同
result2 = []#盛放2个元素相同
result1 = []#盛放1个元素的
for each in list1:
if strong_weak(each) == 3:
result3.append(each)
if strong_weak(each) == 2:
temp = []
if each == each:
temp.append(each)
temp.append(each)
temp.append(each)
if each == each:
temp.append(each)
temp.append(each)
temp.append(each)
if each == each:
temp.append(each)
temp.append(each)
temp.append(each)
each = tuple(temp)
result2.append(each)
if strong_weak(each) == 1:
result1.append(each)
result3 = sorted(result3,reverse=True)
result2 = sorted(result2,reverse=True)
result1 = sorted(result1,reverse=True)
result = []
result.extend(result3)
result.extend(result2)
result.extend(result1)
return result
if __name__ == '__main__':
number = int(input('请输入要实验多少组:'))
list1 = make_points(number)
print(sort_list(list1))
运行结果:
请输入要实验多少组:30
[(5, 5, 5), (6, 6, 4), (6, 6, 4), (6, 6, 3), (6, 6, 3), (6, 6, 1), (6, 6, 1), (5, 5, 4), (5, 5, 3), (5, 5, 2), (5, 5, 1), (4, 4, 6), (4, 4, 2), (4, 4, 2), (3, 3, 6), (2, 2, 6), (2, 2, 5), (2, 2, 5), (2, 2, 3), (1, 1, 5), (6, 4, 2), (6, 4, 1), (6, 4, 1), (6, 3, 2), (5, 4, 3), (5, 4, 2), (5, 4, 2), (5, 4, 1), (5, 3, 2), (4, 2, 1)]
***Repl Closed***
import random
def sort_dices(die):
die_score = []
for each in die:
if each == each == each:
die_score.append(100 * each)
elif each == each:
die_score.append(each * 15 + each)
elif each == each:
die_score.append(each * 15 + each)
elif each == each:
die_score.append(each * 15 + each)
else:
die_score.append(each + each + each)
sortdie_score = sorted(die_score, reverse=True)
final_list = []
for k in range(len(die)):
final_list.append(die)])
return final_list
dices = []
for i in range(20):
dic1 = []
for j in range(3):
dic1.append(random.randint(1, 6))
dices.append(tuple(dic1))
print dices
print sort_dices(dices)
=======
每个骰子元组计算排序的权重分数
三个一样的数字,重复数字权重100
两个一样的数字,重复数字权重 15
都不一样,直接算和
最后按权重排序
#定义每轮掷骰子的三个结果组成的元组的权值,比如(6,6,6)权值为3,(4,4,6)权值为2,(6,5,4)为1
def weight(t):
if t == t and t == t:
return 3
elif t.count(t) == 2 or t.count(t) == 2:
return 2
else:
return 1
#比较两次掷骰子的结果组成的元组的排序顺序,t1排前面返回True,t2排前面返回False
def comp(t1,t2):
if weight(t1) > weight(t2):
return True
elif weight(t1) < weight(t2):
return False
else:
#若权值相等,则按权值分情况进行比较
if weight(t1) == 3:
if t1 > t2:
return True
else:
return False
elif weight(t1) == 2:
#如t1为(4,4,6),则list1为;t2为(1,3,1),则list2为
list1 = []
if t1 == t1:
list1.append(t1)
list1.append(t1)
elif t1 == t1:
list1.append(t1)
list1.append(t1)
else:
list1.append(t1)
list1.append(t1)
list2 = []
if t2 == t2:
list2.append(t2)
list2.append(t2)
elif t2 == t2:
list2.append(t2)
list2.append(t2)
else:
list2.append(t2)
list2.append(t2)
if list1 > list2:
return True
elif list1 == list2:
if list1 > list2:
return True
else:
return False
else:
return False
else:
sum1 = t1 + t1 + t1
sum2 = t2 + t2 + t2
if sum1 > sum2:
return True
else:
return False
#冒泡排序
def my_sort(old):
length =len(old)
for i in range(length-1):
for j in range(length-2,-1,-1):
if not comp(old,old):
t = old
old = old
old = t
import random as r
count = int(input('请输入要投掷骰子的轮数:'))
my_list = []
for i in range(count):
list1 = []
for j in range(3):
list1.append(r.randint(1,6))
tuple1 = tuple(list1)
my_list.append(tuple1)
print('投掷'+ str(count) + '轮骰子的结果为:\n',my_list)
my_sort(my_list)
print('排序后的结果为:\n',my_list) list1 = [(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
def px(list1):
df = []
for i in range(len(list1)):
num = 0
if list1 == list1 and list1 == list1:
num += 36
elif list1 == list1 or list1 == list1:
num += 18
for j in range(3):
num += list1
df.append(num)
#print(df)
#print(len(list1))
print(max1(df,list1))
def max1(list1,list2):
rpx = []
for j in range(len(list1)):
max2 = 0
max_num = 0
for i in range(len(list1)):
if list1 > max2:
max2 = list1
max_num = i
list1 = 0
rpx.append(list2)
return rpx
px(list1)
jilu=[]
for i in dianshu :
if i==i==i :
i=(3,i)+i
elif (i!=i)and(i!=i)and(i!=i) :
i=(1,i+i+i)+i
elif (i==i) :
i=(2,i*10+i)+i
elif (i==i) :
i=(2,i*10+i)+i
elif (i==i) :
i=(2,i*10+i)+i
jilu.append(i)
jilu.sort(reverse=True)
for i in range(len(jilu)):
jilu=jilu
print(jilu) {:9_241:} 本帖最后由 天圆突破 于 2018-3-23 09:50 编辑
from random import randint
def dct(n):
lstf, lst3, lst2, lst = list(), list(), list(), list()
for i in range(n):
a = list(str(randint(1,6)) for i in range(3))
lstf.append(tuple(int(i) for i in a))
b = sorted(a, reverse = True)
if b == b == b:
lst3.append(''.join(b))
elif b == b:
lst2.append(''.join(b))
elif b == b:
lst2.append(''.join(reversed(b)))
else:
lst.append(''.join(b))
lst.sort(reverse=True)
lst2.sort(reverse=True)
lst3.sort(reverse=True)
return lstf, list(tuple((int(j) for j in list(i))) for i in lst3) + list(tuple((int(j) for j in list(i))) for i in lst2) + list(tuple((int(j) for j in list(i))) for i in lst)
if __name__ == '__main__':
print('掷骰后的结果是:\t%s\n排序后的列表是:\t%s'%(dct(11))) import random
list = []
k=0
while k <10:
list1 = []
for num in range(3):
i = random.randint(1,6)
list1.append(i)
print(list1)
list.append(list1) #list1是单一元素看待
k+=1
print(list)
list2=[]
list3=[]
list4=[]
for i in range(10):
if list==list==list:
list2.append(list)
list2.sort(reverse=True)
elif list==list or list==list or list==list:
while not list==list:
list.append(list)
list.pop(0)
list3.append(list)
list3.sort(reverse=True)
elif list!=list:
list4.append(list)
j= 1
list5=[]
len = len(list4)
while j < len:
for k in range(len-j):
if sum(list4)> sum(list4):
list4.append(list4)
list4.pop(1)
else:
list4.append(list4)
list4.pop(0)
list5.append(list4.pop(0))
j+=1
list2.extend(list3)
list2.extend(list5)
list2.extend(list4)
print(list2)
import random
def List_sz(n):
#n组骰子
list_sz = []
while n > 0:
n -= 1
list1 =
list_sz.append(list1)
return list_sz
def Rank(list_sz):
list1 = []
list2 = []
list3 = []
list3_1 = []
list3_2 = []
list3_3 = []
list_rank = []
#把数组分为三类
for each in list_sz:
if each == each == each:
list1.append(each)
elif each == each:
list2.append(each)
elif each == each:
each,each = each,each
list2.append(each)
elif each == each:
each,each = each,each
list2.append(each)
else:
list3.append(each)
list1.sort(reverse = True)
print('list1',list1)
list2.sort(reverse = True)
print('list2',list2)
print('list3',list3)
for each in list3:
list3_1.append(sum(each)) #收集list3每组的sum和每组zip打包
#print(list3_1)
list3_2 = list(zip(list3_1,list3))
list3_2.sort(reverse = True) #根据list3的sum从大到小排序
#print(list3_2)
for each in list3_2:
list3_3.append(each)
list_rank = list1 + list2 + list3_3
return '最终排序',list_rank
print(Rank(List_sz(20))) import numpy as np
li=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1), (4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2),
(1, 5, 2), (5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)]
list1=[]
list2=[]
list3=[]
for i in li:
if i==i==i:
list1.append(i)
if i==i!=ior i==i!=i:
list2.append(i)
if i==i!=i:
i=i
i=i
list2.append(i)
if i!=i!=i:
list3.append(i)
for i in range (len(list2)-1):
for j in range (len(list2)-1):
if list2<list2:
a= list2
list2=list2
list2=a
for i in range (len(list3)-1):
for j in range (len(list3)-1):
if list3+list3+list3 <list3+list3+list3:
a= list3
list3=list3
list3=a
list1.sort(reverse=True)
li=list1+list2+list3
print(li)
import random as rm
import operator as op
n = eval(input(('请输入掷色子的次数:')))
list1,list2,list3,list4,list5=[],[],[],[],[]
for i in range(1,n+1):
list1.append((rm.randint(1,6),rm.randint(1,6),rm.randint(1,6)))
for t in list1:
if t==t==t:
list2.append(t)
elif t==t or t==t or t==t:
list3.append(t)
else:
list4.append(t)
list5 = sorted(list2,key=op.itemgetter(0),reverse=True) + sorted(list3, key=lambda t:(sorted(list(t)),sum(t)),reverse=True) + sorted(list4, key=lambda t: sum(t), reverse=True)
print(list5) arr=[(4, 1, 1), (2, 2, 2), (5, 6, 1), (4, 3, 6), (2, 4, 3), (1, 1, 1),
(4, 3, 6), (6, 3, 3), (1, 3, 6), (4, 4, 3), (6, 2, 2), (1, 5, 2),
(5, 6, 6), (1, 4, 6), (2, 1, 6), (1, 3, 3), (6, 6, 5)
]
brr=[] #记录每个元组的权重
for dice in arr:
sdice=sorted(dice) #对每轮三个点数排序,便于比较
if sdice==sdice: #三个一样大
s=sdice*100
elif sdice==sdice: #两个一样大a,a,b
s=sdice*10+sdice
elif sdice==sdice: #两个一样大a,b,b
s=sdice*10+sdice
else: #都不一样
s=sum(dice)
brr.append(s)
for i in range(0,len(brr)-1): #权重排序,同步排序源数据
for j in range(i+1,len(brr)):
if brr>brr:
brr,brr=brr,brr
arr,arr=arr,arr
print(arr) num= input('请输入掷骰子的次数:')
num = int(num)
list1 = []
import random
n=0
while n<num:
i = random.randint(1,6)
j = random.randint(1,6)
k = random.randint(1,6)
tup = (i,j,k)
list1.append(tup)
n +=1
print('原掷骰子数据:',list1)
print('--------------------------------------------------------')
#三个一样
listsame = []
#两个一样
listtwosame = []
#都不一样的
listnosame = []
for each in list1:
if max(each) == min(each):
listsame.append(each)
elif( each != each and each!=each and each!= each):
listnosame.append(each)
else:
listtwosame.append(each)
listresult = []
#分别对listsame listtwosame listnosame 进行排序
listsame = sorted(listsame,reverse=True)
#对listnosame 进行排序
for each in listnosame:
for i in range(len(listnosame)-1):
for j in range(len(listnosame)-i-1):
iflistnosame+listnosame+listnosame< listnosame+listnosame+listnosame :
listnosame, listnosame = listnosame, listnosame
#对listtwosame 进行排序
def compare(tup):
same =0
nosame =0
if(tup==tup):
same = tup
nosame = tup
elif(tup==tup):
same = tup
nosame = tup
else:
same = tup
nosame = tup
tup2 = (same,nosame)
return tup2
for each in listtwosame:
for i in range(len(listtwosame)-1): # 这个循环负责设置冒泡排序进行的次数
for j in range(len(listtwosame)-i-1):# j为列表下标
if compare(listtwosame) < compare(listtwosame):
listtwosame, listtwosame = listtwosame, listtwosame
elif compare(listtwosame) == compare(listtwosame):
if compare(listtwosame) < compare(listtwosame):
listtwosame, listtwosame = listtwosame, listtwosame
iflistsame:
listresult.extend(listsame)
iflisttwosame:
listresult.extend(listtwosame)
iflistnosame:
listresult.extend(listnosame)
print('排序后数据:',listresult)
请输入掷骰子的次数:12
原掷骰子数据: [(6, 2, 5), (2, 5, 6), (2, 2, 5), (3, 2, 4), (3, 6, 3), (5, 5, 4), (6, 3, 1), (2, 6, 2), (2, 1, 1), (1, 3, 3), (6, 1, 6), (3, 4, 2)]
--------------------------------------------------------
排序后数据: [(6, 1, 6), (5, 5, 4), (3, 6, 3), (1, 3, 3), (2, 6, 2), (2, 2, 5), (2, 1, 1), (6, 2, 5), (2, 5, 6), (6, 3, 1), (3, 2, 4), (3, 4, 2)] 本帖最后由 graceasyi 于 2018-3-27 15:33 编辑
写得太复杂了,唉,脑细胞严重不够用!
import random
from operator import itemgetter, attrgetter
# 产生一组掷骰子的数据,掷骰子的次数由输入参数n决定
def dice(n):
return ) for j in range(n)]
def compare(data):
dl =
for s in dl:
s.sort(reverse=True)
t1, t2, t3 = [], [], []
for d in dl:
if len(d)-len(set(d)) == 2:
t1.append(d)
elif len(d)-len(set(d)) == 1:
t2.append(d)
else:
t3.append(d)
if len(t1) > 1:
t1.sort(reverse=True)
if len(t2) > 1:
t2.sort(key=itemgetter(1, 0, 2), reverse=True)
if len(t3) > 1:
for i in range(len(t3)):
for j in range(i):
if sum(t3) > sum(t3):
t3.insert(j, t3.pop(i))
break
return
print(compare(dice(20)))