Hackerrank Plus Minus Problem Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { float a,b,c,p=0.0,n=0.0,z=0.0; int num; scanf("%d",&num); int arr[num]; for(int i=0;i<num;i++) scanf("%d",&arr[i]); for(int q=0;q<num;q++) { if(arr[q]==0) {z=z+1.0;} if(arr[q]>0) {p=p+1.0;} if(arr[q]<0) {n=n+1.0;} } printf("%.3f\n",p/((double)num)); printf("%.3f\n",n/((double)num)); printf("%.3f\n",z/((double)num)); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Hackerrank Plus Minus Problem Solution in JavaScript
function processData(input) { var lines = input.split('\n'); var N = lines[0]; var numbers = lines[1].split(' ').map(function(el) { return parseInt(el, 10) }); var pos = numbers.filter(function(el) { return el > 0; }).length; var neg = numbers.filter(function(el) { return el < 0; }).length; var zero = N - pos - neg; console.log(pos / N); console.log(neg / N); console.log(zero / N); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
Hackerrank Plus Minus Problem Solution in Python
import sys N = 0 p = 0 m = 0 z = 0 for line in sys.stdin: if(N == 0): N = int(line) else: myList = line.split() for val in myList: if int(val) < 0: m += 1 elif int(val) > 0: p += 1 else: z += 1 #print '%d %d %d' %(p,m,z) print '%f' %((p / float(N))) print '%f' %((m / float(N))) print '%f' %((z / float(N)))
0 Comments