mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-12 04:18:08 +00:00
Fixing Some Minor Issues (#1386)
* Replacing mutable default argument in __init__ * Fixing typo mistakes in lib.py * Simplifying chained comparisons * Update lib.py
This commit is contained in:
parent
927a8c7722
commit
63d8cadc3e
|
@ -27,7 +27,7 @@ import random
|
||||||
|
|
||||||
class Vector(object):
|
class Vector(object):
|
||||||
"""
|
"""
|
||||||
This class represents a vector of arbitray size.
|
This class represents a vector of arbitrary size.
|
||||||
You need to give the vector components.
|
You need to give the vector components.
|
||||||
|
|
||||||
Overview about the methods:
|
Overview about the methods:
|
||||||
|
@ -46,11 +46,13 @@ class Vector(object):
|
||||||
TODO: compare-operator
|
TODO: compare-operator
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, components=[]):
|
def __init__(self, components=None):
|
||||||
"""
|
"""
|
||||||
input: components or nothing
|
input: components or nothing
|
||||||
simple constructor for init the vector
|
simple constructor for init the vector
|
||||||
"""
|
"""
|
||||||
|
if components is None:
|
||||||
|
components = []
|
||||||
self.__components = list(components)
|
self.__components = list(components)
|
||||||
|
|
||||||
def set(self, components):
|
def set(self, components):
|
||||||
|
@ -112,7 +114,7 @@ class Vector(object):
|
||||||
"""
|
"""
|
||||||
input: other vector
|
input: other vector
|
||||||
assumes: other vector has the same size
|
assumes: other vector has the same size
|
||||||
returns a new vector that represents the differenz.
|
returns a new vector that represents the difference.
|
||||||
"""
|
"""
|
||||||
size = len(self)
|
size = len(self)
|
||||||
if size == len(other):
|
if size == len(other):
|
||||||
|
@ -136,7 +138,7 @@ class Vector(object):
|
||||||
summe += self.__components[i] * other.component(i)
|
summe += self.__components[i] * other.component(i)
|
||||||
return summe
|
return summe
|
||||||
else: # error case
|
else: # error case
|
||||||
raise Exception("invalide operand!")
|
raise Exception("invalid operand!")
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
"""
|
"""
|
||||||
|
@ -223,7 +225,7 @@ class Matrix(object):
|
||||||
|
|
||||||
def __init__(self, matrix, w, h):
|
def __init__(self, matrix, w, h):
|
||||||
"""
|
"""
|
||||||
simple constructor for initialzes
|
simple constructor for initializing
|
||||||
the matrix with components.
|
the matrix with components.
|
||||||
"""
|
"""
|
||||||
self.__matrix = matrix
|
self.__matrix = matrix
|
||||||
|
@ -249,7 +251,7 @@ class Matrix(object):
|
||||||
"""
|
"""
|
||||||
changes the x-y component of this matrix
|
changes the x-y component of this matrix
|
||||||
"""
|
"""
|
||||||
if x >= 0 and x < self.__height and y >= 0 and y < self.__width:
|
if 0 <= x < self.__height and 0 <= y < self.__width:
|
||||||
self.__matrix[x][y] = value
|
self.__matrix[x][y] = value
|
||||||
else:
|
else:
|
||||||
raise Exception("changeComponent: indices out of bounds")
|
raise Exception("changeComponent: indices out of bounds")
|
||||||
|
@ -258,7 +260,7 @@ class Matrix(object):
|
||||||
"""
|
"""
|
||||||
returns the specified (x,y) component
|
returns the specified (x,y) component
|
||||||
"""
|
"""
|
||||||
if x >= 0 and x < self.__height and y >= 0 and y < self.__width:
|
if 0 <= x < self.__height and 0 <= y < self.__width:
|
||||||
return self.__matrix[x][y]
|
return self.__matrix[x][y]
|
||||||
else:
|
else:
|
||||||
raise Exception("changeComponent: indices out of bounds")
|
raise Exception("changeComponent: indices out of bounds")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user