【Kata Daily 190910】Who likes it?(谁点了赞?)
题目:
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?(谁点了赞?)的更多相关文章
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【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 ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【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 ...
- 【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 ...
- 【Kata Daily 190920】Square(n) Sum(平方加总)
题目: Complete the square sum function so that it squares each number passed into it and then sums the ...
- 【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 ...
- 【Kata Daily 190918】Spacify(插空)
题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...
随机推荐
- Java知识系统回顾整理01基础03变量08表达式
一.以 ; 结尾的一段代码,即为一个表达式 表达式是由变量.操作符以及方法调用所构成的结构.如下所示: int i = 5; System.out.println(5); 都是表达式 public c ...
- excel——VlookUp函数的使用
VlookUp函数,查询两个表中的相同字段数据,并将需要引用的数据从B表填充到A表 1.打开A表,将需要查询的列选中 在需要引用的列输入 = 在上方,函数选择中选择VLOOKUP函数 Windows: ...
- 2. 在TCGA中找到并下载意向数据
听说过别人用生信分析"空手套白狼"的故事吧想做吗好想学哦~ 或多或少都知道GEO和TCGA这些公共数据库吧!那么你知道怎么在数据库上找到意向数据,并且成功下载呢?这第一步要难倒一大 ...
- 微服务通信之feign集成负载均衡
前言 书接上文,feign接口是如何注册到容器想必已然清楚,现在我们着重关心一个问题,feign调用服务的时候是如何抉择的?上一篇主要是从读源码的角度入手,后续将会逐步从软件构架方面进行剖析. 一.R ...
- 用 Java 实现的八种常用排序算法
八种排序算法可以按照如图分类 交换排序 所谓交换,就是序列中任意两个元素进行比较,根据比较结果来交换各自在序列中的位置,以此达到排序的目的. 1. 冒泡排序 冒泡排序是一种简单的交换排序算法,以升序排 ...
- python的PEP8代码规范
一.缩进:每级缩进用4个空格.如果缩进不正确或缩进格式不统一,一般错误信息会明确告诉你,但有时也会出现invalid syntax报错.所谓缩进不正确,python的缩进是四个空格或一个TAB,如果缩 ...
- 身为电气人,为什么也要学习C语言编程?人生苦短,我学编程!
说起编程大家可能都听过,但编程究竟是怎么一回事你弄懂了吗? 编程=对计算机程序进行编写,这些程序可以是现在手里拿着的手机.办公的电脑.你点击的页面.浏览的网页,都是有程序让它执行你要它做的事情. PL ...
- 我是先学C语言还是先学C++,实不相瞒,鱼和熊掌可兼得!
这是最近一周时间几个读者小伙伴所提的问题,我顺手截了两个图. 实不相瞒,这类问题之前也经常看到. 每次遇到这种问题,看起来很简单,但是打字一时半会还真说不清,想想今天周末了,写一篇文章来统一聊 ...
- idea创建servlet工程初体验
servlet工程创建 前提:创建项目之前需要配置java环境变量 和tomcat配置,配置完成后进入如下操作. tomcat 安装和配置参考 https://www.cnblogs.com/xush ...
- golang xpath解析网页
https://github.com/antchfx/htmlquery package main import ( "fmt" "github.com/antchfx/ ...