Skip to content

Commit 63d8cad

Browse files
Cor3Downcclauss
authored andcommitted
Fixing Some Minor Issues (TheAlgorithms#1386)
* Replacing mutable default argument in __init__ * Fixing typo mistakes in lib.py * Simplifying chained comparisons * Update lib.py
1 parent 927a8c7 commit 63d8cad

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

linear_algebra/src/lib.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class Vector(object):
2929
"""
30-
This class represents a vector of arbitray size.
30+
This class represents a vector of arbitrary size.
3131
You need to give the vector components.
3232
3333
Overview about the methods:
@@ -46,11 +46,13 @@ class Vector(object):
4646
TODO: compare-operator
4747
"""
4848

49-
def __init__(self, components=[]):
49+
def __init__(self, components=None):
5050
"""
5151
input: components or nothing
5252
simple constructor for init the vector
5353
"""
54+
if components is None:
55+
components = []
5456
self.__components = list(components)
5557

5658
def set(self, components):
@@ -112,7 +114,7 @@ def __sub__(self, other):
112114
"""
113115
input: other vector
114116
assumes: other vector has the same size
115-
returns a new vector that represents the differenz.
117+
returns a new vector that represents the difference.
116118
"""
117119
size = len(self)
118120
if size == len(other):
@@ -136,7 +138,7 @@ def __mul__(self, other):
136138
summe += self.__components[i] * other.component(i)
137139
return summe
138140
else: # error case
139-
raise Exception("invalide operand!")
141+
raise Exception("invalid operand!")
140142

141143
def copy(self):
142144
"""
@@ -223,7 +225,7 @@ class Matrix(object):
223225

224226
def __init__(self, matrix, w, h):
225227
"""
226-
simple constructor for initialzes
228+
simple constructor for initializing
227229
the matrix with components.
228230
"""
229231
self.__matrix = matrix
@@ -249,7 +251,7 @@ def changeComponent(self, x, y, value):
249251
"""
250252
changes the x-y component of this matrix
251253
"""
252-
if x >= 0 and x < self.__height and y >= 0 and y < self.__width:
254+
if 0 <= x < self.__height and 0 <= y < self.__width:
253255
self.__matrix[x][y] = value
254256
else:
255257
raise Exception("changeComponent: indices out of bounds")
@@ -258,7 +260,7 @@ def component(self, x, y):
258260
"""
259261
returns the specified (x,y) component
260262
"""
261-
if x >= 0 and x < self.__height and y >= 0 and y < self.__width:
263+
if 0 <= x < self.__height and 0 <= y < self.__width:
262264
return self.__matrix[x][y]
263265
else:
264266
raise Exception("changeComponent: indices out of bounds")

0 commit comments

Comments
 (0)