#1

Write a function called manipulate_data which will act as follows:

When given a list of integers, return a list, where the first element is the count of positives numbers and the second element is the sum of negative numbers.

NB: Treat 0 as positive.

This is my code so far

``````def manipulate_data(data):
return [len([n for n in data if isinstance(n, int) and n >= 0]), sum(n for n in data if isinstance(n, int) and n < 0)]``````

it should satisfy the following test

``````import unittest

class ManipulateDataTestCases(unittest.TestCase):
def test_only_lists_allowed(self):
result = manipulate_data({})
self.assertEqual(result, 'Only lists allowed', msg='Invalid argument')

def test_it_returns_correct_output_with_positives(self):
result = manipulate_data([1, 2, 3, 4])
self.assertEqual(result, [4, 0], msg='Invalid output')

def test_returns_correct_ouptut_with_negatives(self):
result = manipulate_data([1, -9, 2, 3, 4, -5]);
self.assertEqual(result, [4, -14], msg='Invalid output')``````

#2

First of all, please put your code in backticks, so it looks clearer here (a guide to formatting your post can be found here: https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/51139/2).

Second thing — what problem exactly do you have with this code?

#3

I keep getting this error:
Failure in line 11, in test_only_lists_allowed self.assertEqual(result, 'Only lists allowed', msg='Invalid argument') AssertionError: Invalid argument

#4

why not list comprehension for the sum as well and cast it to a `list()`? You might need to use an additional set of brackets, so both list comprehensions execute first

#5

#6

#7

Hey myk _kym hw many labs av u don? Writing doz codes to pass some hidden test is really killing pls help

#8

pls help me out on this When given a list of integers, return a list, where the first element is the count of positives numbers and the second element is the sum of negative numbers.

NB: Treat 0 as positive.

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.