Knowee
Questions
Features
Study Tools

Write a Python program to read two integer lists. If the lengths of the two lists are equal, create a new list that contains the absolute differences between corresponding elements of the given lists, if not, then print "Unequal lengths".Note: Please map your input and output layout according to the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:list1:·10,20,30list2:·4,5,8[6,·15,·22]Test Case 2:Expected Output:list1:·15,45,65,85,96list2:·20,25,32,65,45,68,69,45Unequal·lengths

Question

Write a Python program to read two integer lists. If the lengths of the two lists are equal, create a new list that contains the absolute differences between corresponding elements of the given lists, if not, then print "Unequal lengths".Note: Please map your input and output layout according to the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:list1:·10,20,30list2:·4,5,8[6,·15,·22]Test Case 2:Expected Output:list1:·15,45,65,85,96list2:·20,25,32,65,45,68,69,45Unequal·lengths

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that fulfills your requirements:

def compare_lists(list1, list2):
    if len(list1) == len(list2):
        return [abs(a - b) for a, b in zip(list1, list2)]
    else:
        return "Unequal lengths"

# Test Case 1
list1 = [10, 20, 30]
list2 = [4, 5, 8]
print(compare_lists(list1, list2))

# Test Case 2
list1 = [15, 45, 65, 85, 96]
list2 = [20, 25, 32, 65, 45, 68, 69, 45]
print(compare_lists(list1, list2))

In this program, we first define a function compare_lists that takes two lists as arguments. If the lengths of the lists are equal, it uses a list comprehension to create a new list that contains the absolute differences between corresponding elements of the two lists. If the lengths are not equal, it returns the string "Unequal lengths".

Then, we test this function with two test cases. The first test case uses lists of equal length, and the second test case uses lists of unequal length. The results are printed to the console.

This problem has been solved

Similar Questions

Test Case 1:Expected Output:list1:·10,20,30list2:·4,5,8[6,·15,·22]Test Case 2:Expected Output:list1:·15,45,65,85,96list2:·20,25,32,65,45,68,69,45Unequal·lengths

You are given two lists of different lengths of positive integers. Write an algorithm to count the number of elements that are not common to each list.InputThe first line of the input consists of an integer - listInput1_size, an integer representing the number of elements in the first list (N).The second line consists of N space-separated integers representing the first list of positive integers.The third line consists of an integer- listInput2_size, representing the number of elements in the second list (M).The last line consists of M space-separated integers representing the second list of positive integers.OutputPrint a positive integer representing the count of elements that are not common to both the lists of integers.ExampleInput:111 1 2 3 4 5 5 7 6 9 101011 12 13 4 5 6 7 18 19 20Output:12Explanation:The numbers that are not common to both lists are [1, 1, 2, 3, 9, 10, 11, 12, 13, 18, 19, 20].So, the output is 12.

Given the Python code below, what will be the output when comparing the lists list1, list2, and list3?list1 = [4]list2 = list1list3 = [4]print(list1 is list2, list1 == list3)

Write a Python program to iterate over two lists simultaneously.Constraints:Input Format:Line 1: <List – 1>Line 2: <List – 2>Output Format:Each set of values separated by a space.Example:Input:[1, 2, 3]['red', 'white', 'black']Output:1 red2 white3 blackExplanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [1, 2, 3] ['red', 'white', 'black']1 red2 white3 black

Write a Python program to interchange the first and last element of the ListConstraints:Input Format:Line 1: <Read a List>Output Format:<List>Example:Input:[5,10,15,20,25,30,35,40]Output:[40, 10, 15, 20, 25, 30, 35, 5]Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [5,10,15,20,25,30,35,40][40, 10, 15, 20, 25, 30, 35, 5]

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.