ooxx7788 发表于 2017-6-12 16:16:55

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 *****

SixPy 发表于 2017-6-12 17:15:02

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))

ooxx7788 发表于 2017-6-12 17:24:28

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哦,你这样返回的可不对哦。

SixPy 发表于 2017-6-12 17:43:04

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

新手·ing 发表于 2017-6-12 18:18:58

我以后一定return{:10_245:}

jerryxjr1220 发表于 2017-6-12 21:07:01

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

ooxx7788 发表于 2017-6-12 23:22:42

jerryxjr1220 发表于 2017-6-12 21:07
no one likes this
Peter likes this
Jacob and Alex like this


我觉得你和six可能是把我说的优雅理解的复杂了。
我虽然写不出我答案提供的那种形式,但是我会选择用字典来做出结果。

jerryxjr1220 发表于 2017-6-12 23:38:56

ooxx7788 发表于 2017-6-12 23:22
我觉得你和six可能是把我说的优雅理解的复杂了。
我虽然写不出我答案提供的那种形式,但是我会选择用字 ...

其实最直观还是用if来写,反正一共也就4种情况,脑子都不用动。。。

冬雪雪冬 发表于 2017-6-13 00:00:42

还是你的答案好。
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)

sunnychou 发表于 2017-6-13 09:37:29

{:5_91:}

gopythoner 发表于 2017-6-13 13:00:36

我选择用IF,IF语句不优雅么?
反正条件不多的时候IF是最优选择,我是这么认为的

瓦蓝 发表于 2017-6-14 19:30:02

学习一下

三万天 发表于 2017-6-16 09:10:15

{:5_91:}

solomonxian 发表于 2017-7-28 18:53:59

默默支持
不太明白优雅指的是怎样的,唯有看答案了

我觉得直接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

ooxx7788 发表于 2017-7-28 19:05:20

solomonxian 发表于 2017-7-28 18:53
默默支持
不太明白优雅指的是怎样的,唯有看答案了



其实我就是这么一说啦,毕竟if大家都会写,希望有一些不一样的答案而已。

shigure_takimi 发表于 2017-12-5 17:13:24

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"]))

新手潘包邮 发表于 2018-4-17 23:36:34

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"])

holiday_python 发表于 2020-6-21 10:47:52

使用*args

19971023 发表于 2020-7-19 17:33:06

1

小陨aoq 发表于 2020-7-30 16:15:35

康康答案
页: [1] 2
查看完整版本: Python: 每日一题 64