鱼C论坛

 找回密码
 立即注册
查看: 3849|回复: 20

[技术交流] Python: 每日一题 64

[复制链接]
发表于 2017-6-12 16:16:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ooxx7788 于 2017-6-12 23:14 编辑

今天的题目简单,但是我希望可以有优雅的答案。

给出一个likes函数,根据里面的人民数返回对应的语句。

  1. likes [] // return "no one likes this"
  2. likes ["Peter"] // retrun "Peter likes this"
  3. likes ["Jacob", "Alex"] // return "Jacob and Alex like this"
  4. likes ["Max", "John", "Mark"] //return "Max, John and Mark like this"
  5. likes ["Alex", "Jacob", "Mark", "Max"] // return "Alex, Jacob and 2 others like this"
复制代码


顺便说个建议,我出的题目中间有些是偏向基础的题目,所以我经常会看一些新手朋友的答案。这些新手朋友做的时候喜欢用print来输出答案。

但是我个人建议,最好以return的形式来返回值。除了专门以输出为目的的函数,对于绝大多数时候,我们使用函数的目的还是为了获得一个值而不是仅仅在屏幕上面打印一次。

虽然从print到return中间并不是特别困难,但是对于刚入门的人来说,从print到return往往会出现问题。而你不能掌握return等于就没有真正的掌握定义函数。也可以观察一下我题目中各位大佬的答案,他们基本上都是以return的形式来返回结果的。


唉,今天这么简单的题目依然是看得多,回的少!看来这系列可以收了。
游客,如果您要查看本帖隐藏内容请回复

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-12 17:15:02 | 显示全部楼层
  1. def likes(ls):
  2.     fn=(lambda x:"no one",
  3.         lambda x:x[0],
  4.         lambda x:' and '.join(x))
  5.     if len(ls)<3 :
  6.         return "%s likes this" % (fn[len(ls)](ls))

  7.     a,b,*c = ls
  8.     return "%s, %s and %s like this" % (a,b,(c[0]if len(c)==1 else '%s others'%len(c)))


  9. ls=[[],
  10.     ["Peter"],
  11.     ["Jacob", "Alex"],
  12.     ["Max", "John", "Mark"],
  13.     ["Alex", "Jacob", "Mark", "Max"]]

  14. for l in ls:
  15.     print(likes(l))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-12 17:24:28 | 显示全部楼层


你代码的运行结果。
  1. no one likes this
  2. Peter likes this
  3. Jacob and Alex likes this
  4. Max, John and Mark like this
  5. Alex, Jacob and 2 others like this
复制代码

两个人的时候,也是like哦,你这样返回的可不对哦。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-12 17:43:04 | 显示全部楼层
ooxx7788 发表于 2017-6-12 17:24
你代码的运行结果。

两个人的时候,也是like哦,你这样返回的可不对哦。

酱紫?
  1. def likes(ls):
  2.     fn=(lambda x:"no one",
  3.         lambda x:x[0],
  4.         lambda x:' and '.join(x))
  5.     if len(ls)<3 :
  6.         ret = fn[len(ls)](ls)
  7.     else:
  8.         a,b,*c = ls
  9.         ret = "%s, %s and %s" % (a,b,(c[0]if len(c)==1 else '%s others'%len(c)))
  10.     return "%s like%s this" % (ret,('','s')[len(ls)<2])


  11. ls=[[],
  12.     ["Peter"],
  13.     ["Jacob", "Alex"],
  14.     ["Max", "John", "Mark"],
  15.     ["Alex", "Jacob", "Mark", "Max"]]

  16. for l in ls:
  17.     print(likes(l))
复制代码

  1. no one likes this
  2. Peter likes this
  3. Jacob and Alex like this
  4. Max, John and Mark like this
  5. Alex, Jacob and 2 others like this
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-6-12 18:18:58 | 显示全部楼层
我以后一定return
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-12 21:07:01 | 显示全部楼层
  1. def likes(L):
  2.     if len(L) == 0: return 'no one likes this'
  3.     one = 1 if len(L) == 1 else 0
  4.     two_three = 1 if 2 <=len(L) <= 3 else 0
  5.     more = 1 if len(L) > 3 else 0
  6.     return L[0]*one + (', '.join(L[:-1])+' and '+L[-1])*two_three + (', '.join(L[:2])+' and '+str(len(L)-2)+' others')*more + ' like'+'s'*one + ' this'

  7. long_L = [[],
  8.         ['Peter'],
  9.         ['Jacob', 'Alex'],
  10.         ['Max', 'John', 'Mark'],
  11.         ["Alex", "Jacob", "Mark", "Max"]]

  12. for L in long_L:
  13.     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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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可能是把我说的优雅理解的复杂了。
我虽然写不出我答案提供的那种形式,但是我会选择用字典来做出结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

其实最直观还是用if来写,反正一共也就4种情况,脑子都不用动。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-13 00:00:42 | 显示全部楼层
还是你的答案好。
  1. def likes(lst):
  2.     if len(lst) == 0:
  3.         return "no one likes this"
  4.     if len(lst) == 1:
  5.         return "%s likes this"%lst[0]
  6.     if len(lst) == 2:
  7.         return "%s and %s like this"%(tuple(lst[:2]))
  8.     if len(lst) == 3:
  9.         return "%s, %s and %s like this"%(tuple(lst[:3]))
  10.     return "%s, %s and %d others like this"%(lst[0], lst[1], len(lst) - 2)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-13 09:37:29 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-13 13:00:36 | 显示全部楼层
我选择用IF,IF语句不优雅么?
反正条件不多的时候IF是最优选择,我是这么认为的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-14 19:30:02 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-16 09:10:15 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-28 18:53:59 | 显示全部楼层
默默支持
不太明白优雅指的是怎样的,唯有看答案了

我觉得直接if写下来,思路和结构都很清晰,就这题来说也不麻烦
  1. # 先用最直接踏实的方法
  2. def likes(a):
  3.     if len(a)==0:
  4.         return "no one likes this"
  5.     elif len(a)==1:
  6.         return "%s likes this"%a[0]
  7.     elif len(a)==2:
  8.         return "%s and %s like this"%(a[0],a[1])
  9.     elif len(a)==3:
  10.         return "%s, %s and %s like this" %(a[0],a[1],a[2])
  11.     else:
  12.         return "%s, %s and %d others like this"%(a[0],a[1],len(a)-2)
复制代码

然后我想了一下,如果不用 if 那就用 try了
  1. # 用try故意引发错误
  2. def likes2(a):
  3.     try:
  4.         d = "no one likes this"
  5.         d = "%s likes this"%a[0]
  6.         d = "%s and %s like this"%(a[0],a[1])
  7.         d = "%s, %s and %s like this" %(a[0],a[1],a[2])
  8.         d = a[3]
  9.         return  "%s, %s and %d others like this"%(a[0],a[1],len(a)-2)
  10.     except IndexError:
  11.         return d
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-28 19:05:20 | 显示全部楼层
solomonxian 发表于 2017-7-28 18:53
默默支持
不太明白优雅指的是怎样的,唯有看答案了

其实我就是这么一说啦,毕竟if大家都会写,希望有一些不一样的答案而已。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-5 17:13:24 | 显示全部楼层
  1. def likes(nameList):
  2.     if nameList == []:
  3.         return "no one likes this"
  4.     elif len(nameList) == 1:
  5.         return '{} likes this'.format(nameList[0])
  6.     elif len(nameList) == 2:
  7.         return '{0} and {1} likes this'.format(nameList[0], nameList[1])
  8.     elif len(nameList) == 3:
  9.         return '{0}, {1} and {2} likes this'.format(nameList[0], nameList[1], nameList[2])
  10.     else:
  11.         return '{0}, {1} and {2} others like this'.format(nameList[0], nameList[1], len(nameList)-2)
  12.    
  13. print(likes([]))
  14. print(likes(["Peter"]))
  15. print(likes(["Jacob", "Alex"]))
  16. print(likes(["Max", "John", "Mark"]))
  17. print(likes(["Alex", "Jacob", "Mark", "Max"]))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[2]
    else:
        string_something = ", ".join(string[:2]) + ", and " + str(string_len - 2) + " others"
    return string_something + " likes this"
likes(["Max", "Peter","haha", "zhazha","jajaj"])
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-21 10:47:52 | 显示全部楼层
使用*args
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-19 17:33:06 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-30 16:15:35 | 显示全部楼层
康康答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 22:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表