-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_addition.py
More file actions
66 lines (49 loc) · 2.13 KB
/
list_addition.py
File metadata and controls
66 lines (49 loc) · 2.13 KB
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
63
64
65
# The problem:
# Given a list of numbers, determine whether
# any two of them will add up to a number k
# A basic solution:
def add_k(list_num, k):
for index in range(len(list_num)):
for other_index in range(len(list_num)):
if index != other_index:
if(list_num[index] + list_num[other_index] == k):
return True
else:
return False
# This essentially works by going through each element in the list
# and trying to add it to all other numbers in the list. While this
# works, we are at O(n^2) time for the worst case, in case it's the last
# numbers in the list where we find our addition. We are going through
# some calculations twice.
# Let's see if we can get it done in one pass without repeating ourselves
def add_k_plus_plus(list_num, k):
for index in range(len(list_num)):
for other_index in range(index):
if(list_num[index] + list_num[other_index] == k):
return True
else:
return False
# this time we only go up until that number, so that for x_y in
# list_num, we only add numbers from x_0 to x_(y-1) to x_y
# this way we go through with only one pass on the array, and
# change our time to O((n-1)!)
# tests
list_ones = [1, 1, 1, 1]
list_basic = [1, 2, 3, 4, 5]
list_wild = [1, 2, -3, 4000, 0.5, 0]
empty_list = []
assert add_k(list_ones, 2) == True, "Should be True"
assert add_k(list_ones, 4) == False, "Should be False"
assert add_k(empty_list, 4) == False, "Should be False"
assert add_k(list_basic, 4) == True, "Should be True"
assert add_k(list_basic, 10) == False, "Should be False"
assert add_k(list_wild, 100) == False, "Should be False"
assert add_k(list_wild, 4000.5) == True, "Should be True"
assert add_k_plus_plus(list_ones, 2) == True, "Should be True"
assert add_k_plus_plus(list_ones, 4) == False, "Should be False"
assert add_k_plus_plus(empty_list, 4) == False, "Should be False"
assert add_k_plus_plus(list_basic, 4) == True, "Should be True"
assert add_k_plus_plus(list_basic, 10) == False, "Should be False"
assert add_k_plus_plus(list_wild, 100) == False, "Should be False"
assert add_k_plus_plus(list_wild, 4000.5) == True, "Should be True"
# A more efficient solution