Write a Python function subsequence(l1,l2) that takes two sorted lists as arguments and returns True if the the first list is a subsequence of the second list, and returns False otherwise.
A subsequence of a list is obtained by dropping some values. Thus, [2,3,4] and [2,2,5] are subsequences of [2,2,3,4,5], but [2,4,4] and [2,4,3] are not.
Answers
Answer:
A function all() compares the items in the lists and return True if every item is truthy, else return False.
Explanation:
Python Code:
l1=[2,3,4] #let elements in L1
l2=[2,2,5] #let elements in l2
def subsequence(l1,l2):
if(all(x in l1 for x in l2)):
print("True")
else:
print("False")
subsequence(l1,l2) #function calling
Python code to compare the list values:
Output:
False
Explanation:
code:
def subsequence(l1,l2):#defining the method subsequence that accepts two lists
if(all(i in l1 for i in l2)):#defining if block that checks list values
print("True") #use print method to return true value
else:#defining else block
print("False")#use print method to return false value
l1=[2,3,4] #defining list1 and assign value
l2=[2,2,5] #defining list2 and assign value
subsequence(l1,l2) #call the function subsequence
Code description:
- In the above-given code, a method "subsequence" is defined that uses two lists in its parameters, in the next step, if block is declared that uses the "all" method.
- In this method, it checks list element values if it matches it prints "true" as a message or goes to else block, in this it will print "False" as the message.
- Outside the method, the two lists "l1 and l2" is declared, that stores the value and pass to the method and call it.
Learn more:
- List: https://brainly.in/question/4046934