logo CodeStepByStep logo

sum_of_2020

Write a function named sum_of_2020 that accepts a string representing a file name as a parameter, reads the given file, finds the two numbers in that file that sum to exactly 2020, and returns the product of those numbers. The file will contain exactly one non-negative integer per line. For example, if the file named numbers.txt contains the following values:

1721
979
366
299
675
1456

The two entries that sum to 2020 are 1721 and 299. Multiplying them together produces a product of 1721 * 299 = 514579, so the call of sum_of_2020("numbers.txt") should return 514579.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be exactly one pair of values in the file that sum to 2020.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.