Question

Problem 5 in python, write a function called sequencelength that accepts two input arguments: a list...

Problem 5

in python, write a function called sequencelength that accepts two input arguments: a list of integers (called mylist here) and an integer (called x here). The function should return True if mylist contains an increasing sequence with a length of at least x values. Otherwise, the function should return False. An increasing sequence of numbers is one in which each number is larger than the one before it. For example, [4, 8, 13, 14, 21] is an increasing sequence of length 5, but [4, 8, 13, 12, 21] is not.

Examples:
sequencelength([1, 4, 3, 7, 8, 5, 9], 2)  True sequencelength([1, 4, 3, 7, 8, 5, 9], 3)  True sequencelength([1, 4, 3, 7, 8, 5, 9], 4)  False sequencelength([2, 4, 6, 5, 7, 9, 11, 8, 12, 13], 4)True sequencelength([2, 4, 6, 5, 7, 9, 11, 8, 12, 13], 5)  False

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I am posting code and screenshot of program with output .

In this problem in function sequencelength(list,n) firstly I find longest increasing sequence then compare with given desired length of longest increasing sequence and return True and False accordingly.

Code :-

# function to find list contains an increasing sequence of length n
def sequencelength(list,n):
    # intialization of variable
    maxlength = 0
    count = 0
    # loop to traverse through list element
    for i in range(0,len(list)):
        # first element of list so it is always a increasing sequence
        if i == 0:
            # increment in count i.e in increasing sequence
            count += 1
            # check whether current count is greater then previous max length or not
            maxlength = max(maxlength,count)
        # if next element in list is greater then current element
        elif list[i] > list[i-1]:
            # increment in count i.e in increasing sequence
            count += 1
            # check whether current count is greater then previous max length or not
            maxlength = max(maxlength, count)
        # if next element in list is less then current element so break in increasing sequence 
        # reset count to 1
        else:
            count = 1
    # check whether length of longest increasing sequence is greater then or equal to 
    # desired length or not
    if maxlength >= n:
        return True
    else:
        return False

n = int(input("Enter number of element in my_list "))
my_list = []
print("Enter my_list element ")
for i in range(0,n):
    my_list.append(int(input()))
x = int(input("desired length of increasing sequence in list"))
print(sequencelength(my_list,x))

Screenshot of program with output :-

Add a comment
Know the answer?
Add Answer to:
Problem 5 in python, write a function called sequencelength that accepts two input arguments: a list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

  • In Python beginner code: Write a function named even_div, that takes two arguments (you can safely...

    In Python beginner code: Write a function named even_div, that takes two arguments (you can safely assume that both arguments will always be non-zero integers). When this function is called, it should return how many times the first argument can be divided by the second argument, if it is evenly divisible (no remainder). If it is not evenly divisible, the function should return 0. Examples: even_div(5, 2) will return 0 (5 divided by 2 is 2.5, not evenly divisible) even_div(10,...

  • Python problem QUESTION 48 Write a function named was that accepts two integer values as arquments...

    Python problem QUESTION 48 Write a function named was that accepts two integer values as arquments and returns the wake that is greater of the two for example, if 7 and 12 are passed as arguments the function should return 12. If the arguments are equal the function should return zero. TTTA 3112) T V 2

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

  • python Write a function called has_odd_even that takes three integers as parameters and that returns True...

    python Write a function called has_odd_even that takes three integers as parameters and that returns True if there is at least one odd and at least one even among the three numbers and that returns False otherwise. Below are some sample calls and the appropriate value to return. Function call Value returned has_odd_even(2, 4, 6) False has_odd_even(2, 3, 4) True has_odd_even(12, 4, 17) True has_odd_even(5, 17, 4) True has_odd_even(14, 7, 5) True has_odd_even(5, 4, 2) True has_odd_even(13, 20, 91) True...

  • python In a program, write a function that accepts two arguments: a list, and a number...

    python In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display the number n, the list of numbers, and a sub list of all the numbers in the list that are greater than the number n. Initialize the list and the number n in the main function of your code and call your function, passing the list and number as arguments. Run your code...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • Python 3 code Write a function named coinflip that accepts an input called flips. The function...

    Python 3 code Write a function named coinflip that accepts an input called flips. The function should sim-ulate flipping a coin flips times. The function should return True if the coin was the same result for every flip. You can assume the function receives a positive integer as input. Write a second function named simulation that runs trials of the coin flip experiment. simulation should accept two parameters: the number of trials, and the number of coin flips to do...

  • PYTHON The first function you will write should be called ‘countDown’. Your function should take zero...

    PYTHON The first function you will write should be called ‘countDown’. Your function should take zero (0) arguments. The function should return one (1) list containing integers from 10 to 1 and finally, the string “Liftoff!”. (i.e., [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ‘Liftoff!’]). You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc). 3. Function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT