题目:

Description:

You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.

Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"

For 4 or more names, the number in and 2 others simply increases.

-----------------------------------------------------------------------------------------------

题目大意就是根据names的个数,返回一段文字,说明谁点了赞。

解题办法:

def likes(names):
if len(names) == 0:
return "no one likes this"
elif len(names) == 1:
return "%s likes this" % names[0]
elif len(names) == 2:
return "%s and %s like this" % (names[0], names[1])
elif len(names) == 3:
return "%s, %s and %s like this" % (names[0], names[1], names[2])
else:
return "%s, %s and %s others like this" % (names[0], names[1], len(names)-2)

简单粗暴的方式,直接枚举出来所有的结果。

还有一种比较优秀的方式:

def likes(names):
n = len(names)
return {
0: 'no one likes this',
1: '{} likes this',
2: '{} and {} like this',
3: '{}, {} and {} like this',
4: '{}, {} and {others} others like this'
}[min(4, n)].format(*names[:3], others=n-2)

解读:通过字典的方式,再配合min函数来确定字典的key值,根据key值来找到对应的返回文字。

知识点:

1、使用星号* 可以表示打印出list中的元素

2、min()函数可以返回最小值

3、格式化的打印可以使用百分号%,或者.format()来表示。

【Kata Daily 190910】Who likes it?(谁点了赞?)的更多相关文章

  1. 【Kata Daily 190929】Password Hashes(密码哈希)

    题目: When you sign up for an account somewhere, some websites do not actually store your password in ...

  2. 【Kata Daily 191012】Find numbers which are divisible by given number

    题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...

  3. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  4. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  5. 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)

    题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...

  6. 【Kata Daily 190923】Odder Than the Rest(找出奇数)

    题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...

  7. 【Kata Daily 190920】Square(n) Sum(平方加总)

    题目: Complete the square sum function so that it squares each number passed into it and then sums the ...

  8. 【Kata Daily 190919】Sort Out The Men From Boys(排序)

    题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...

  9. 【Kata Daily 190918】Spacify(插空)

    题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...

随机推荐

  1. 浅谈Prufer序列

    \(\text{Prufer}\)序列,是树与序列的一种双射. 构建过程: 每次找到一个编号最小的叶子节点\(Leaf\),将它删掉,并将它所连接的点的度数\(-1\),且加入\(\text{Pruf ...

  2. 跟我一起学Redis之五种基本类型及其应用场景举例(干了6个小时)

    前言 来啦,老弟?来啦,上一篇就当唠唠嗑,接下来就开始进行实操撸命令,计划是先整体单纯说说Redis的各种用法和应用,最后再结合代码归纳总结. Redis默认有16个数据库(编号为0~15),默认使用 ...

  3. C#与sql进行图片存取

    1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Da ...

  4. docker容器命令2

    docker容器命令2 启动守护式容器 docker run -d 镜像名字 [root@tzh ~]# docker images REPOSITORY TAG IMAGE ID CREATED S ...

  5. SessionStorage、LocalStorage详解

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/sessionstorage-and-localst ...

  6. 微信小程序 audio组件 默认控件 无法隐藏/一直显示/改了controls=‘false’也没用2019/5/28

    <audio>默认控件,如果需要隐藏,不需要特意设置controls = 'false',直接把这个属性删除即可,不然无论如何都会存在 之前,设置了controls = 'false' & ...

  7. 租房数据分析,knn算法使用

    import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel('jiemo.x ...

  8. 如何写好 C语言 main 函数!你准备好编写 C 程序了吗?

    学习如何构造一个 C 文件并编写一个 C main 函数来成功地处理命令行参数.   我知道,现在孩子们用 Python 和 JavaScript 编写他们的疯狂"应用程序".但是 ...

  9. redis 开启AOF 持久化

    redis 开启AOF 找到redis 安装目录 打开 redis.conf  修改以下参数: appendonly  yes        (默认no,关闭)表示是否开启AOF持久化: append ...

  10. 基于Docker的MySql

    MySQL Server安装教程 考虑到实际情况需要经常使用MySQL,为了方便大家能够快速基于Docker搭建MySQL这里以Linux下为例 进行说明,对于Windows用户来说直接通过查看官网H ...