Skip to content

Commit af7504c

Browse files
committed
fixing
1 parent 16d7bfb commit af7504c

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

machine_learning/astar.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
'''
3+
"""
44
The A* algorithm combines features of uniform-cost search and pure
55
heuristic search to efficiently compute optimal solutions.
66
A* algorithm is a best-first search algorithm in which the cost
@@ -11,11 +11,11 @@
1111
regular graph-searching algorithm,
1212
essentially planning ahead at each step so a more optimal decision
1313
is made.A* also known as the algorithm with brains
14-
'''
14+
"""
1515

1616

1717
class Cell(object):
18-
'''
18+
"""
1919
Class cell represents a cell in the world which have the property
2020
position : The position of the represented by tupleof x and y
2121
co-ordinates initially set to (0,0)
@@ -24,18 +24,21 @@ class Cell(object):
2424
g,h,f : The parameters for constructing the heuristic function
2525
which can be any function. for simplicity used line
2626
distance
27-
'''
27+
"""
28+
2829
def __init__(self):
2930
self.position = (0, 0)
3031
self.parent = None
3132

3233
self.g = 0
3334
self.h = 0
3435
self.f = 0
35-
'''
36+
37+
"""
3638
overrides equals method because otherwise cell assign will give
3739
wrong results
38-
'''
40+
"""
41+
3942
def __eq__(self, cell):
4043
return self.position == cell.position
4144

@@ -45,11 +48,11 @@ def showcell(self):
4548

4649
class Gridworld(object):
4750

48-
'''
51+
"""
4952
Gridworld class represents the external world here a grid M*M
5053
matrix
5154
w : create a numpy array with the given world_size default is 5
52-
'''
55+
"""
5356

5457
def __init__(self, world_size=(5, 5)):
5558
self.w = np.zeros(world_size)
@@ -59,48 +62,57 @@ def __init__(self, world_size=(5, 5)):
5962
def show(self):
6063
print(self.w)
6164

62-
'''
65+
"""
6366
get_neighbours
6467
As the name suggests this function will return the neighbours of
6568
the a particular cell
66-
'''
69+
"""
70+
6771
def get_neigbours(self, cell):
6872
neughbour_cord = [
69-
(-1, -1), (-1, 0), (-1, 1), (0, -1),
70-
(0, 1), (1, -1), (1, 0), (1, 1)]
73+
(-1, -1),
74+
(-1, 0),
75+
(-1, 1),
76+
(0, -1),
77+
(0, 1),
78+
(1, -1),
79+
(1, 0),
80+
(1, 1),
81+
]
7182
current_x = cell.position[0]
7283
current_y = cell.position[1]
7384
neighbours = []
7485
for n in neughbour_cord:
7586
x = current_x + n[0]
7687
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)):
88+
if (x >= 0 and x < self.world_x_limit) and (
89+
y >= 0 and y < self.world_y_limit
90+
):
8091
c = Cell()
8192
c.position = (x, y)
8293
c.parent = cell
8394
neighbours.append(c)
8495
return neighbours
8596

86-
'''
97+
98+
"""
8799
Implementation of a start algorithm
88100
world : Object of the world object
89101
start : Object of the cell as start position
90102
stop : Object of the cell as goal position
91-
'''
103+
"""
92104

93105

94106
def astar(world, start, goal):
95-
'''
107+
"""
96108
>>> p = Gridworld()
97109
>>> start = Cell()
98110
>>> start.position = (0,0)
99111
>>> goal = Cell()
100112
>>> goal.position = (4,4)
101113
>>> astar(p, start, goal)
102114
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
103-
'''
115+
"""
104116
_open = []
105117
_closed = []
106118
_open.append(start)
@@ -118,7 +130,7 @@ def astar(world, start, goal):
118130
n.g = current.g + 1
119131
x1, y1 = n.position
120132
x2, y2 = goal.position
121-
n.h = (y2 - y1)**2 + (x2 - x1)**2
133+
n.h = (y2 - y1) ** 2 + (x2 - x1) ** 2
122134
n.f = n.h + n.g
123135

124136
for c in _open:
@@ -133,20 +145,21 @@ def astar(world, start, goal):
133145
path = path[::-1]
134146
return path
135147

136-
if __name__ == '__main__':
137-
'''
148+
149+
if __name__ == "__main__":
150+
"""
138151
sample run
139-
'''
140-
# object for the world
152+
"""
153+
# object for the world
141154
p = Gridworld()
142-
# stat position and Goal
155+
# stat position and Goal
143156
start = Cell()
144157
start.position = (0, 0)
145158
goal = Cell()
146159
goal.position = (4, 4)
147160
print("path from {} to {} ".format(start.position, goal.position))
148161
s = astar(p, start, goal)
149-
# Just for visual Purpose
162+
# Just for visual Purpose
150163
for i in s:
151164
p.w[i] = 1
152165
print(p.w)

maths/kadanes_algorithm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
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+
7+
test_data = [[-2, -8, -9], [2, 8, 9], [-1, 0, 1], [0, 0], []]
78

89

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

0 commit comments

Comments
 (0)