Python练习题答案: 处罚超速【难度:3级】

阅读: 评论:0

Python练习题答案: 处罚超速【难度:3级】

Python练习题答案: 处罚超速【难度:3级】

处罚超速【难度:3级】:

答案1:

# return str of the smallest value of the combined numbers in a_list
# the length of a_list can vary betweem 2 and 20  
def penalty(a_list):return ''.join(sorted(a_list, cmp=(lambda a, b: cmp(a+b, b+a))))
​

答案2:

def penalty(lst):lst, maxLen = list(map(str, lst)), max(map(len, lst))return ''.join(sorted(lst, key=lambda s: s.ljust(maxLen, s[-1])))​

答案3:

# return str of the smallest value of the combined numbers in a_list
def penalty(a_list):return ''.join(sorted(a_list, key = lambda n: n + n[:1]))​

答案4:

penalty=lambda a:''.join(sorted(a,key=lambda n:2*n))​

答案5:

from functools import cmp_to_keydef penalty(a_list):return ''.join(sorted(a_list, key = cmp_to_key(sorter)))def sorter(a, b):return -1 if a + b < b + a else 1    ​

答案6:

# return str of the smallest value of the combined numbers in a_list
# the length of a_list can vary betweem 2 and 20  
def penalty(l):return "".join(sorted(l, key=lambda k: (k*2, -len(k))))​

答案7:

# return str of the smallest value of the combined numbers in a_list
# the length of a_list can vary betweem 2 and 20  
def penalty(i):a = sorted(i, key=lambda k:(k*1000, -len(k)))return "".join(a)​

答案8:

# return str of the smallest value of the combined numbers in a_list
# the length of a_list can vary betweem 2 and 20     def penalty(a_list):return ''.join(sorted(a_list, cmp=lambda x,y: int(x+y) - int(y+x) ))​

答案9:

def penalty(a):m = len(max(a, key=len))return "".join(x[:len(x)//m] for x in sorted(x * m for x in a))​

答案10:

from functools import cmp_to_keydef cmp (a, b):
# should be a > b if a[0] > b[0]
# if a[0] == b[0], should be a > b if a[1] a = list(a)b = list(b)if a == b:return 0if int(a[0]) < int(b[0]):return -1elif int(a[0]) > int(b[0]):return 1else:  #a[0] == b[0]if len(a) > 1 and len(b) > 1:return cmp(a[1:], b[1:])elif len(a) > 1 and len(b) == 1:return (-1 if int(a[1]) < int(b[0]) else 1)elif len(a) == 1 and len(b) > 1:return (-1 if int(a[0]) < int(b[1]) else 1)def penalty(a_list):print(a_list) return ''.join(sorted(a_list,key = cmp_to_key(cmp)))​



景越Python基础训练营QQ群


欢迎各位同学加群讨论,一起学习,共同成长!

本文发布于:2024-02-03 04:30:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170690579948657.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:练习题   难度   答案   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23