Skip to content

Commit b41ea77

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Warm up: Plus Minus solved ✓
1 parent f0f092b commit b41ea77

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ae.hackerrank;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Locale;
6+
/**
7+
* Plus Minus.
8+
*
9+
* @link Problem definition [[docs/hackerrank/warmup/plus_minus.md]]
10+
*/
11+
public class PlusMinus {
12+
13+
private PlusMinus() {
14+
}
15+
16+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
17+
18+
/**
19+
* plusMinus.
20+
*/
21+
public static String plusMinus(List<Integer> arr) {
22+
23+
int positives = 0;
24+
int negatives = 0;
25+
int zeros = 0;
26+
27+
for (int x : arr) {
28+
if (x > 0) {
29+
positives += 1;
30+
}
31+
if (x < 0) {
32+
negatives += 1;
33+
}
34+
if (x == 0) {
35+
zeros += 1;
36+
}
37+
}
38+
39+
List<String> result = new ArrayList<>();
40+
result.add(String.format(Locale.ROOT, "%.6f", (double) positives / arr.size()));
41+
result.add(String.format(Locale.ROOT, "%.6f", (double) negatives / arr.size()));
42+
result.add(String.format(Locale.ROOT, "%.6f", (double) zeros / arr.size()));
43+
44+
return String.join("\n", result);
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ae.hackerrank;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
12+
13+
@TestInstance(Lifecycle.PER_CLASS)
14+
class PlusMinusTest {
15+
16+
public class PlusMinusTestCase {
17+
public List<Integer> input;
18+
public String expected;
19+
20+
public PlusMinusTestCase(List<Integer> _input, String _expected) {
21+
this.input = _input;
22+
this.expected = _expected;
23+
}
24+
}
25+
26+
public List<PlusMinusTestCase> testCases;
27+
28+
@BeforeAll
29+
public void setup() {
30+
this.testCases = Arrays.asList(
31+
new PlusMinusTestCase(
32+
Arrays.asList(-4, 3, -9, 0, 4, 1),
33+
String.join("\n", "0.500000", "0.333333", "0.166667")
34+
)
35+
);
36+
}
37+
38+
@Test void testPlusMinus() {
39+
40+
for (PlusMinusTestCase testCase : this.testCases) {
41+
String solutionFound = PlusMinus.plusMinus(testCase.input);
42+
43+
assertEquals(testCase.expected, solutionFound,
44+
String.format("%s(%s) answer must be: %s",
45+
"PlusMinus.plusMinus",
46+
testCase.input.toString(),
47+
testCase.expected.toString())
48+
);
49+
}
50+
}
51+
}

docs/hackerrank/warmup/plus_minus.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [Plus Minus](https://www.hackerrank.com/challenges/plus-minus)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given an array of integers, calculate the ratios of its elements
7+
that are positive, negative, and zero. Print the decimal value of
8+
each fraction on a new line with 6 places after the decimal.
9+
10+
**Note**: This challenge introduces precision problems.
11+
The test cases are scaled to six decimal places, though answers
12+
with absolute error of up to $ 10^{-4} $ are acceptable.
13+
14+
## Example
15+
16+
$ arr = [1, 1, 0, -1, -1] $
17+
18+
There are $ n = 5 $ elements, two positive, two negative and one zero.
19+
Their ratios are $ 2/5 = 0.400000 $, $ 2/5 = 0.400000 $ and $ 1/5 = 0.200000 $.
20+
Results are printed as:
21+
22+
```text
23+
0.400000
24+
0.400000
25+
0.200000
26+
```
27+
28+
## Function Description
29+
30+
Complete the plusMinus function in the editor below.
31+
plusMinus has the following parameter(s):
32+
33+
- int arr[n]: an array of integers
34+
35+
## Print
36+
37+
Print the ratios of positive, negative and zero values in the array.
38+
Each value should be printed on a separate line with $ 6 $ digits after
39+
the decimal. The function should not return a value.
40+
41+
## Input Format
42+
43+
The first line contains an integer, `n`, the size of the array.
44+
The second line contains `n` space-separated integers that describe `arr[n]`.
45+
46+
## Constraints
47+
48+
$ 0 < n \leq 100 $ \
49+
$ -100 \leq arr[i] \leq 100 $
50+
51+
## Output Format
52+
53+
**Print** the following lines, each to decimals:
54+
55+
1. proportion of positive values
56+
2. proportion of negative values
57+
3. proportion of zeros
58+
59+
## Sample Input
60+
61+
```text
62+
STDIN Function
63+
----- --------
64+
6 arr[] size n = 6
65+
-4 3 -9 0 4 1 arr = [-4, 3, -9, 0, 4, 1]
66+
```
67+
68+
## Sample Output
69+
70+
```text
71+
0.500000
72+
0.333333
73+
0.166667
74+
```
75+
76+
## Explanation
77+
78+
There are $ 3 $ positive numbers, negative numbers, and $ 1 $ zero in the array.
79+
The proportions of occurrence are positive: $ 3/6 = 0.500000 $,
80+
negative: $ 2/6 = 0.333333 $ and zeros: $ 1/6 = 0.166667 $.

0 commit comments

Comments
 (0)