From 93d2d3d3de4d0ab336c78cad7bc61cb9a2e86c89 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 27 Aug 2024 19:12:17 -0400 Subject: [PATCH 1/2] [CONFIG] [Github Actions] ESLint script parameters updated. --- .github/workflows/eslint.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/eslint.yml b/.github/workflows/eslint.yml index 8ec69c09..2da84af0 100644 --- a/.github/workflows/eslint.yml +++ b/.github/workflows/eslint.yml @@ -45,17 +45,20 @@ jobs: - name: Install ESLint run: | - npm install eslint@8.56.0 - npm install @microsoft/eslint-formatter-sarif@3.0.0 - + npm install --include=dev eslint@8.57.0 + npm install --include=dev @microsoft/eslint-formatter-sarif@3.0.0 + - 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: From 43478fc0a9747e4e5759dd294c484f1a07c99f0d Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 27 Aug 2024 17:51:24 -0400 Subject: [PATCH 2/2] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation?= =?UTF-8?q?=20Kit:=20Sorting:=20Comparator.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sort/ctci-comparator-sorting.md | 86 +++++++++++++++++++ .../sort/ctci_comparator_sorting.Player.ts | 22 +++++ .../sort/ctci_comparator_sorting.test.ts | 42 +++++++++ .../ctci_comparator_sorting.testcases.json | 86 +++++++++++++++++++ .../sort/ctci_comparator_sorting.ts | 53 ++++++++++++ 5 files changed, 289 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md create mode 100644 src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.Player.ts create mode 100644 src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.test.ts create mode 100644 src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.testcases.json create mode 100644 src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.ts diff --git a/docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md b/docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md new file mode 100644 index 00000000..ac14bb86 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/sort/ctci-comparator-sorting.md @@ -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. diff --git a/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.Player.ts b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.Player.ts new file mode 100644 index 00000000..d281d2be --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.Player.ts @@ -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 }; diff --git a/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.test.ts b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.test.ts new file mode 100644 index 00000000..0bd653d6 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.test.ts @@ -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(); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.testcases.json b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.testcases.json new file mode 100644 index 00000000..795c4247 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.testcases.json @@ -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" + } +] diff --git a/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.ts b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.ts new file mode 100644 index 00000000..a13be477 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/ctci_comparator_sorting.ts @@ -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 };