Question: Plz finish the code in Python Question 5 ( 2 0 marks ) A sequence of numbers, denoted as { a 1 , a 2

Plz finish the code in Python
Question 5(20 marks)
A sequence of numbers, denoted as {a1,a2,a3,dots,an}, is called monotonic if it either consis-
tently increases or consistently decreases as you move along the sequence. There are two
types of monotonic sequences:
Monotonic increasing sequence: aiai+1 for all 1in, e.g.,1,2,3,3,7,11.
Monotonic decreasing sequence: aiai+1 for all 1in, e.g,10,9,8,6,4,1.
Write a function longest_monotonic_subseq_size(nums: list[int])-> int that takes
a non-empty list of integers nums and returns the length of the longest monotonic subsequence
found from the consecutive elements of the list.
Notes:
Instead of writing two redundant loops, one for finding the longest monotonic increasing
subsequence and another for the longest monotonic decreasing subsequence, that differ
only by the comparison operator, think of how to avoid redundant code. For example,
the eval () function allows evaluating an expression, e.g.,"a >= b", of a string formed
from your desired operator and operands.
This question aims at finding subsequences from consecutive elements. Subsequences
formed from hopping elements do not count. For example, for 1,2,5,3,6,4,7, the
expected result is 3(subsequence 1,2,5) instead of 5(subsequence 1,2,3,4,7).
Sample Runs:
longest_monotonic_subseq_size ([4,4,5,6,6,2,1]))5
# Two monotonic subsequences found:
# 4,4,5,6,6(monotonic increasing)- the longest
# 6,6,2,1(monotonic decreasing)
# The function should return 5 in this example.
longest_monotonic_subseq_size ([10,9,5,5,3,1,4,8])6
# Two monotonic subsequences found:
# [10,9,5,5,3,1](monotonic decreasing)- the longest
# [1,4,8](monotonic increasing)
# The function should return 6 in this example.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!