Skip to content

Commit c63a02b

Browse files
author
Jakub Jirous
authored
Merge pull request #1 from jakubjirous/feature/plus-minus
1) Plus Minus
2 parents 74a34ef + 949bab4 commit c63a02b

File tree

7 files changed

+162
-20
lines changed

7 files changed

+162
-20
lines changed

.eslintrc.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"es2021": true
5-
},
6-
"extends": "standard-with-typescript",
7-
"overrides": [
8-
],
9-
"parserOptions": {
10-
"ecmaVersion": "latest",
11-
"sourceType": "module"
12-
},
13-
"rules": {
14-
}
2+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint"],
5+
"root": true
156
}

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@ Developers compete by writing programs according to provided specifications.
66

77
---
88

9+
<p align="center">
10+
<img src="https://sonarcloud.io/images/project_badges/sonarcloud-white.svg" alt="Sonar Cloud">
11+
</p>
12+
13+
<p align="center">
14+
<img src="https://sonarcloud.io/api/project_badges/measure?jakubjirous_hacker-rank-code-challenge&metric=coverage" alt="Coverage">
15+
<img src="https://sonarcloud.io/api/project_badges/measure?jakubjirous_hacker-rank-code-challenge&metric=code_smells" alt="Code Smells">
16+
<img src="https://sonarcloud.io/api/project_badges/measure?jakubjirous_hacker-rank-code-challenge&metric=ncloc" alt="Lines of Code">
17+
</p>
18+
19+
---
20+
21+
## Challenges in TypeScript
22+
23+
### Preparation Kit
24+
25+
1. [Plus Minus](/src/challenges/01-plus-minus/)
26+

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"homepage": "https://github.com/jakubjirous/hacker-rank-code-challenge#readme",
2020
"scripts": {
2121
"build": "tsc",
22+
"lint": "eslint . --ext .ts",
2223
"test": "jest",
2324
"test:w": "jest --watch",
2425
"test:wa": "jest --watchAll",
@@ -63,13 +64,15 @@
6364
},
6465
"devDependencies": {
6566
"@types/jest": "^29.2.4",
66-
"@typescript-eslint/eslint-plugin": "^5.0.0",
67-
"eslint": "^8.0.1",
67+
"@typescript-eslint/eslint-plugin": "^5.48.0",
68+
"@typescript-eslint/parser": "^5.48.0",
69+
"eslint": "^8.31.0",
70+
"eslint-config-google": "^0.14.0",
6871
"eslint-config-standard-with-typescript": "^24.0.0",
6972
"eslint-plugin-import": "^2.25.2",
7073
"eslint-plugin-n": "^15.0.0",
7174
"eslint-plugin-promise": "^6.0.0",
7275
"ts-jest": "^29.0.3",
73-
"typescript": "*"
76+
"typescript": "^4.9.4"
7477
}
7578
}

sonar-project.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sonar.projectKey=jakubjirous_hacker-rank-code-challenge
2+
sonar.organization=jakubjirous
3+
4+
# This is the name and version displayed in the SonarCloud UI.
5+
sonar.projectName=edabit-code-challenge
6+
sonar.projectVersion=1.0
7+
8+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
9+
sonar.sources=src/
10+
11+
# Encoding of the source code. Default is default system encoding
12+
#sonar.sourceEncoding=UTF-8
13+
14+
sonar.coverage.exclusions=src/**/test/**/*
15+
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero.
3+
* Print the decimal value of each fraction on a new line with 6 places after the decimal.
4+
*
5+
* Note:
6+
* - This challenge introduces precision problems.
7+
* - The test cases are scaled to six decimal places, though answers with absolute error of up to 10^-4 are acceptable.
8+
*
9+
* Output Format:
10+
* - Print the following lines, each to decimals:
11+
* - 1) proportion of positive values
12+
* - 2) proportion of negative values
13+
* - 3) proportion of zeros
14+
*
15+
* Example:
16+
* - arr = [1, 1, 0, -1, -1]
17+
* - There are n = 5 elements, two positive, two negative and one zero.
18+
* - Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000.
19+
* - Results are printed as:
20+
* - 0.400000
21+
* - 0.400000
22+
* - 0.200000
23+
*
24+
* @param inputArray
25+
*/
26+
export const plusMinus = (inputArray: number[]): string => {
27+
return inputArray
28+
.reduce(
29+
(acc, value) => {
30+
let [positives, negatives, zeros] = acc;
31+
32+
return [
33+
value > 0 ? ++positives : positives,
34+
value < 0 ? ++negatives : negatives,
35+
value === 0 ? ++zeros : zeros,
36+
];
37+
},
38+
[0, 0, 0]
39+
)
40+
.reduce(
41+
(acc, value) => acc + `${(value / inputArray.length).toFixed(6)}\n`,
42+
""
43+
);
44+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { plusMinus } from "../plus-minus";
2+
3+
type FirstArg = number[];
4+
type ExpectedResult = string;
5+
type Cases = [FirstArg, ExpectedResult][];
6+
7+
/**
8+
*
9+
*/
10+
describe("Plus Minus", () => {
11+
const cases: Cases = [
12+
[[0, 0, -1, 1, 1], "0.400000\n0.200000\n0.400000"],
13+
[[-4, 3, -9, 0, 4, 1], "0.500000\n0.333333\n0.166667"],
14+
[[1, -2, -7, 9, 1, -8, -5], "0.428571\n0.571429\n0.000000"],
15+
[[0, 4, -3, 3, -6], "0.400000\n0.400000\n0.200000"],
16+
[
17+
[
18+
55, 48, 48, 45, 91, 97, 45, 1, 39, 54, 36, 6, 19, 35, 66, 36, 72, 93,
19+
38, 21, 65, 70, 36, 63, 39, 76, 82, 26, 67, 29, 24, 82, 62, 53, 1, 50,
20+
47, 65, 67, 19, 66, 90, 77,
21+
],
22+
"1.000000\n0.000000\n0.000000",
23+
],
24+
[[0, 100, 35, 0, 94, 40, 42, 87, 59, 0], "0.700000\n0.000000\n0.300000"],
25+
[
26+
[
27+
0, -67, -74, -38, -72, -53, 0, -13, -95, -91, -100, -59, 0, -10, -68,
28+
-71, -62, -21, 0, -42, -57, -16, -66, -23, 0, -80, -63, -68, -65, -71,
29+
0, -71, -15, -32, -26, -8, 0, -6, -51, -87, -19, -71, 0, -93, -26, -35,
30+
-56, -89, 0, -21, -74, -39, -57, -8, 0, -69, -29, -24, -99, -53, 0, -65,
31+
-42, -72, -18, -4, 0, -73, -46, -63, -78, -87,
32+
],
33+
"0.000000\n0.833333\n0.166667",
34+
],
35+
[
36+
[
37+
-92, -21, -93, -27, -45, -63, 53, 45, 0, 6, -67, 84, 96, 86, 18, 36, 53,
38+
0, 47, 88, 91, -59, 65, 62, 3, 13, 0, -49, -47, 94, -63, 65, -23, 48,
39+
-5, 0, -10, 67, -23, 19, -11, 46, 80, -83, 0, -40, 74, -63, -20, -72,
40+
98, -72, 66, 0, -58, -1, 67, -22, 8, -45, 32, -65, 0, -10, -65, -81,
41+
-36, -55, -99, -18, -82,
42+
],
43+
"0.408451\n0.492958\n0.098592",
44+
],
45+
[
46+
[
47+
-6, 1, 79, 17, 64, 94, 87, -77, 0, -26, 2, -94, 87, -81, -73, -28, 43,
48+
0, -35, 39, -37, -49, -29, 93, 64, 54, 0, -73, -58, -100, 33, -75, -47,
49+
35, -7, 0, 52, -37, -99, 58, -23, 70, -52, 18, 0, -79, -38, 45, -61, 45,
50+
51, -48, 32, 0, -44, -56, 29, -74, -1, 92, -93, 23, 0, 55, -31, 75, -43,
51+
20, 19, 58, -4, 0, 59, -80, 18, -74, 81, 63, 62, -92, 0, -23, 7, -91,
52+
22, -1, 38, -73, 79, 0, -2, 90, 80, 74, -74, -85, -48, 31, 0, -80,
53+
],
54+
"0.440000\n0.450000\n0.110000",
55+
],
56+
[[1, 2, 3, -1, -2, -3, 0, 0], "0.375000\n0.375000\n0.250000"],
57+
];
58+
59+
test.each(cases)(
60+
"for array of integers %p output should be %p",
61+
(firstArg, expectedResult) => {
62+
const result = plusMinus(firstArg);
63+
expect(result.trim()).toEqual(expectedResult.trim());
64+
}
65+
);
66+
});

yarn.lock

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@
764764
dependencies:
765765
"@types/yargs-parser" "*"
766766

767-
"@typescript-eslint/eslint-plugin@^5.0.0":
767+
"@typescript-eslint/eslint-plugin@^5.48.0":
768768
version "5.48.0"
769769
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.48.0.tgz#54f8368d080eb384a455f60c2ee044e948a8ce67"
770770
integrity sha512-SVLafp0NXpoJY7ut6VFVUU9I+YeFsDzeQwtK0WZ+xbRN3mtxJ08je+6Oi2N89qDn087COdO0u3blKZNv9VetRQ==
@@ -779,7 +779,7 @@
779779
semver "^7.3.7"
780780
tsutils "^3.21.0"
781781

782-
"@typescript-eslint/parser@^5.0.0":
782+
"@typescript-eslint/parser@^5.0.0", "@typescript-eslint/parser@^5.48.0":
783783
version "5.48.0"
784784
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.48.0.tgz#02803355b23884a83e543755349809a50b7ed9ba"
785785
integrity sha512-1mxNA8qfgxX8kBvRDIHEzrRGrKHQfQlbW6iHyfHYS0Q4X1af+S6mkLNtgCOsGVl8+/LUPrqdHMssAemkrQ01qg==
@@ -1371,6 +1371,11 @@ escape-string-regexp@^4.0.0:
13711371
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
13721372
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==
13731373

1374+
eslint-config-google@^0.14.0:
1375+
version "0.14.0"
1376+
resolved "https://registry.yarnpkg.com/eslint-config-google/-/eslint-config-google-0.14.0.tgz#4f5f8759ba6e11b424294a219dbfa18c508bcc1a"
1377+
integrity sha512-WsbX4WbjuMvTdeVL6+J3rK1RGhCTqjsFjX7UMSMgZiyxxaNLkoJENbrGExzERFeoTpGw3F3FypTiWAP9ZXzkEw==
1378+
13741379
eslint-config-standard-with-typescript@^24.0.0:
13751380
version "24.0.0"
13761381
resolved "https://registry.yarnpkg.com/eslint-config-standard-with-typescript/-/eslint-config-standard-with-typescript-24.0.0.tgz#30e83b580380fd71477bf80bcb5405e8a46f555f"
@@ -1490,7 +1495,7 @@ eslint-visitor-keys@^3.3.0:
14901495
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.3.0.tgz#f6480fa6b1f30efe2d1968aa8ac745b862469826"
14911496
integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA==
14921497

1493-
eslint@^8.0.1:
1498+
eslint@^8.31.0:
14941499
version "8.31.0"
14951500
resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.31.0.tgz#75028e77cbcff102a9feae1d718135931532d524"
14961501
integrity sha512-0tQQEVdmPZ1UtUKXjX7EMm9BlgJ08G90IhWh0PKDCb3ZLsgAOHI8fYSIzYVZej92zsgq+ft0FGsxhJ3xo2tbuA==
@@ -3225,7 +3230,7 @@ type-fest@^0.21.3:
32253230
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
32263231
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==
32273232

3228-
typescript@*:
3233+
typescript@^4.9.4:
32293234
version "4.9.4"
32303235
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78"
32313236
integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==

0 commit comments

Comments
 (0)