Write the program in python Using a list of integers (which you may code directly into your program, rather than inputting), write a program to find and print all pairs of integers in the list whose sum is equal to a given input number, as long as the two numbers are not the same. For example, if the list is [4, 5, 7, 1, 9, 3, 4, 2, 6] and given input sum is 8, the output should be the pairs (5,3) (7,1) (2,6). Note that (4,4) is not a valid pair since the two numbers are the same. Your program should include the following functions to generate the output: function main which gets the required sum number and uses the following functions to generate the desired output as shown below function findPairs which, given the list of integers and the desired sum, finds and returns a list of strings showing the found pairs of numbers that meet the criteria given above; each string should be of the form "(num1,num2)"; note that your function must use a nested loop for this rather than using built-in list functions function printResults which, given the two lists and the sum, prints the original list, the given sum and the resulting list of strings, in the format as shown below Sample input/output for run 1: Enter the expected sum of two values: 8 The given list is: [4, 5, 7, 1, 9, 3, 4, 2, 6] The list of pairs that add to the given sum of 8 is: (5,3) (7,1) (2,6) Sample input/output for run 2: Enter the expected sum of two values: 88 The given list is: [17, 22, 25, 31, 32, 33, 40, 44, 44, 45, 47, 49, 50, 53, 55, 59, 62, 66, 67, 78, 80] The list of pairs that add to the given sum of 88 is: (22,66) (33,55)
Write the program in python
Using a list of integers (which you may code directly into your program, rather than inputting), write a program to find and print all pairs of integers in the list whose sum is equal to a given input number, as long as the two numbers are not the same. For example, if the list is [4, 5, 7, 1, 9, 3, 4, 2, 6] and given input sum is 8, the output should be the pairs (5,3) (7,1) (2,6). Note that (4,4) is not a valid pair since the two numbers are the same. Your program should include the following functions to generate the output:
-
function main which gets the required sum number and uses the following functions to generate the desired output as shown below
-
function findPairs which, given the list of integers and the desired sum, finds and returns a list of strings showing the found pairs of numbers that meet the criteria given above; each string should be of the form "(num1,num2)"; note that your function must use a nested loop for this rather than using built-in list functions
-
function printResults which, given the two lists and the sum, prints the original list, the given sum and the resulting list of strings, in the format as shown below
Sample input/output for run 1:
Enter the expected sum of two values: 8 The given list is: [4, 5, 7, 1, 9, 3, 4, 2, 6] The list of pairs that add to the given sum of 8 is: (5,3)(7,1) (2,6)
Sample input/output for run 2:
Enter the expected sum of two values: 88 The given list is: [17, 22, 25, 31, 32, 33, 40, 44, 44, 45, 47, 49, 50, 53, 55, 59, 62, 66, 67, 78, 80] The list of pairs that add to the given sum of 88 is: (22,66) (33,55)
Step by step
Solved in 2 steps with 1 images