6kyu Who likes it

题目:

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 more than 4 names, the number in and 2 others simply increases.

答案:

    // 1

function likes (names) {

names = names || [];

switch(names.length) {

case 0: return ‘no one likes this‘; break;

case 1: return names[0] + ‘ likes this‘; break;

case 2: return names[0] + ‘ and ‘ + names[1] + ‘ like this‘; break;

case 3: return names[0] + ‘, ‘ + names[1] + ‘ and ‘ + names[2] + ‘ like this‘; break;

default: return names[0] + ‘,‘ + names[1] + ‘ and ‘ + (names.length - 2) + ‘ others like this‘;

}

}

// 2

function likes (names) {

var templates = [

‘no one likes this‘,

‘{name} likes this‘,

‘{name} and {name} like this‘,

‘{name}, {name} and {name} like this‘,

‘{name}, {name} and {n} others like this‘

];

var idx = Math.min(names.length, 4);

return templates[idx].replace(/{name}|{n}/g/, function (val) {

return val === ‘{name}‘ ? names.shift() : names.length;

});

}

时间: 2024-10-22 15:28:04

6kyu Who likes it的相关文章

GTW likes gt(BC 模拟 or 优先队列)

GTW likes gt Accepts: 54 Submissions: 782 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 从前,有nnn只萌萌的GT,他们分成了两组在一起玩游戏.他们会排列成一排,第iii只GT会随机得到一个能力值bib_ib?i??.在第iii秒的时候,第iii只GT可以消灭掉所有排在他前面的和他不是同一组的且能力值小于他的GT.为了使游戏更加有趣,G

GTW likes math(简单数学)

GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description After attending the class given by Jin Longyu, who is a specially-graded teacher of Mathematics, GTW

GTW likes math(BC 1001)

GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学<从自主招生到竞赛>.然而书里的题目太多了,GTW还有很多事情要忙(比如把妹),于是他把那些题目交给了你.每一道题目会给你一个函数f(x)=ax2+bx+cf(x)=求这个函

hdu-5597 GTW likes function(欧拉函数+找规律)

题目链接: GTW likes function Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Description Now you are given two definitions as follows. f(x)=∑xk=0(−1)k22x−2kCk2x−k+1,f0(x)=f(x),fn(x)=f(fn−1(x))(n≥1) Note that

HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到

GTW likes math Memory Limit: 131072/131072 K (Java/Others) 问题描述 某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学<从自主招生到竞赛>.然而书里的题目太多了,GTW还有很多事情要忙(比如把妹),于是他把那些题目交给了你.每一道题目会给你一个函数f(x)=ax^2+bx+cf(x)=ax?2??+bx+c,求这个函数在整数区间[l,r][l,r]之间的最值. 输入描述 第一行一个整数T,表示数据组数.(T\leq 1000T≤

HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数

GTW likes function Memory Limit: 131072/131072 K (Java/Others) 问题描述 现在给出下列两个定义: f(x)=f_{0}(x)=\sum_{k=0}^{x}(-1)^{k}2^{2x-2k}C_{2x-k+1}^{k},f_{n}(x)=f(f_{n-1}(x))(n\geq 1)f(x)=f?0??(x)=∑?k=0?x??(−1)?k??2?2x−2k??C?2x−k+1?k??,f?n??(x)=f(f?n−1??(x))(n≥1

System design interview: how to design comments and reply, likes button and total views on Youtube

System design interview: how to design comments and reply, likes button and total views on Youtube Methodology: READ MF! [Originally from the Post: System design interview: how to design a chat system (e.g., Facebook Messenger, WeChat or WhatsApp)] R

Codeforces Round #631 (Div. 2) D.Dreamoon Likes Sequences

题目连接:Dreamoon Likes Sequences  题意:给你d和m,让你构造一个递增数组a,使数组b(i==1,b[i]=a[i] ; i>1, b[i]=b[i-1]^a[i])递增,求a有几种,答案模m. 题解:根据异或的性质可以得出:2后边不能有3, 4后边不能有5~7, 8后边不能有9~15...... 然后就很好写了.用数组b记录第i个数可以取得数有多少个,数组dp记录长度为 i 的 a 数组有几种.看下边的代码应该就清楚了. 1 #include<bits/stdc++

Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring(贪心好题/意识流题解)

Dreamoon likes coloring cells very much. There is a row of nn cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 11 to nn . You are given an integer mm and mm integers l1,l2,…,lml1,l2,…,lm (1≤li≤n1≤li≤n ) Dreamoo