-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathexample_018_2_DP_pair_sum_target_value.py
63 lines (41 loc) · 1.52 KB
/
example_018_2_DP_pair_sum_target_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Simple Dynamic Programming Example
Finds a pair of numbers in a sequence that sum to a target value.
"""
def pairValues(sequence, target_sum):
"""
Finds a pair of numbers in a sequence that sum to a target value.
Args:
sequence: A list of numbers.
target: The target value.
Returns:
A tuple of the two numbers that sum to the target value, or None if no such pair exists.
"""
for i in range(len(sequence)):
for j in range(len(sequence)):
if i != j and sequence[i] + sequence[j] == target_sum:
return (sequence[i], sequence[j])
return None
# Using Dynamic Programming
# Using dynamic programming, we can create a dictionary to store the values that are already calculated.
# The key for the dictionary will be the target value minus the value from the array.
# If we find a key in the dictionary, we return the pair.
# A memoized approach
def pairValues_DP(sequence, target_sum):
memoized_set = set()
for num in sequence:
diff = target_sum - num
if diff in memoized_set:
return diff, num
else:
memoized_set.add(num)
return None
def main():
sequence = [8, 10, 2, 9, 7, 5]
target_sum = 11
result = pairValues(sequence, target_sum)
print("Pair with sum ", target_sum, " is: ", result)
result = pairValues_DP(sequence, target_sum)
print("Pair with sum ", target_sum, " is: ", result)
if __name__=="__main__":
main()