Python: 每日一题 64
本帖最后由 ooxx7788 于 2017-6-12 23:14 编辑今天的题目简单,但是我希望可以有优雅的答案。
给出一个likes函数,根据里面的人民数返回对应的语句。
likes [] // return "no one likes this"
likes ["Peter"] // retrun "Peter likes this"
likes ["Jacob", "Alex"] // return "Jacob and Alex like this"
likes ["Max", "John", "Mark"] //return "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // return "Alex, Jacob and 2 others like this"
顺便说个建议,我出的题目中间有些是偏向基础的题目,所以我经常会看一些新手朋友的答案。这些新手朋友做的时候喜欢用print来输出答案。
但是我个人建议,最好以return的形式来返回值。除了专门以输出为目的的函数,对于绝大多数时候,我们使用函数的目的还是为了获得一个值而不是仅仅在屏幕上面打印一次。
虽然从print到return中间并不是特别困难,但是对于刚入门的人来说,从print到return往往会出现问题。而你不能掌握return等于就没有真正的掌握定义函数。也可以观察一下我题目中各位大佬的答案,他们基本上都是以return的形式来返回结果的。
唉,今天这么简单的题目依然是看得多,回的少!看来这系列可以收了。
**** Hidden Message ***** def likes(ls):
fn=(lambda x:"no one",
lambda x:x,
lambda x:' and '.join(x))
if len(ls)<3 :
return "%s likes this" % (fn(ls))
a,b,*c = ls
return "%s, %s and %s like this" % (a,b,(cif len(c)==1 else '%s others'%len(c)))
ls=[[],
["Peter"],
["Jacob", "Alex"],
["Max", "John", "Mark"],
["Alex", "Jacob", "Mark", "Max"]]
for l in ls:
print(likes(l))
SixPy 发表于 2017-6-12 17:15
你代码的运行结果。
no one likes this
Peter likes this
Jacob and Alex likes this
Max, John and Mark like this
Alex, Jacob and 2 others like this
两个人的时候,也是like哦,你这样返回的可不对哦。 ooxx7788 发表于 2017-6-12 17:24
你代码的运行结果。
两个人的时候,也是like哦,你这样返回的可不对哦。
酱紫?
def likes(ls):
fn=(lambda x:"no one",
lambda x:x,
lambda x:' and '.join(x))
if len(ls)<3 :
ret = fn(ls)
else:
a,b,*c = ls
ret = "%s, %s and %s" % (a,b,(cif len(c)==1 else '%s others'%len(c)))
return "%s like%s this" % (ret,('','s'))
ls=[[],
["Peter"],
["Jacob", "Alex"],
["Max", "John", "Mark"],
["Alex", "Jacob", "Mark", "Max"]]
for l in ls:
print(likes(l))
no one likes this
Peter likes this
Jacob and Alex like this
Max, John and Mark like this
Alex, Jacob and 2 others like this 我以后一定return{:10_245:} def likes(L):
if len(L) == 0: return 'no one likes this'
one = 1 if len(L) == 1 else 0
two_three = 1 if 2 <=len(L) <= 3 else 0
more = 1 if len(L) > 3 else 0
return L*one + (', '.join(L[:-1])+' and '+L[-1])*two_three + (', '.join(L[:2])+' and '+str(len(L)-2)+' others')*more + ' like'+'s'*one + ' this'
long_L = [[],
['Peter'],
['Jacob', 'Alex'],
['Max', 'John', 'Mark'],
["Alex", "Jacob", "Mark", "Max"]]
for L in long_L:
print(likes(L))
no one likes this
Peter likes this
Jacob and Alex like this
Max, John and Mark like this
Alex, Jacob and 2 others like this jerryxjr1220 发表于 2017-6-12 21:07
no one likes this
Peter likes this
Jacob and Alex like this
我觉得你和six可能是把我说的优雅理解的复杂了。
我虽然写不出我答案提供的那种形式,但是我会选择用字典来做出结果。 ooxx7788 发表于 2017-6-12 23:22
我觉得你和six可能是把我说的优雅理解的复杂了。
我虽然写不出我答案提供的那种形式,但是我会选择用字 ...
其实最直观还是用if来写,反正一共也就4种情况,脑子都不用动。。。 还是你的答案好。
def likes(lst):
if len(lst) == 0:
return "no one likes this"
if len(lst) == 1:
return "%s likes this"%lst
if len(lst) == 2:
return "%s and %s like this"%(tuple(lst[:2]))
if len(lst) == 3:
return "%s, %s and %s like this"%(tuple(lst[:3]))
return "%s, %s and %d others like this"%(lst, lst, len(lst) - 2)
{:5_91:} 我选择用IF,IF语句不优雅么?
反正条件不多的时候IF是最优选择,我是这么认为的 学习一下
{:5_91:} 默默支持
不太明白优雅指的是怎样的,唯有看答案了
我觉得直接if写下来,思路和结构都很清晰,就这题来说也不麻烦
# 先用最直接踏实的方法
def likes(a):
if len(a)==0:
return "no one likes this"
elif len(a)==1:
return "%s likes this"%a
elif len(a)==2:
return "%s and %s like this"%(a,a)
elif len(a)==3:
return "%s, %s and %s like this" %(a,a,a)
else:
return "%s, %s and %d others like this"%(a,a,len(a)-2)
然后我想了一下,如果不用 if 那就用 try了
# 用try故意引发错误
def likes2(a):
try:
d = "no one likes this"
d = "%s likes this"%a
d = "%s and %s like this"%(a,a)
d = "%s, %s and %s like this" %(a,a,a)
d = a
return"%s, %s and %d others like this"%(a,a,len(a)-2)
except IndexError:
return d solomonxian 发表于 2017-7-28 18:53
默默支持
不太明白优雅指的是怎样的,唯有看答案了
其实我就是这么一说啦,毕竟if大家都会写,希望有一些不一样的答案而已。 def likes(nameList):
if nameList == []:
return "no one likes this"
elif len(nameList) == 1:
return '{} likes this'.format(nameList)
elif len(nameList) == 2:
return '{0} and {1} likes this'.format(nameList, nameList)
elif len(nameList) == 3:
return '{0}, {1} and {2} likes this'.format(nameList, nameList, nameList)
else:
return '{0}, {1} and {2} others like this'.format(nameList, nameList, len(nameList)-2)
print(likes([]))
print(likes(["Peter"]))
print(likes(["Jacob", "Alex"]))
print(likes(["Max", "John", "Mark"]))
print(likes(["Alex", "Jacob", "Mark", "Max"]))
def likes(string):
string_len = len(string)
if string_len == 0:
string_something = "no one"
elif string_len == 1:
string_something = "".join(string)
elif string_len == 2:
string_something = " and ".join(string)
elif string_len == 3:
string_something = ", ".join(string[:2]) + ", and " + string
else:
string_something = ", ".join(string[:2]) + ", and " + str(string_len - 2) + " others"
return string_something + " likes this"
likes(["Max", "Peter","haha", "zhazha","jajaj"]) 使用*args 1 康康答案
页:
[1]
2