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>Apparently, you have only to add the following at the end of your function:</p>

<pre><code>else:
    return 'Only lists allowed'
</code></pre>

<p>I would say it works pretty well... ;)</p>

<p><a href="http://i.stack.imgur.com/8W3t6.png" rel="nofollow"><img src="http://i.stack.imgur.com/8W3t6.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/duL3I.png" rel="nofollow"><img src="http://i.stack.imgur.com/duL3I.png" alt="enter image description here"></a></p>

python