DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #295 - Sort Leaderboards

In this challenge, you'll be given a leaderboard in the form of an array, as well as a list of strings. Using the information in the list, sort the leaderboard.

Example

array:

['John',
 'Brian',
 'Jim',
 'Dave',
 'Fred']

list:
['Dave +1', 'Fred +4', 'Brian -1']

The steps for our example would be:

# Dave up 1
['John',
 'Brian',
 'Dave',
 'Jim',
 'Fred']
# Fred up 4
['Fred',
 'John',
 'Brian',
 'Dave',
 'Jim']
# Brian down 1
['Fred',
 'John',
 'Dave',
 'Brian',
 'Jim']

Then, we return the completed leaderboard:

['Fred',
 'John',
 'Dave',
 'Brian',
 'Jim']

Strings will never ask you to move a name higher or lower than possible. The strings in the list will always be a name in the leaderboard, followed by a space and a positive or negative number.

Tests

leaderboardSort(['John', 'Brian', 'Jim', 'Dave', 'Fred'], ['Dave +1', 'Fred +4', 'Brian -1'])
leaderboardSort(['Bob', 'Larry', 'Kevin', 'Jack', 'Max'], ['Max +3', 'Kevin -1', 'Kevin +3'])

Good luck!


This challenge comes from topping on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (2)

Collapse
 
_bkeren profile image
''

JS

const leaderboardSort = (list, stepList) =>
{
    stepList.forEach(stepInfo => {
        let [name, step] = stepInfo.split(" ")
        step = +step
        const nameIndex = list.indexOf(name)
        list.splice(nameIndex,1)
        list.splice(nameIndex-step,0,name)
    })
    return list
}
Collapse
 
caleb_rudder profile image
Caleb Rudder • Edited

Javascript!

function leaderboardSort(leaderBoard, moveList){
    moveList.forEach(element => {
        let moveInfo = element.split(' ');
        let player = moveInfo[0];
        let moveAmmount = moveInfo[1];
        let currentIndex = leaderBoard.indexOf(player);
        let newIndex = currentIndex - moveAmmount;

        if(newIndex < currentIndex){
            for(let i = currentIndex; i > newIndex; i--){
                leaderBoard[i] = leaderBoard[i-1];
            }
            leaderBoard[newIndex] = player;
        }else{
            for(let i = currentIndex; i < newIndex; i++){
                //move everything up one
                leaderBoard[i] = leaderBoard[i+1];
            }
            leaderBoard[newIndex] = player;
        }

    });
    return leaderBoard;
}

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!