0

def manipulate_data(n): positives,negatives=[],[] if not isinstance(n,list): return else: for i in n: if i>=0: positives.append(i) if i<0 negatives.append(i) new=[len(positives,sum(negatives)] return new. please the unittest is still telling me that my code failed the "only lists" allowed test.what do I do.

6th Sep 2016, 12:39 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar
7 Antworten
+ 1
def manipulate_data(n: list) -> list: countPos, sumNeg = 0, 0 for i in n: if i>=0: countPos += 1 else: sumNeg += i return [countPos, sumNeg]
6th Sep 2016, 1:55 PM
Zen
Zen - avatar
+ 1
This synthax precises the type of the argument and return value. This is called type hints, and was introduced in Python 3.5: https://docs.python.org/3/library/typing.html Test my code here, it's working: http://code.sololearn.com/cQE1I52IgJIk
6th Sep 2016, 11:19 PM
Zen
Zen - avatar
+ 1
Maybe add this at the beginning of your function block too: if not isinstance(n,list): raise ValueError
7th Sep 2016, 4:43 PM
Zen
Zen - avatar
+ 1
hey @ Dan-Awoh Emanuel, y is my code not working please. here it isdef manipulate_data(n): positives,negatives=[],[] if not isinstance(n,list): return else: for i in n: if i>=0: positives.append(i) if i<0 negatives.append(i) new=[len(positives,sum(negatives)] return new
14th Sep 2016, 1:51 PM
Tobby Tee
Tobby Tee - avatar
0
please assist me with this problem this is is the original question Write a function called manipulated_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.
6th Sep 2016, 12:39 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar
0
Zen please what does that syntax do. I've never seen it before manipulate_data(n:list)->list: and the interpreter is telling me that there is a syntax error
6th Sep 2016, 2:13 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar
0
ok. I've added it and the code worked fine. unittests are key to determining the level of proficiency. I thought I was ok until I started designing codes to meet specifications. NB: the fact that it works doesn't mean it is a good piece of code!!
7th Sep 2016, 6:08 PM
Dan-Awoh Emmanuel
Dan-Awoh Emmanuel - avatar