Menu
Coddy logo textTech

Recap - Leaderboard

Part of the Logic & Flow section of Coddy's R journey. Lesson 41 of 64.

challenge icon

Challenge

Medium

Complete leaderboard(names, points). Each entry records the points one player scored in one round, and a player can appear many times. Return one line per player, 1. ana 15 (2 rounds), ranked by total points from high to low. Players with the same total are ranked alphabetically.

Then add Best round: followed by the player and the points of the single highest entry, such as cy 12. When several entries share the highest points, use the first of them.

The supplied code reads the rounds as name:points pairs separated by commas and prints each returned line.

Try it yourself

leaderboard <- function(names, points) {
  # Write your code here
  character(0)
}

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
pairs <- strsplit(strsplit(input[1], ",")[[1]], ":")
names <- sapply(pairs, function(p) p[1])
values <- as.numeric(sapply(pairs, function(p) p[2]))
cat(leaderboard(names, values), sep = "\n")

All lessons in Logic & Flow

Practice on your own: Online R compiler