Skip to content

Commit a7cd633

Browse files
cclaussgithub-actions
and
github-actions
authored
Fix astar (#1966)
* Fix astar Single character variable names are old school. * fixup! Format Python code with psf/black push * Tuple * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 3d9bb05 commit a7cd633

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
* [Test Linear Algebra](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/test_linear_algebra.py)
267267

268268
## Machine Learning
269+
* [Astar](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/astar.py)
269270
* [Decision Tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py)
270271
* [Gaussian Naive Bayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gaussian_naive_bayes.py)
271272
* [Gradient Descent](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_descent.py)
@@ -327,6 +328,7 @@
327328
* [Hardy Ramanujanalgo](https://github.com/TheAlgorithms/Python/blob/master/maths/hardy_ramanujanalgo.py)
328329
* [Is Square Free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py)
329330
* [Jaccard Similarity](https://github.com/TheAlgorithms/Python/blob/master/maths/jaccard_similarity.py)
331+
* [Kadanes](https://github.com/TheAlgorithms/Python/blob/master/maths/kadanes.py)
330332
* [Karatsuba](https://github.com/TheAlgorithms/Python/blob/master/maths/karatsuba.py)
331333
* [Kth Lexicographic Permutation](https://github.com/TheAlgorithms/Python/blob/master/maths/kth_lexicographic_permutation.py)
332334
* [Largest Of Very Large Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/largest_of_very_large_numbers.py)

machine_learning/astar.py

+45-47
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import numpy as np
2-
3-
'''
1+
"""
42
The A* algorithm combines features of uniform-cost search and pure
53
heuristic search to efficiently compute optimal solutions.
64
A* algorithm is a best-first search algorithm in which the cost
@@ -11,11 +9,12 @@
119
regular graph-searching algorithm,
1210
essentially planning ahead at each step so a more optimal decision
1311
is made.A* also known as the algorithm with brains
14-
'''
12+
"""
13+
import numpy as np
1514

1615

1716
class Cell(object):
18-
'''
17+
"""
1918
Class cell represents a cell in the world which have the property
2019
position : The position of the represented by tupleof x and y
2120
co-ordinates initially set to (0,0)
@@ -24,18 +23,21 @@ class Cell(object):
2423
g,h,f : The parameters for constructing the heuristic function
2524
which can be any function. for simplicity used line
2625
distance
27-
'''
26+
"""
27+
2828
def __init__(self):
2929
self.position = (0, 0)
3030
self.parent = None
3131

3232
self.g = 0
3333
self.h = 0
3434
self.f = 0
35-
'''
35+
36+
"""
3637
overrides equals method because otherwise cell assign will give
3738
wrong results
38-
'''
39+
"""
40+
3941
def __eq__(self, cell):
4042
return self.position == cell.position
4143

@@ -44,12 +46,11 @@ def showcell(self):
4446

4547

4648
class Gridworld(object):
47-
48-
'''
49+
"""
4950
Gridworld class represents the external world here a grid M*M
5051
matrix
51-
w : create a numpy array with the given world_size default is 5
52-
'''
52+
world_size: create a numpy array with the given world_size default is 5
53+
"""
5354

5455
def __init__(self, world_size=(5, 5)):
5556
self.w = np.zeros(world_size)
@@ -59,48 +60,49 @@ def __init__(self, world_size=(5, 5)):
5960
def show(self):
6061
print(self.w)
6162

62-
'''
63-
get_neighbours
64-
As the name suggests this function will return the neighbours of
65-
the a particular cell
66-
'''
6763
def get_neigbours(self, cell):
64+
"""
65+
Return the neighbours of cell
66+
"""
6867
neughbour_cord = [
69-
(-1, -1), (-1, 0), (-1, 1), (0, -1),
70-
(0, 1), (1, -1), (1, 0), (1, 1)]
68+
(-1, -1),
69+
(-1, 0),
70+
(-1, 1),
71+
(0, -1),
72+
(0, 1),
73+
(1, -1),
74+
(1, 0),
75+
(1, 1),
76+
]
7177
current_x = cell.position[0]
7278
current_y = cell.position[1]
7379
neighbours = []
7480
for n in neughbour_cord:
7581
x = current_x + n[0]
7682
y = current_y + n[1]
77-
if (
78-
(x >= 0 and x < self.world_x_limit) and
79-
(y >= 0 and y < self.world_y_limit)):
83+
if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit:
8084
c = Cell()
8185
c.position = (x, y)
8286
c.parent = cell
8387
neighbours.append(c)
8488
return neighbours
8589

86-
'''
87-
Implementation of a start algorithm
88-
world : Object of the world object
89-
start : Object of the cell as start position
90-
stop : Object of the cell as goal position
91-
'''
92-
9390

9491
def astar(world, start, goal):
95-
'''
92+
"""
93+
Implementation of a start algorithm
94+
world : Object of the world object
95+
start : Object of the cell as start position
96+
stop : Object of the cell as goal position
97+
9698
>>> p = Gridworld()
9799
>>> start = Cell()
98100
>>> start.position = (0,0)
99101
>>> goal = Cell()
100102
>>> goal.position = (4,4)
101103
>>> astar(p, start, goal)
102104
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
103-
'''
105+
"""
104106
_open = []
105107
_closed = []
106108
_open.append(start)
@@ -118,7 +120,7 @@ def astar(world, start, goal):
118120
n.g = current.g + 1
119121
x1, y1 = n.position
120122
x2, y2 = goal.position
121-
n.h = (y2 - y1)**2 + (x2 - x1)**2
123+
n.h = (y2 - y1) ** 2 + (x2 - x1) ** 2
122124
n.f = n.h + n.g
123125

124126
for c in _open:
@@ -130,23 +132,19 @@ def astar(world, start, goal):
130132
path.append(current.position)
131133
current = current.parent
132134
path.append(current.position)
133-
path = path[::-1]
134-
return path
135-
136-
if __name__ == '__main__':
137-
'''
138-
sample run
139-
'''
140-
# object for the world
141-
p = Gridworld()
142-
# stat position and Goal
135+
return path[::-1]
136+
137+
138+
if __name__ == "__main__":
139+
world = Gridworld()
140+
# stat position and Goal
143141
start = Cell()
144142
start.position = (0, 0)
145143
goal = Cell()
146144
goal.position = (4, 4)
147-
print("path from {} to {} ".format(start.position, goal.position))
148-
s = astar(p, start, goal)
149-
# Just for visual Purpose
145+
print(f"path from {start.position} to {goal.position}")
146+
s = astar(world, start, goal)
147+
# Just for visual reasons
150148
for i in s:
151-
p.w[i] = 1
152-
print(p.w)
149+
world.w[i] = 1
150+
print(world.w)

maths/kadanes_algorithm.py renamed to maths/kadanes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d
44
https://en.wikipedia.org/wiki/Maximum_subarray_problem
55
"""
6-
test_data = ([-2, -8, -9], [2, 8, 9], [-1, 0, 1], [0, 0], [])
6+
test_data: tuple = ([-2, -8, -9], [2, 8, 9], [-1, 0, 1], [0, 0], [])
77

88

99
def negative_exist(arr: list) -> int:

0 commit comments

Comments
 (0)