Question

In: Computer Science

Given two lists, write python code to print “True” if the two lists have at least...

Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.

Solutions

Expert Solution

Code 1:

def common_elements(L1, L2):
    outcome = False
  

    for a in L1:      # traversal of L1 i.e, 1st list
  
        for b in L2: # traversal of L2 i.e, 2nd list
    
            if a == b:          #Checking if common element preset, if present
                outcome = True
                return outcome
                  
    return outcome
      

# Driver code
L1 = list()
L2 = list()

size1 = int(input("enter size of L1:"))
print("Enter the elements of L1:")

for a in range(int(size1)): #creating an list of required size
   x = int(input(""))         #storing input elements in list L1
   L1.append(x)

size2 = int(input("Enter size of L2:"))
print("Enter the elements of L2:")

for a in range(int(size2)):   #creating an list of required size
   x = int(input(""))           #storing input elements in list L1
   L2.append(x)

print(common_elements(L1, L2)) #Print the result

----------------------------------------------------------------------------------------------------------------

Snapshot of Code and Output:

CODE 2: (Taking input lists directly)

def common_elements(L1, L2):
outcome = False
  

for a in L1: # traversal of L1 i.e, 1st list
  
for b in L2: # traversal of L2 i.e, 2nd list
  
if a == b: #Checking if common element preset, if present
outcome = True
return outcome
  
return outcome
  

#Driver Code
L1 = [1, 2, 3]
L2 = [3, 4, 5]
print(common_elements(L1, L2))
  
L1 = [1, 2, 3]
L2 = [4, 5, 6]
print(common_elements(L1, L2))

Snapshot of Code and Output:



Related Solutions

Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print()...
write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print() function to output a variety of data Use the input() function to ask for multiple data types Use basic arithmetic operations in a program Use string methods and operations Use if/else if/else statements to determine the flow of the program Comment your code Debug your code Test your code Scenario A new marketing company is launching an Artificial Intelligence (AI) ChatBot for their website...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m (sample mean) and s (sample standard deviation) from the sample.
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m and s from the sample. Do the same using the Bayes’ estimator assuming a prior distribution for μ.
PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT