-
-
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.
Merge pull request #455 from sir-gon/feature/ctci-comparator-sorting
[Hacker Rank] Interview Preparation Kit: Sorting: Comparator. Solved ✅.
- Loading branch information
Showing
6 changed files
with
297 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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: | ||
|
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. |
22 changes: 22 additions & 0 deletions
22
src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.Player.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,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 }; |
42 changes: 42 additions & 0 deletions
42
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,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(); | ||
}); | ||
}); | ||
}); |
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" | ||
} | ||
] |
53 changes: 53 additions & 0 deletions
53
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,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 }; |