Created
October 7, 2015 10:27
-
-
Save ejamesc/6d07f39dfe85f6ca0dc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Find two numbers in a sorted arr that sum to s | |
| def sum(arr, s): | |
| print arr | |
| p1, p2 = 0, len(arr) - 1 | |
| while p1 < p2: | |
| # print p1, p2 | |
| curr = arr[p1] + arr[p2] | |
| if curr == s: | |
| return arr[p1], arr[p2] | |
| elif curr > s: | |
| p2 = p2 - 1 | |
| elif curr < s: | |
| p1 = p1 + 1 | |
| return (0, 0) | |
| if __name__ == "__main__": | |
| input = [1, 2, 4, 7, 11, 15] | |
| pair = sum(input, 15) | |
| print str(pair[0]) + " " + str(pair[1]) | |
| input2 = [1, 4, 45, 6, 10, -8] | |
| input2.sort() | |
| pair = sum(input2, 16) | |
| print str(pair[0]) + " " + str(pair[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment