题目:

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. 【代码审计】PHP代码审计---基础记录

    PHP伪协议 PHP伪协议事实上是其支持的协议与封装协议,支持的种类有以下12种. * file:// - 访问本地文件系统 * http:// - 访问 HTTP(s) 网址 * ftp:// - ...

  2. Matlab中加汉明窗 ahmming 作用

    转自:http://www.cnblogs.com/lxy2017/p/4049124.html 1.什么是汉明窗? 语音信号一般在10ms到30ms之间,我们可以把它看成是平稳的.为了处理语音信号, ...

  3. Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'

    查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...

  4. JavaScript利用函数反转数组

    要求: 给定一数组,将其元素倒序排列并输出. 代码实现: // 利用函数翻转任意数组 reverse 翻转 function reverse(arr) { var newArr = []; for ( ...

  5. 简说Modbus-RTU与Modbus-ASCII

    Modbus在串行总线通信中的协议有RTU和ASCII两种.RTU是Remote Terminal Unit的缩写,意思是远程终端单元.ASCII是American Standard Code for ...

  6. Linux执行脚本让进程挂掉后自动重启

    1 创建循环监听脚本  autostart.sh 例: 其中futures-market-server-v3andwebsoket.jar 是要监听的执行程序 #/bin/bashwhile true ...

  7. 【纯水题】CF 833A The Meaningless Game

    题目大意 洛谷链接 现在两个人做游戏,每个人刚开始都是数字\(1\),谁赢了就能乘以\(k^2\),输的乘以\(k\),现在给你最终这两个人的得分,让你判断是否有这个可能,有可能的话输出Yes,否则输 ...

  8. centos8安装fastdfs6.06集群方式二之:tracker的安装/配置/运行

    一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...

  9. cmd/powershell常用命令 git常用命令

    cmd/powershell: 1. 新建文件夹: mkdir directoryName 2. 新建文件: cmd: type nul>fileName (空文件) powershell: n ...

  10. GeoServer发布shapfile字段名和值乱码问题解决

    摘要: 网上说了一大堆方法又是转格式咯又是改源代码了,修改很简单: 修改Styles下的你的style: Xml代码 修改Stores下你的图层的属性,设置 DBF charset为GBK 以上设置G ...