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