Skip to content

Commit 6fdd53c

Browse files
Fixes LGTM issues (TheAlgorithms#1745)
* Fixes redefinition of a variable * Fixes implementing __eq__ * Updates docstring
1 parent 80718bd commit 6fdd53c

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

dynamic_programming/max_sum_contiguous_subsequence.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def max_subarray_sum(nums: list) -> int:
66
if not nums:
77
return 0
88
n = len(nums)
9-
s = [0] * n
9+
1010
res, s, s_pre = nums[0], nums[0], nums[0]
1111
for i in range(1, n):
1212
s = max(nums[i], s_pre + nums[i])

searches/hill_climbing.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
class SearchProblem:
66
"""
7-
A interface to define search problems. The interface will be illustrated using
8-
the example of mathematical function.
7+
An interface to define search problems.
8+
The interface will be illustrated using the example of mathematical function.
99
"""
1010

1111
def __init__(self, x: int, y: int, step_size: int, function_to_optimize):
1212
"""
1313
The constructor of the search problem.
14-
x: the x coordinate of the current search state.
15-
y: the y coordinate of the current search state.
16-
step_size: size of the step to take when looking for neighbors.
17-
function_to_optimize: a function to optimize having the signature f(x, y).
14+
15+
x: the x coordinate of the current search state.
16+
y: the y coordinate of the current search state.
17+
step_size: size of the step to take when looking for neighbors.
18+
function_to_optimize: a function to optimize having the signature f(x, y).
1819
"""
1920
self.x = x
2021
self.y = y
@@ -63,6 +64,14 @@ def __hash__(self):
6364
"""
6465
return hash(str(self))
6566

67+
def __eq__(self, obj):
68+
"""
69+
Check if the 2 objects are equal.
70+
"""
71+
if isinstance(obj, SearchProblem):
72+
return hash(str(self)) == hash(str(obj))
73+
return False
74+
6675
def __str__(self):
6776
"""
6877
string representation of the current search state.
@@ -85,10 +94,11 @@ def hill_climbing(
8594
max_iter: int = 10000,
8695
) -> SearchProblem:
8796
"""
88-
implementation of the hill climbling algorithm. We start with a given state, find
89-
all its neighbors, move towards the neighbor which provides the maximum (or
90-
minimum) change. We keep doing this until we are at a state where we do not
91-
have any neighbors which can improve the solution.
97+
Implementation of the hill climbling algorithm.
98+
We start with a given state, find all its neighbors,
99+
move towards the neighbor which provides the maximum (or minimum) change.
100+
We keep doing this until we are at a state where we do not have any
101+
neighbors which can improve the solution.
92102
Args:
93103
search_prob: The search state at the start.
94104
find_max: If True, the algorithm should find the maximum else the minimum.

0 commit comments

Comments
 (0)