-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank] Interview Preparation Kit: Sorting: Comparator. Solved ✅.
- Loading branch information
Gonzalo Diaz
committed
Aug 27, 2024
1 parent
93d2d3d
commit be11e08
Showing
4 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
41 changes: 41 additions & 0 deletions
41
src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { | ||
Player, | ||
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('David', 100); | ||
const aPlayerAsString = aPlayer.toString(); | ||
const aExpected = ''; | ||
|
||
expect(aExpected).toStrictEqual(aPlayerAsString); | ||
|
||
const bPlayer = new Player('Kurt', 10); | ||
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(); | ||
}); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
75 changes: 75 additions & 0 deletions
75
src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable no-useless-constructor */ | ||
/* eslint-disable max-classes-per-file */ | ||
/** | ||
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md]] | ||
*/ | ||
|
||
// Start Given code | ||
|
||
export class Player { | ||
name = ''; | ||
|
||
score = 0; | ||
|
||
toString(): string { | ||
Check failure Code scanning / ESLint Enforce that class methods utilize `this` Error
Expected 'this' to be used by class method 'toString'.
|
||
// Given code | ||
return ''; | ||
} | ||
|
||
comparator(bPlayer: this): number { | ||
Check failure Code scanning / ESLint Enforce that class methods utilize `this` Error
Expected 'this' to be used by class method 'comparator'.
|
||
// Given code | ||
return 0 * bPlayer.score; | ||
} | ||
} | ||
|
||
// End Given code | ||
|
||
export class SortablePlayer extends Player { | ||
name = ''; | ||
|
||
score = 0; | ||
|
||
constructor(name: string, score: number) { | ||
super(name, score); | ||
|
||
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, comparatorSorting }; |