Question

In: Computer Science

In Python write a loop that tests whether two lists contain the same integers in the...

In Python write a loop that tests whether two lists contain the same integers in the same order:

listA = [1, 2, 3, 4]

listB = [1, 2, 3, 4]

equal = True

for ______ in ______:

   if _______ != ________ :

      _______ = ________

if equal:

   print(_______)

else:

   print(_______)

Solutions

Expert Solution

Program:

# define first list
listA = [1, 4, 3, 2]

# define second list
listB = [1, 2, 3, 4]

# initially set equal as true
equal = True

# Take each element from first and second list
for (data1, data2) in zip(listA, listB):

    # check both value are equal
    if data1 != data2:

        # Change equal as False
        equal = False

# check equal is true
if equal:

    # then print both lists contain the same integers in the same order
    print("lists contain the same integers in the same order")

# if not
else:

    # then print both lists contain not same integers or not in the same order
   print("lists contain the not the same integers and not in the same order")

Screenshot:

.


Related Solutions

Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Write a Python program called arecongruent.py that determines whether two integers a and b are congruent...
Write a Python program called arecongruent.py that determines whether two integers a and b are congruent modulo n. Your code must work as follows: From the command prompt the user runs the program by typing python arecongruent.py and then your program interface prompts the user for the values of a, b, and n. The program outputs either True or False, and the values of a mod n and b mod n. Submit your Python source code arecongruent.py. NOTE: please include...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
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.
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
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...
python Write a Loop class. The contents of a loop object are represented internally as a...
python Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables: loop - a python list containing the elements of the loop list head - which stores the index of the first element of the loop list current - which stores the index of the last element of the loop list size - which stores how many actual elements are in the loop max - which...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT