Learn Data Structures and Algorithms With Python - Brute Force Algorithms Cheatsheet - Codecademy
Learn Data Structures and Algorithms With Python - Brute Force Algorithms Cheatsheet - Codecademy
Learn Data Structures and Algorithms With Python - Brute Force Algorithms Cheatsheet - Codecademy
For a list that contains n items, the best case for a linear
search is when the target value is equal to the first
element of the list. In such cases, only one comparison is
needed. Therefore, the best case performance is O(1).
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 1/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 2/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
Linear search
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 3/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
The linear search function may throw a ValueError with def linear_search(lst, match):
a message when the target value is not found in the
for idx in range(len(lst)):
search list. Calling the linear search function inside a try
block is recommended to catch the ValueError if lst[idx] == match:
exception in the except block. return idx
else:
raise ValueError("{0} not in
list".format(match))
The Linear Search function can be enhanced to find and def find_maximum(lst):
return the maximum value in a list of numeric elements.
max = None
This is done by maintaining a variable that is compared to
every element and updated when its value is smaller than for el in lst:
the current element. if max == None or el > max:
max = el
return max
test_scores = [88, 93, 75, 100, 80, 67,
71, 92, 90, 83]
print(find_maximum(test_scores)) # returns
100
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 4/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
A linear search function may have more than one match def linear_search(lst, match):
from the input list. Instead of returning just one index to
matches = []
the matched element, we return a list of indices. Every
time we encounter a match, we add the index to the list. for idx in range(len(lst)):
if lst[idx] == match:
matches.append(idx)
if matches:
return matches
else:
raise ValueError("{0} not in
list".format(match))
A brute force algorithm solves a problem through # pseudocode that prints all divisors of n
exhaustion: it goes through all possible choices
by brute force
until a solution is found.
The time complexity of a brute force algorithm is
often proportional to the input size. define printDivisors, n
Brute force algorithms are simple and consistent,
for all numbers from 1 to n
but very slow.
if the number is a divisor of n
print the number
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 5/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 6/7
2/25/24, 11:37 PM Learn Data Structures and Algorithms with Python: Brute Force Algorithms Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python/modules/brute-force-algorithms/cheatsheet 7/7