110k views
3 votes
For this homework you will make use of loops, if statements, and exceptions. Your program will accept a list of numbers and a target value and display how many pairs of numbers in the list add up to the target value. For instance, if our list was [1, 2, 3, 7, 9] and the target value was 10, then our answer would be 2 (since 1 + 9 = 10 and 3 + 7 = 10).

Three functions are required:

get_input():

This function takes no parameters, but will ask the user for input. For all inputs, you will need to use an indefinite loop and exception handling like we discussed in class. It is required to catch user input error and gracefully recover. The only error you need to consider is a ValueError.

First, it will ask for an integer, n (the number of element in the list)

Then it will ask for n numbers (if a value is given that is a bad input, do not count that as an element).

Finally, it will ask for a target value (as an integer), k

The function will return the list of integers and k.

sum_to_k(nums, k):

This function will take two parameters: a list of integers (nums), and a target value (k). It then will return the number of pairs of numbers in nums that sum to exactly k.

Hint: try nesting two loops, one keeping track of the first number, one keeping track of the second number. If you ensure that the second number is always later than the first number in the list then you won't get duplicate solutions (we should not count 3 + 7 and 7 + 3 as two separate solutions).

main():

This mostly links the two above functions together. You will call get_input() and sum_to_k(nums, k) from main(). It will then print out the number of pairs that sum to k

User Dqminh
by
8.7k points

1 Answer

7 votes

Final answer:

This question is about writing a program that accepts a list of numbers and a target value, and displays how many pairs of numbers in the list add up to the target value. The program will use loops, if statements, and exceptions to handle user input and calculate the number of pairs.

Step-by-step explanation:

get_input() Function:

The get_input() function is responsible for taking user input. It first prompts the user to enter the number of elements in the list, n, and then asks the user to enter n numbers. Exception handling is used to handle any input errors, specifically ValueError. If a bad input is encountered, it is not counted as an element in the list. Finally, the function prompts the user to enter the target value, k, and returns the list of integers and k.

sum_to_k() Function:

The sum_to_k() function takes a list of integers, nums, and a target value, k. It returns the number of pairs of numbers in nums that sum to exactly k. To achieve this, the function uses nested loops, with one loop keeping track of the first number and the other loop keeping track of the second number. To avoid counting duplicate solutions, the second number is always chosen to be later in the list than the first number.

main() Function:

The main() function acts as the main program flow. It calls the get_input() function to obtain the list of numbers and the target value, and then calls the sum_to_k() function to determine the number of pairs that sum to k. Finally, it prints out the result.

User Cmani
by
7.4k points