Skip to content

Commit 40ad516

Browse files
authored
Merge pull request #15 from sir-gon/feature/plus_minus
Feature/plus minus
2 parents 8f5fa28 + 46a38a0 commit 40ad516

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

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 $.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
const int HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS = 3;
8+
9+
char **HACKERRANK_WARMUP_plusMinusCalculate(int arr_count, const int *arr);
10+
void HACKERRANK_WARMUP_plusMinus(int arr_count, const int *arr);
11+
12+
#ifdef __cplusplus
13+
} // extern "C"
14+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <exercises/hackerrank/warmup/plus_minus.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/plus_minus.md]]
5+
*/
6+
7+
#include <stdio.h> // malloc
8+
#include <stdlib.h> // snprintf
9+
10+
const int BUFFER_MAX_SIZE = 10;
11+
12+
char *format_result(double number) {
13+
char *buffer = malloc(BUFFER_MAX_SIZE * sizeof(char));
14+
15+
snprintf(buffer, BUFFER_MAX_SIZE, "%0.6lf", number);
16+
return buffer;
17+
}
18+
19+
char **HACKERRANK_WARMUP_plusMinusCalculate(int arr_count, const int *arr) {
20+
int positives = 0;
21+
int negatives = 0;
22+
int zeros = 0;
23+
24+
int i = 0;
25+
26+
while (i < arr_count) {
27+
int value = arr[i];
28+
29+
if (value > 0) {
30+
positives += 1;
31+
}
32+
if (value < 0) {
33+
negatives += 1;
34+
}
35+
if (value == 0) {
36+
zeros += 1;
37+
}
38+
39+
i += 1;
40+
}
41+
42+
double const results[3] = {(double)positives / arr_count,
43+
(double)negatives / arr_count,
44+
(double)zeros / arr_count};
45+
46+
int n = 3; // Número de strings (puede ser arbitrario)
47+
char **answer = malloc(n * sizeof(char *)); // Array de punteros
48+
49+
for (i = 0; i < n; i++) {
50+
char *formatted = format_result(results[i]);
51+
answer[i] = formatted;
52+
}
53+
54+
return answer;
55+
}
56+
57+
void HACKERRANK_WARMUP_plusMinus(int arr_count, const int *arr) {
58+
char **output = HACKERRANK_WARMUP_plusMinusCalculate(arr_count, arr);
59+
60+
for (int i = 0; i < HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS; i++) {
61+
printf("%s", output[i]);
62+
free(output[i]);
63+
}
64+
65+
free(output);
66+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/plus_minus.h>
4+
#include <iostream>
5+
#include <stdexcept>
6+
#include <vector>
7+
8+
#include <filesystem>
9+
#include <fstream>
10+
#include <nlohmann/json.hpp>
11+
using json = nlohmann::json;
12+
13+
TEST_CASE("plusMinus JSON Test Cases", "[hackerrank] [jsontestcase] [warmup]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() + "/unit/lib/hackerrank/warmup/plus_minus.testcases.json";
17+
18+
INFO("plusMinus JSON test cases FILE: " << path);
19+
20+
std::ifstream f(path);
21+
json data = json::parse(f);
22+
23+
for (auto testcase : data) {
24+
auto input_size = static_cast<int>(testcase["input"].size());
25+
std::vector<int> input_vector = testcase["input"];
26+
const int *input_array = input_vector.data();
27+
28+
char **result =
29+
HACKERRANK_WARMUP_plusMinusCalculate(input_size, input_array);
30+
31+
std::vector<std::string> result_as_vector;
32+
33+
for (int i = 0; i < HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS; i++) {
34+
result_as_vector.emplace_back(result[i]);
35+
free(result[i]);
36+
}
37+
free(result);
38+
39+
CHECK(result_as_vector == testcase["expected"]);
40+
41+
// Just call void function, to collect coverage
42+
HACKERRANK_WARMUP_plusMinus(input_size, input_array);
43+
}
44+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"title": "test case 0",
4+
"input": [-4, 3, -9, 0, 4, 1],
5+
"expected": ["0.500000", "0.333333", "0.166667"]
6+
}
7+
]

0 commit comments

Comments
 (0)