DEV Community

Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd

Collapse
 
aspittel profile image
Ali Spittel

Monday

Count of positives / sum of negatives (8 KYU):

Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.

link

Collapse
 
choroba profile image
E. Choroba
#! /usr/bin/perl
use warnings;
use strict;

use List::Util qw{ sum0 };

sub cpsn {
    return unless @_;
    my @neg = grep $_ <= 0, @_;
    return @_ - @neg, sum0(@neg)
}

use Test::More tests => 5;
is_deeply [cpsn()], [];
is_deeply [cpsn(-1)], [0, -1];
is_deeply [cpsn(0, 0)], [0, 0];
is_deeply [cpsn(2)], [1, 0];
is_deeply [cpsn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15)],
          [10, -65];
Collapse
 
thejessleigh profile image
jess unrein

Idk that this is an actual good Go solution - I'm not a Go expert so it can be hard for me to tell sometimes. It's so idiomatically verbose!

func Count(a []int) (r []int) {
    r = make([]int, 0)

    if len(a) == 0 {
        return
    }

    var pos int = 0
    var neg int = 0

    for _, item := range a {
        if item >= 0 {
            pos += 1
        } else {
            neg += item
        }
    }

    r = append(r, pos)
    return append(r, neg)
}
Collapse
 
dance2die profile image
Sung M. Kim • Edited

Solved it awhile ago (forgot about it).

Here is a C# answer.

using System.Linq;
using System;

public class Kata
{
    public static int[] CountPositivesSumNegatives(int[] a)
    {
        // guard clause for edge cases
        if (a == null || a.Length == 0) return new int[0];

        int count = 0;
        int sum = 0;
        // ".ToList()" is required to iterate the sequence
        a.Select(n => n > 0 ? count++ : sum += n).ToList();

        return new[] {count, sum};
    }
}

And just re-solved it using JavaScript

function countPositivesSumNegatives(input) {
    return (input && input.length >= 1) 
      ? input.reduce((acc, n) => {
          n > 0 ? acc[0]++ : acc[1] += n;
          return acc;
        }, [0, 0]) 
    : [];
}
Collapse
 
aspittel profile image
Ali Spittel

Refactored to the code-golfiest thing ever with help of @joshcheek !

const countPositivesSumNegatives=i=>(i||[]).reduce(([c=0,s=0],v)=>v>0?[++c,s]:[c,s+v],[])
Collapse
 
bodonferenc profile image
Ferenc Bodon • Edited

Q/kdb+ solution (let l be the input list):

(sum l > 0; sum l where l < 0)
Collapse
 
kspeakman profile image
Kasey Speakman • Edited

F#

let update (count, sum) value =
    if value > 0 then (count + 1), sum
    else              count, (sum + value)

// usage, start with 0 count/sum, update them for each value
let (count, sum) = Array.fold update (0, 0) inputArr

This returns a tuple instead of an array, which I believe is an improvement.

Collapse
 
kaelscion profile image
kaelscion

Python 3 solution update. Posted to the wrong part of the discussion :P


def count_and_sum(arr, x):
    if not arr == None and len(arr) > 0:
        ct = len([i for i in arr if i > x])
        sum_neg = sum([i for i in arr if i <= x])
        return [ct, sum_neg]
    else:
        return([])
Collapse
 
sdicke profile image
Sebastian Martin Dicke

Haskell

countPositivesSumNegatives :: [Int] -> [Int]
countPositivesSumNegatives input =
    let positives = length $ filter (>= 0) input in
    let negatives = sum $ filter (<0) input in
    [positives, negatives]
Collapse
 
clandau profile image
Courtney

TypeScript

export function countPositivesSumNegatives(input: number[]) : number[] {
  let positiveCount = 0, negativeSum = 0;
  if(!input || !input.length) return [];
  for(let num of input) {
      if(num <= 0) negativeSum += num;
      else positiveCount++;
    }
  return [positiveCount, negativeSum];
}