Skip to content

Commit 17f0dee

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Plus Minus solved ✓
1 parent 16f9b3b commit 17f0dee

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

docs/hackerrank/warmup/plusMinus.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 $.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/plusMinus.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
utils "gon.cl/algorithm-exercises/src/utils"
12+
)
13+
14+
func PlusMinus(arr []int) string {
15+
16+
positives := 0
17+
negatives := 0
18+
zeros := 0
19+
20+
for i := 0; i < len(arr); i += 1 {
21+
var num = arr[i]
22+
23+
if num > 0 {
24+
positives += 1
25+
} else if num < 0 {
26+
negatives += 1
27+
} else if num == 0 {
28+
zeros += 1
29+
}
30+
}
31+
32+
result := []string{}
33+
34+
result = append(result, fmt.Sprintf("%.6f", float64(positives)/float64(len(arr))))
35+
result = append(result, fmt.Sprintf("%.6f", float64(negatives)/float64(len(arr))))
36+
result = append(result, fmt.Sprintf("%.6f", float64(zeros)/float64(len(arr))))
37+
38+
answer := strings.Join(result, "\n")
39+
40+
utils.Info("PlusMinus answer => %s", answer)
41+
42+
return answer
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestPlusMinus(t *testing.T) {
12+
13+
expectedSolution := strings.Join([]string{"0.500000", "0.333333", "0.166667"}, "\n")
14+
input := []int{-4, 3, -9, 0, 4, 1}
15+
16+
testname := fmt.Sprintf("PlusMinus(%d) => %v \n", input, expectedSolution)
17+
t.Run(testname, func(t *testing.T) {
18+
19+
ans := PlusMinus(input)
20+
assert.Equal(t, expectedSolution, ans)
21+
})
22+
}

0 commit comments

Comments
 (0)