Skip to content

Commit 0a2fa23

Browse files
committed
PEP8. string atoi added.
1 parent 88c4c61 commit 0a2fa23

17 files changed

+156
-219
lines changed

algorithms/a_star_path_finding.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import heapq
22

3+
34
class Cell(object):
45
def __init__(self, x, y, reachable):
5-
"""
6-
Initialize new cell
6+
"""Initialize new cell.
77
88
@param reachable is cell reachable? not a wall?
99
@param x cell x coordinate
@@ -21,6 +21,7 @@ def __init__(self, x, y, reachable):
2121
self.h = 0
2222
self.f = 0
2323

24+
2425
class AStar(object):
2526
def __init__(self):
2627
# open list
@@ -34,8 +35,7 @@ def __init__(self):
3435
self.grid_width = None
3536

3637
def init_grid(self, width, height, walls, start, end):
37-
"""
38-
Prepare grid cells, walls.
38+
"""Prepare grid cells, walls.
3939
4040
@param width grid's width.
4141
@param height grid's height.
@@ -56,17 +56,16 @@ def init_grid(self, width, height, walls, start, end):
5656
self.end = self.get_cell(*end)
5757

5858
def get_heuristic(self, cell):
59-
"""
60-
Compute the heuristic value H for a cell: distance between
61-
this cell and the ending cell multiply by 10.
59+
"""Compute the heuristic value H for a cell.
60+
61+
Distance between this cell and the ending cell multiply by 10.
6262
6363
@returns heuristic value H
6464
"""
6565
return 10 * (abs(cell.x - self.end.x) + abs(cell.y - self.end.y))
6666

6767
def get_cell(self, x, y):
68-
"""
69-
Returns a cell from the cells list
68+
"""Returns a cell from the cells list.
7069
7170
@param x cell x coordinate
7271
@param y cell y coordinate
@@ -75,9 +74,9 @@ def get_cell(self, x, y):
7574
return self.cells[x * self.grid_height + y]
7675

7776
def get_adjacent_cells(self, cell):
78-
"""
79-
Returns adjacent cells to a cell. Clockwise starting
80-
from the one on the right.
77+
"""Returns adjacent cells to a cell.
78+
79+
Clockwise starting from the one on the right.
8180
8281
@param cell get adjacent cells for this cell
8382
@returns adjacent cells list.
@@ -105,8 +104,7 @@ def get_path(self):
105104
return path
106105

107106
def update_cell(self, adj, cell):
108-
"""
109-
Update adjacent cell
107+
"""Update adjacent cell.
110108
111109
@param adj adjacent cell to current cell
112110
@param cell current cell being processed
@@ -117,8 +115,7 @@ def update_cell(self, adj, cell):
117115
adj.f = adj.h + adj.g
118116

119117
def solve(self):
120-
"""
121-
Solve maze, find path to ending cell.
118+
"""Solve maze, find path to ending cell.
122119
123120
@returns path or None if not found.
124121
"""
@@ -146,5 +143,3 @@ def solve(self):
146143
self.update_cell(adj_cell, cell)
147144
# add adj cell to open list
148145
heapq.heappush(self.opened, (adj_cell.f, adj_cell))
149-
150-

algorithms/binary_tree.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
class Node:
2-
"""
3-
Tree node: left and right child + data which can be any object
1+
from __future__ import print_function
2+
3+
4+
class Node(object):
5+
"""Tree node: left and right child + data which can be any object
6+
47
"""
58
def __init__(self, data):
6-
"""
7-
Node constructor
9+
"""Node constructor
810
911
@param data node data object
1012
"""
@@ -13,8 +15,7 @@ def __init__(self, data):
1315
self.data = data
1416

1517
def insert(self, data):
16-
"""
17-
Insert new node with data
18+
"""Insert new node with data
1819
1920
@param data node data object to insert
2021
"""
@@ -33,8 +34,7 @@ def insert(self, data):
3334
self.data = data
3435

3536
def lookup(self, data, parent=None):
36-
"""
37-
Lookup node containing data
37+
"""Lookup node containing data
3838
3939
@param data node data object to look up
4040
@param parent node's parent
@@ -52,8 +52,7 @@ def lookup(self, data, parent=None):
5252
return self, parent
5353

5454
def delete(self, data):
55-
"""
56-
Delete node containing data
55+
"""Delete node containing data
5756
5857
@param data node's content to delete
5958
"""
@@ -68,7 +67,6 @@ def delete(self, data):
6867
parent.left = None
6968
else:
7069
parent.right = None
71-
del node
7270
else:
7371
self.data = None
7472
elif children_count == 1:
@@ -83,7 +81,6 @@ def delete(self, data):
8381
parent.left = n
8482
else:
8583
parent.right = n
86-
del node
8784
else:
8885
self.left = n.left
8986
self.right = n.right
@@ -105,8 +102,7 @@ def delete(self, data):
105102
parent.right = successor.right
106103

107104
def compare_trees(self, node):
108-
"""
109-
Compare 2 trees
105+
"""Compare 2 trees
110106
111107
@param node tree to compare
112108
@returns True if the tree passed is identical to this tree
@@ -131,18 +127,18 @@ def compare_trees(self, node):
131127
return res
132128

133129
def print_tree(self):
134-
"""
135-
Print tree content inorder
130+
"""Print tree content inorder
131+
136132
"""
137133
if self.left:
138134
self.left.print_tree()
139-
print self.data,
135+
print(self.data, end=" ")
140136
if self.right:
141137
self.right.print_tree()
142138

143139
def tree_data(self):
144-
"""
145-
Generator to get the tree nodes data
140+
"""Generator to get the tree nodes data
141+
146142
"""
147143
# we use a stack to traverse the tree in a non-recursive way
148144
stack = []
@@ -151,14 +147,14 @@ def tree_data(self):
151147
if node:
152148
stack.append(node)
153149
node = node.left
154-
else: # we are returning so we pop the node and we yield it
150+
else:
151+
# we are returning so we pop the node and we yield it
155152
node = stack.pop()
156153
yield node.data
157154
node = node.right
158155

159156
def children_count(self):
160-
"""
161-
Return the number of children
157+
"""Return the number of children
162158
163159
@returns number of children: 0, 1, 2
164160
"""
@@ -168,4 +164,3 @@ def children_count(self):
168164
if self.right:
169165
cnt += 1
170166
return cnt
171-

algorithms/generators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
def fib(n):
2-
"""
3-
Generator for Fibonacci serie
2+
"""Generator for Fibonacci serie.
43
54
Example: for i in fib(5): print i
65
@param n fib range upper bound
@@ -14,4 +13,3 @@ def fib(n):
1413
yield b
1514
a, b = b, a+b
1615
i += 1
17-

algorithms/list.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
def find_int(i, l):
2-
"""
3-
Find integer in a sorted list.
2+
"""Find integer in a sorted list.
43
54
Example: 4 in [1, 3, 4, 6, 7, 9] -> 2
65
@param i integer to find.
@@ -23,9 +22,9 @@ def find_int(i, l):
2322
if res:
2423
return res + p_idx
2524

25+
2626
def find_max_sub(l):
27-
"""
28-
Find subset with higest sum
27+
"""Find subset with higest sum.
2928
3029
Example: [-2, 3, -4, 5, 1, -5] -> (3,4), 6
3130
@param l list

algorithms/maze.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +0,0 @@
1-
grid = [[0, 0, 0, 0, 0, 1],
2-
[1, 1, 0, 0, 0, 1],
3-
[0, 0, 0, 1, 0, 0],
4-
[0, 1, 1, 0, 0, 1],
5-
[0, 1, 0, 0, 1, 0],
6-
[0, 1, 0, 0, 0, 2]
7-
]
8-
9-
def search(x, y):
10-
if grid[x][y] == 2:
11-
print 'found at %d,%d' % (x, y)
12-
return True
13-
elif grid[x][y] == 1:
14-
print 'wall at %d,%d' % (x, y)
15-
return False
16-
elif grid[x][y] == 3:
17-
print 'visited at %d,%d' % (x, y)
18-
return False
19-
20-
print 'visiting %d,%d' % (x, y)
21-
22-
# mark as visited
23-
grid[x][y] = 3
24-
if ((x < len(grid)-1 and search(x+1, y))
25-
or (y > 0 and search(x, y-1))
26-
or (x > 0 and search(x-1, y))
27-
or (y < len(grid)-1 and search(x, y+1))):
28-
return True
29-
30-
return False
31-
32-
search(0, 0)
33-

algorithms/performance/performance_string_matching.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

algorithms/permutations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
def permutations(l):
2-
"""
3-
Generator for list permutations
2+
"""Generator for list permutations.
43
54
@param l list to generate permutations for
65
@result yield each permutation

0 commit comments

Comments
 (0)