Skip to content

[Hacker Rank] Interview Preparation Kit: Sorting: Comparator. Solved ✅. #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ jobs:

- name: Install ESLint
run: |
npm install [email protected]
npm install @microsoft/[email protected]

npm install --include=dev [email protected]
npm install --include=dev @microsoft/[email protected]
- name: Test ESLint
run: |
npx --yes eslint --env-info
- name: Run ESLint
run: npx eslint .
run: >
npx eslint .
--config .eslintrc
--max-warnings=0
--ext .js,.jsx,.ts,.tsx
--format @microsoft/eslint-formatter-sarif
--output-file eslint-results.sarif
continue-on-error: true

- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# [Sorting: Comparator](https://www.hackerrank.com/challenges/ctci-comparator-sorting)

Write a Comparator for sorting elements in an array.

- Difficulty: `#medium`
- Category: `#ProblemSolvingBasic` `#sorting`

Comparators are used to compare two objects.
In this challenge, you'll create a comparator and use it to sort an array.
The Player class is provided in the editor below. It has two fields:

1. `name`: a string.
2. `score`: an integer.

Given an array of `n` Player objects,
write a comparator that sorts them in order of decreasing score.

If `2` or more players have the same score,
sort those players alphabetically ascending by name.
To do this, you must create a Checker class that
implements the Comparator interface,
then write an int compare(Player a, Player b) method implementing the
[Comparator.compare(T o1, T o2)](https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html#compare(T,%20T))
method.
In short, when sorting in ascending order,
a comparator function returns `-1` if `a < b`,
`0` if `a = b`, and `1` if `a > b`.

Declare a Checker class that implements the comparator method as described.

It should sort first descending by score,
then ascending by name.
The code stub reads the input,
creates a list of Player objects,
uses your method to sort the data, and prints it out properly.

## Example

`n = 3`
`data = [[Smith, 20], [Jones, 15], [Jones, 20]]`

Sort the list as $ data_{sorted} $ = `[Jones, 20], [[Smith, 20], [Jones, 15]]`.
Sort first descending by score, then ascending by name.

## Input Format

The first line contains an integer, `n`, the number of players.
Each of the next `n` lines contains a player's `name` and `score`,
a string and an integer.

## Constraints

- $ 0 \leq score \leq 1000 $
- Two or more players can have the same name.
- Player names consist of lowercase English alphabetic letters.

## Output Format

You are not responsible for printing any output to stdout.
Locked stub code in Solution will instantiate a Checker object,
use it to sort the Player array, and print each sorted element.

## Sample Input

```text
5
amy 100
david 100
heraldo 50
aakansha 75
aleksa 150
```

## Sample Output

```text
aleksa 150
amy 100
david 100
aakansha 75
heraldo 50
```

## Explanation

The players are first sorted descending by score, then ascending by name.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Start Given code

export class Player {
name = '';

score = 0;

toString(): string {
// Given code
this.name.toString();
return '';
}

comparator(bPlayer: this): number {
// Given code
return 0 * this.score * bPlayer.score;
}
}

// End Given code

export default { Player };
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from '@jest/globals';

import { Player } from './ctci_comparator_sorting.Player';

import {
SortablePlayer,
comparatorSorting,
comparatorSortingPrint
} from './ctci_comparator_sorting';
import TEST_CASES from './ctci_comparator_sorting.testcases.json';

describe('countSwaps', () => {
it('test_player', () => {
expect.assertions(2);

const aPlayer = new Player();
const aPlayerAsString = aPlayer.toString();
const aExpected = '';

expect(aExpected).toStrictEqual(aPlayerAsString);

const bPlayer = new Player();
const comparatorAnswerExpected = 0;

expect(aPlayer.comparator(bPlayer)).toStrictEqual(comparatorAnswerExpected);
});

it('test_comparator_sorting', () => {
expect.assertions(8);

TEST_CASES.forEach((test) => {
const players: SortablePlayer[] = [];

for (const player of test.input) {
players.push(new SortablePlayer(player.name, player.score));
}

expect(comparatorSorting(players)).toStrictEqual(test.sorted);
expect(comparatorSortingPrint(players)).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[
{
"title": "Sample Test Case 0",
"input": [
{
"name": "amy",
"score": 100
},
{
"name": "david",
"score": 100
},
{
"name": "heraldo",
"score": 50
},
{
"name": "aakansha",
"score": 75
},
{
"name": "aleksa",
"score": 150
}
],
"sorted": ["aleksa 150", "amy 100", "david 100", "aakansha 75", "heraldo 50"],
"expected": "aleksa 150\namy 100\ndavid 100\naakansha 75\nheraldo 50"
},
{
"title": "Sample Test Case 6",
"input": [
{
"name": "smith",
"score": 20
},
{
"name": "jones",
"score": 15
},
{
"name": "jones",
"score": 20
}
],
"sorted": ["jones 20", "smith 20", "jones 15"],
"expected": "jones 20\nsmith 20\njones 15"
},
{
"title": "Sample Test Case 7",
"input": [
{
"name": "davis",
"score": 15
},
{
"name": "davis",
"score": 20
},
{
"name": "davis",
"score": 10
},
{
"name": "edgehill",
"score": 15
}
],
"sorted": ["davis 20", "davis 15", "edgehill 15", "davis 10"],
"expected": "davis 20\ndavis 15\nedgehill 15\ndavis 10"
},
{
"title": "Edge case: draw",
"input": [
{
"name": "kurt",
"score": 100
},
{
"name": "kurt",
"score": 100
}
],
"sorted": ["kurt 100", "kurt 100"],
"expected": "kurt 100\nkurt 100"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md]]
*/

import { Player } from './ctci_comparator_sorting.Player';

export class SortablePlayer extends Player {
name = '';

score = 0;

constructor(name: string, score: number) {
super();

this.name = name;
this.score = score;
}

toString(): string {
return `${this.name} ${this.score}`;
}

comparator(bPlayer: this): number {
if (this.score > bPlayer.score) {
return -1;
}
if (this.score < bPlayer.score) {
return 1;
}
if (this.name < bPlayer.name) {
return -1;
}
if (this.name > bPlayer.name) {
return 1;
}

return 0;
}
}

export function comparatorSorting(players: SortablePlayer[]): string[] {
const sortedPlayers = [...players].sort(
(a: SortablePlayer, b: SortablePlayer): number => a.comparator(b)
);

return sortedPlayers.map((player: SortablePlayer) => player.toString());
}

export function comparatorSortingPrint(players: SortablePlayer[]): void {
console.log(comparatorSorting(players)?.join('\n'));
}

export default { Player, SortablePlayer, comparatorSorting };