diff --git a/dynamic_programming/coin_change.py b/dynamic_programming/coin_change.py index a85d8e08dbb1..12ced411780f 100644 --- a/dynamic_programming/coin_change.py +++ b/dynamic_programming/coin_change.py @@ -8,6 +8,18 @@ def dp_count(S, m, n): + """ + >>> dp_count([1, 2, 3], 3, 4) + 4 + >>> dp_count([1, 2, 3], 3, 7) + 8 + >>> dp_count([2, 5, 3, 6], 4, 10) + 5 + >>> dp_count([10], 1, 99) + 0 + >>> dp_count([4, 5, 6], 3, 0) + 1 + """ # table[i] represents the number of ways to get to amount i table = [0] * (n + 1) @@ -24,7 +36,7 @@ def dp_count(S, m, n): return table[n] - if __name__ == "__main__": - print(dp_count([1, 2, 3], 3, 4)) # answer 4 - print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5 + import doctest + + doctest.testmod() diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 5ce0f696ad71..bf9e0d302a89 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -27,7 +27,7 @@ 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. Overview about the methods: @@ -37,7 +37,7 @@ class Vector(object): __str__() : toString method component(i : int): gets the i-th component (start by 0) __len__() : gets the size of the vector (number of components) - euclidLength() : returns the eulidean length of the vector. + euclidLength() : returns the euclidean length of the vector. operator + : vector addition operator - : vector subtraction operator * : scalar multiplication and dot product @@ -46,11 +46,13 @@ class Vector(object): TODO: compare-operator """ - def __init__(self, components=[]): + def __init__(self, components=None): """ input: components or nothing simple constructor for init the vector """ + if components is None: + components = [] self.__components = list(components) def set(self, components): @@ -86,9 +88,9 @@ def __len__(self): """ return len(self.__components) - def eulidLength(self): + def euclidLength(self): """ - returns the eulidean length of the vector + returns the euclidean length of the vector """ summe = 0 for c in self.__components: @@ -112,7 +114,7 @@ def __sub__(self, other): """ input: other vector 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) if size == len(other): @@ -136,7 +138,7 @@ def __mul__(self, other): summe += self.__components[i] * other.component(i) return summe else: # error case - raise Exception("invalide operand!") + raise Exception("invalid operand!") def copy(self): """ @@ -223,7 +225,7 @@ class Matrix(object): def __init__(self, matrix, w, h): """ - simple constructor for initialzes + simple constructor for initializing the matrix with components. """ self.__matrix = matrix @@ -249,7 +251,7 @@ def changeComponent(self, x, y, value): """ 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 else: raise Exception("changeComponent: indices out of bounds") @@ -258,7 +260,7 @@ def component(self, x, y): """ 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] else: raise Exception("changeComponent: indices out of bounds")