Files
freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/rosetta-code/top-rank-per-group.md

2.6 KiB
Raw Blame History

id, title, challengeType, videoUrl
id title challengeType videoUrl
595011cba5a81735713873bd 每组排名最高 5

--description--

任务:

在每个组中查找排名前N位的数据其中N作为参数提供。等级和组的名称也作为参数提供。

鉴于以下数据:

 [
{姓名:'Tyler Bennett'id'E10297'薪水32000部门'D101'}
{姓名:'John Rappl'id'E21437'薪水47000部门'D050'}
{姓名:'George Woltman'id'E00127'薪水53500部门'D101'}
{name'Adam Smith'id'E63535'薪水18000部门'D202'}
{姓名:'Claire Buckman'id'E39876'薪水27800部门'D202'}
{姓名:'David McClellan'id'E04242'薪水41500部门'D101'}
{name'Rich Holcomb'id'E01234'薪水49500dept'D202'}
{姓名:'Nathan Adams'id'E41298'薪水21900部门'D050'}
{姓名:'Richard Potter'id'E43128'薪水15900部门'D101'}
{姓名:'David Motsinger'id'E27002'薪水19250dept'D202'}
{姓名:'Tim Sampair'id'E03033'薪水27000部门'D101'}
{姓名:'Kim Arlich'id'E10001'薪水57000部门'D190'}
{name'Timothy Grove'id'E16398'薪水29900部门'D190'}
]。

通过调用topRankPerGroup(10, data, 'dept', 'salary')可以对每个部门的前10名员工进行排名。给出以下数据

 [
{name'Friday 13th',类型:'恐怖'评分9.9}
{姓名:“榆树街上的梦魇”,类型:'恐怖'等级5.7}
{name'Titanic',类型:'drama'评分7.3}
{name'Maze Runner',类型:'scifi'评级7.1}
{name'Blade runner',类型:'scifi'评分8.9}
]。
通过调用topRankPerGroup(1, data, 'genre', 'rating')可以在每个类型中对排名最高的电影进行排名

--hints--

topRankPerGroup是一个函数。

assert(typeof topRankPerGroup === 'function');

topRankPerGroup在负n值上返回undefined。

assert(typeof topRankPerGroup(-1, []) === 'undefined');

第一部门必须是D050

assert.equal(res1[0][0].dept, 'D050');

第一部门必须是D050

assert.equal(res1[0][1].salary, 21900);

最后一个部门必须是D202

assert.equal(res1[3][3].dept, 'D202');

topRankPerGroup(1, ...)必须仅返回每组的排名最高的结果。

assert.equal(res2[2].length, 1);

topRankPerGroup(1, ...)必须仅返回每组的排名最高的结果。

assert.equal(res3[2][1].name, 'Maze Runner');

--solutions--