Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Given a list of integers and a single sum value,
- return the first two values (parse from the left please)
- in order of appearance that add up to form the sum.
- sum_pairs([11, 3, 7, 5], 10)
- # ^--^ 3 + 7 = 10
- == [3, 7]
- sum_pairs([4, 3, 2, 3, 4], 6)
- # ^-----^ 4 + 2 = 6, indices: 0, 2 *
- # ^-----^ 3 + 3 = 6, indices: 1, 3
- # ^-----^ 2 + 4 = 6, indices: 2, 4
- # * entire pair is earlier, and therefore is the correct answer
- == [4, 2]
- sum_pairs([0, 0, -2, 3], 2)
- # there are no pairs of values that can be added to produce 2.
- == None/nil/undefined (Based on the language)
- sum_pairs([10, 5, 2, 3, 7, 5], 10)
- # ^-----------^ 5 + 5 = 10, indices: 1, 5
- # ^--^ 3 + 7 = 10, indices: 3, 4 *
- # * entire pair is earlier, and therefore is the correct answer
- == [3, 7]
- Negative numbers and duplicate numbers can and will appear.
- NOTE: There will also be lists tested of lengths
- upwards of 10,000,000 elements. Be sure your code doesn't time out.
- """
- def sum_pairs(ints, s):
- pass
- def assert_eq_or_none(tfunc_call_result, check_against, text):
- if check_against is None:
- assert tfunc_call_result is None, text
- else:
- assert tfunc_call_result == check_against, text
- if __name__ == '__main__':
- args_cases = [
- ([1, 4, 8, 7, 3, 15], 8),
- ([1, -2, 3, 0, -6, 1], -6),
- ([20, -13, 40], -7),
- ([1, 2, 3, 4, 1, 0], 2),
- ([10, 5, 2, 3, 7, 5], 10),
- ([4, -2, 3, 3, 4], 8),
- ([0, 2, 0], 0),
- ([5, 9, 13, -3], 10),
- ]
- test_cases = [
- ([1, 7], "Basic: {} should return [1, 7] for sum = 8"),
- ([0, -6], "Negatives: {} should return [0, -6] for sum = -6"),
- (None, "No Match: {} should return None for sum = -7"),
- ([1, 1], "First Match From Left: {} should return [1, 1] for sum = 2 "),
- ([3, 7], "First Match From Left REDUX!: {} should return [3, 7] for sum = 10 "),
- ([4, 4], "Duplicates: {} should return [4, 4] for sum = 8"),
- ([0, 0], "Zeroes: {} should return [0, 0] for sum = 0"),
- ([13, -3], "Subtraction: {} should return [13, -3] for sum = 10"),
- ]
- for args, case in zip(args_cases, test_cases):
- assert_eq_or_none(sum_pairs(*args), case[0], case[1].format(args[0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement