Positives and negatives¶
Write the function
posneg1()that takes a list of numbers, and returns the string'pos'if all of them are positive, the string'neg'if all of them are negative and the string'posneg'otherwise. The given list is not empty and the value zero is considered positive. Save this function into the fileposneg1.py. Examples:>>> posneg1([-34, -3.1, -28, 0, 0, -95.9]) 'posneg' >>> posneg1([23, 4.5, 0, 0, -33, 12, -5.7]) 'posneg' >>> posneg1([-4, -3, -1.5, -7, -8.2, -5]) 'neg' >>> posneg1([0, -3]) 'posneg' >>> posneg1([0, 0, 0]) 'pos' >>> posneg1([2, 2, 0, 0, 8, 9, 0, 0, 0]) 'pos'
Note
More tests are provided in file
posneg1.txtSolution
A solution of this function is provided in file
posneg1.pyWrite the function
posneg2()that given a list of numbers, returns two lists: one with the positive and zero numbers, and the other one with the negative numbers from the given list. Values in the resulting list must be in the same order than in the given list. Save this function into the fileposneg2.py. Examples:>>> posneg2([1.0, 5.0, -2.0, 0.0, 9.1, -7.4, -6.7, 2.5]) ([1.0, 5.0, 0.0, 9.1, 2.5], [-2.0, -7.4, -6.7])
Note
More tests are provided in file
posneg2.txtSolution
A solution of this function is provided in file
posneg2.pyWrite the function
posneg3()that given a list of numbers, returns a list with two sublists: one with the positive and zero numbers, and the other one with the negative numbers from the given list. Values in the resulting sublists must be ordered increasingly. Save this function into the fileposneg3.py. Examples:>>> posneg3([1.0, 5.0, -2.0, 0.0, 9.1, -7.4, -6.7, 2.5]) [[0.0, 1.0, 2.5, 5.0, 9.1], [-7.4, -6.7, -2.0]]
Note
More tests are provided in file
posneg3.txtSolution
A solution of this function is provided in file
posneg3.pyWrite the modifier function
changeneg()that given a list of numbers, modifies this list so that negative numbers are replaced by 0. Save this function into the filechangeneg.py. Examples:>>> l1 = [3,-2,13,-32,0] >>> changeneg(l1) >>> l1 [3, 0, 13, 0, 0] >>> l2 = [1,2,3.2] >>> changeneg(l2) >>> l2 [1, 2, 3.2]
Note
More tests are provided in file
changeneg.txtSolution
A solution of this function is provided in file
changeneg.pyWrite the function
three_negatives()that given a list of numbers returnsTrueif there are three consecutive negative elements andFalseotherwise. If the list has less than three numbers, this function also returnsFalse. Save this function into the filethree_negatives.py. Examples:>>> three_negatives([-34, -3.1, -28, 0, 0, 0, -95.9]) True >>> three_negatives([23, 4.5, 0, 0, -33, 12, -5.7]) False >>> three_negatives([0, -3, -1.5, -7, 8.2, -5]) True >>> three_negatives([0, 3]) False >>> three_negatives([-4, -5, -8]) True >>> three_negatives([2, 2, -7, -9, -89, -5, -54, 8, 9, -1, -1, -2]) True
Note
More tests are provided in file
three_negatives.txtSolution
A solution of this function is provided in file
three_negatives.py