Question

In: Computer Science

In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...

In Python,

Q.

Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index.

Assumptions and restrictions:

  • The function must return a number.
  • If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements.
  • You can assume that the argument is a sequence, and that its elements are numbers (integer or decimal), but other than that you should not make any assumptions. In particular, you should not assume that it is any particular type of sequence (list, array, etc) and use only operations that are applicable to all sequence types.

Test has to pass this function.

def test_max_increase():
    '''
    This function runs a number of tests of the max_increase function.
    If it works ok, you will just see the output ("all tests passed") at
    the end when you call this function; if some test fails, there will
    be an error message.
    '''

    assert max_increase([]) == 0.0, "empty list has no pair";
    assert max_increase([1]) == 0.0, "size-1 list has no pair";
    assert max_increase((1,2,3,2)) == 2.0;
    assert max_increase([1.0,3.0,1.0,2.0]) == 2.0;
    assert max_increase([3,-1,2,1]) == 3.0;
    assert max_increase([3,2,1,1]) == 0.0, "no increasing pair";
    assert max_increase([226, 264, 337, 364, 485, 529, 482]) == 303.0;

    btc_data = [ 6729.44, 6690.88, 6526.36, 6359.98, 6475.89, 6258.74,
                 6485.10, 6396.64, 6579.00, 6313.51, 6270.20, 6195.01,
                 6253.67, 6313.90, 6233.10, 6139.99, 6546.45, 6282.50,
                 6718.22, 6941.20, 7030.01, 7017.61, 7414.08, 7533.92,
                 7603.99, 7725.43, 8170.01, 8216.74, 8235.70, 8188.00,
                 7939.00, 8174.06 ]
    btc_data.reverse()
    assert abs(max_increase(tuple(btc_data))-589.45) < 1e-6;

    print("all tests passed")

Solutions

Expert Solution

def max_increase(seq):
    if len(seq) <= 1:
        return 0
    max_diff = None
    for i in range(len(seq)):
        diff = None
        for j in range(i + 1, len(seq)):
            if diff is None or seq[j] - seq[i] > diff:
                diff = seq[j] - seq[i]
        if max_diff is None or (diff is not None and diff > max_diff):
            max_diff = diff
    return max_diff


def test_max_increase():
    '''
    This function runs a number of tests of the max_increase function.
    If it works ok, you will just see the output ("all tests passed") at
    the end when you call this function; if some test fails, there will
    be an error message.
    '''

    assert max_increase([]) == 0.0, "empty list has no pair";
    assert max_increase([1]) == 0.0, "size-1 list has no pair";
    assert max_increase((1, 2, 3, 2)) == 2.0;
    assert max_increase([1.0, 3.0, 1.0, 2.0]) == 2.0;
    assert max_increase([3, -1, 2, 1]) == 3.0;
    assert max_increase([3, 2, 1, 1]) == 0.0, "no increasing pair";
    assert max_increase([226, 264, 337, 364, 485, 529, 482]) == 303.0;

    btc_data = [6729.44, 6690.88, 6526.36, 6359.98, 6475.89, 6258.74,
                6485.10, 6396.64, 6579.00, 6313.51, 6270.20, 6195.01,
                6253.67, 6313.90, 6233.10, 6139.99, 6546.45, 6282.50,
                6718.22, 6941.20, 7030.01, 7017.61, 7414.08, 7533.92,
                7603.99, 7725.43, 8170.01, 8216.74, 8235.70, 8188.00,
                7939.00, 8174.06]
    btc_data.reverse()
    assert abs(max_increase(tuple(btc_data)) - 589.45) < 1e-6;

    print("all tests passed")


test_max_increase()


Related Solutions

Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
A) Develop a function GC(seq) using MATLAB that takes in as input parameter a nucleotide sequence seq returns the computed (G+C) content for the inputted seq.
A)    Develop a function GC(seq) using MATLAB that takes in as input parameter a nucleotide sequence seq returns the computed (G+C) content for the inputted seq.B)    Develop a function GCSlidingWindow (seq, winsize, overlap) that takes three input parameters, a biological seq, a sliding window size winsize and the extent of overlap between successive sliding windows { overlap. It calls the function GC and plots the G+C property measured along the span of the argument seq.
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT