Data structures homework in Python
<p>I'm supposed to write a function called manipulate_data which will act as follows:</p>

<p>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. </p>

<p>Here is my code</p>

<pre><code>def manipulate_data(data):
  if isinstance(data, (list, tuple, set)):
    #checking if its a list
    return [len([s for s in data if isinstance(s, int) and s &gt; 0]), sum(s for s in data if isinstance(s, int) and s &lt; 0)]
</code></pre>

<p>code it should be tested on</p>

<pre><code>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')
</code></pre>

<p>this should work now for home study</p>

<pre><code>def manipulate_data(data):
        if isinstance(data, list):
            return [sum(1 for n in data if isinstance(n, int) and n &gt;= 0),
                    sum(n for n in data if isinstance(n, int) and n &lt; 0)]
        else:
            return 'Only lists allowed'
        class manipulateDataTestCases(unittest.TestCases):
            def test_only_lists_allowed(self):
                result=manipulate_data({})
                self.assertEqual(result,'only lists allowed',msg='invalid argument')

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

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

        if __name__=='__main__':
            unittest.main()
</code></pre>

python