Skip to content

Commit bb07f15

Browse files
author
Jakub Jirous
authored
Merge pull request #4 from jakubjirous/feature/mini-max-sum
2) Mini-Max Sum
2 parents 3e9462f + 6b961ae commit bb07f15

File tree

6 files changed

+102
-11
lines changed

6 files changed

+102
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Developers compete by writing programs according to provided specifications.
2323
### Preparation Kit
2424

2525
1. [Plus Minus](/src/challenges/01-plus-minus/)
26+
2. [Mini-Max Sum](/src/challenges/02-mini-max-sum/)

src/challenges/01-plus-minus/plus-minus.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* - 3) proportion of zeros
1414
*
1515
* Example:
16-
* - arr = [1, 1, 0, -1, -1]
16+
* - inputArray = [1, 1, 0, -1, -1]
1717
* - There are n = 5 elements, two positive, two negative and one zero.
1818
* - Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000.
1919
* - Results are printed as:
@@ -23,7 +23,10 @@
2323
*
2424
* @param inputArray
2525
*/
26-
export const plusMinus = (inputArray: number[]): string => {
26+
export type Input = number[];
27+
export type Output = string;
28+
29+
export const plusMinus = (inputArray: Input): Output => {
2730
return inputArray
2831
.reduce(
2932
(acc, value) => {

src/challenges/01-plus-minus/test/plus-minus.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import { plusMinus } from "../plus-minus";
1+
import { Input, Output, plusMinus } from "../plus-minus";
22

3-
type FirstArg = number[];
4-
type ExpectedResult = string;
5-
type Cases = [FirstArg, ExpectedResult][];
3+
type Cases = [Input, Output][];
64

7-
/**
8-
*
9-
*/
105
describe("Plus Minus", () => {
116
const cases: Cases = [
127
[[0, 0, -1, 1, 1], "0.400000\n0.200000\n0.400000"],
@@ -57,7 +52,7 @@ describe("Plus Minus", () => {
5752
];
5853

5954
test.each(cases)(
60-
"for array of integers %p output should be %p",
55+
"for given array of integers %p output should be %p",
6156
(firstArg, expectedResult) => {
6257
const result = plusMinus(firstArg);
6358
expect(result.trim()).toEqual(expectedResult.trim());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then
3+
* print the respective minimum and maximum values as a single line of two space-separated long integers.
4+
*
5+
* Explanation:
6+
* - The numbers are 1, 2, 3, 4, and 5.
7+
* - Calculate the following sums using four of the five integers:
8+
* - 1) Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14.
9+
* - 2) Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13.
10+
* - 3) Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12.
11+
* - 4) Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11.
12+
* - 5) Sum everything except 5, the sum is 1 + 2 + 3 + 4 = 10.
13+
*
14+
* Output Format:
15+
* - Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers.
16+
* - The output can be greater than a 32 bit integer.
17+
*
18+
* Example:
19+
* - inputArray = [1, 3, 5, 7, 9]
20+
* - The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24.
21+
* - The function prints:
22+
* - 16 24
23+
*
24+
* @param inputArray
25+
*/
26+
27+
export type Input = number[];
28+
export type Output = [number, number];
29+
30+
export const miniMaxSum = (inputArray: Input): Output => {
31+
const min = Math.min(...inputArray);
32+
const max = Math.max(...inputArray);
33+
const sum = inputArray.reduce((acc, value) => acc + value);
34+
35+
return [sum - max, sum - min];
36+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Input, miniMaxSum, Output } from "../mini-max-sum";
2+
3+
type Cases = [Input, Output][];
4+
5+
describe("Mini-Max Sum", () => {
6+
const cases: Cases = [
7+
[
8+
[1, 2, 3, 4, 5],
9+
[10, 14],
10+
],
11+
[
12+
[256741038, 623958417, 467905213, 714532089, 938071625],
13+
[2063136757, 2744467344],
14+
],
15+
[
16+
[396285104, 573261094, 759641832, 819230764, 364801279],
17+
[2093989309, 2548418794],
18+
],
19+
[
20+
[140638725, 436257910, 953274816, 734065819, 362748590],
21+
[1673711044, 2486347135],
22+
],
23+
[
24+
[769082435, 210437958, 673982045, 375809214, 380564127],
25+
[1640793344, 2199437821],
26+
],
27+
[
28+
[426980153, 354802167, 142980735, 968217435, 734892650],
29+
[1659655705, 2484892405],
30+
],
31+
[
32+
[942381765, 627450398, 954173620, 583762094, 236817490],
33+
[2390411747, 3107767877],
34+
],
35+
[
36+
[539674108, 549382170, 270968351, 746219035, 140597628],
37+
[1500622257, 2106243664],
38+
],
39+
[
40+
[254961783, 604179258, 462517083, 967304281, 860273491],
41+
[2181931615, 2894274113],
42+
],
43+
[
44+
[7, 69, 2, 221, 8974],
45+
[299, 9271],
46+
],
47+
];
48+
49+
test.each(cases)(
50+
"for given array of integers %p output should be %p",
51+
(firstArg, expectedResult) => {
52+
const result = miniMaxSum(firstArg);
53+
expect(result).toEqual(expectedResult);
54+
}
55+
);
56+
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
4141
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
4242
// "resolveJsonModule": true, /* Enable importing .json files. */
43-
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should plusMinus to a project. */
43+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
4444

4545
/* JavaScript Support */
4646
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */

0 commit comments

Comments
 (0)