Positives and negatives

  1. 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 file posneg1.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.txt

    Solution

    A solution of this function is provided in file posneg1.py

  2. Write 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 file posneg2.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.txt

    Solution

    A solution of this function is provided in file posneg2.py

  3. Write 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 file posneg3.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.txt

    Solution

    A solution of this function is provided in file posneg3.py

  4. Write 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 file changeneg.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.txt

    Solution

    A solution of this function is provided in file changeneg.py

  5. Write the function three_negatives() that given a list of numbers returns True if there are three consecutive negative elements and False otherwise. If the list has less than three numbers, this function also returns False. Save this function into the file three_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.txt

    Solution

    A solution of this function is provided in file three_negatives.py