In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 22nd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #06 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
The name of the .py source file you submit to Mooshak should NOT start with a digit (you will get an error if you submit it like that).
Howard Wolowitz, aerospace engineer (and the only one in the group who’s actually been to space, as he reminds everyone), is working on a safety system for one of NASA's rocket modules.
Each module has a list of n distinct integer codes, representing energy calibration levels of its control thrusters.
To ensure flight stability, NASA’s algorithm requires that at least one pair of thruster codes adds up exactly to a target value x. This value represents a desired combined thrust output.
If no such pair exists, the system issues a warning (and Howard has to explain it to Sheldon, which is somehow worse than explaining it to NASA...). Can you help Howard in finding this pair?
Write a function rocket_pair(lst, x) that receives a list lst of distinct positive integers and an integer x, returning True if the value x can be obtained by summing a pair of values from lst or False otherwise.
The following limits are guaranteed in all the test cases that will be given to your program:
| 1 ≤ |lst| ≤ 50 000 | Number of elements in the list | |
| 1 ≤ lst[i] ≤ 109 | Values in the list | |
| 1 ≤ x ≤ 2 × 109 | Target pair value |
NOTE: the limits are high and you must have an efficient solution.
If your code simply goes through all possible pairs one by one, it will result in a Time Limit Exceeded.
| Example Function Calls | Example Output |
print(rocket_pair([14, 7, 20, 5, 9], 14)) print(rocket_pair([10, 3, 8, 12], 25)) |
True False |
Explanation of the first example
5 + 9 = 14, perfect balance for the module!
Explanation of the second example
No two thruster codes produce a total output of 25.