Skip to content

adding doctests on coin_change.py and fixed some typos #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions dynamic_programming/coin_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
22 changes: 12 additions & 10 deletions linear_algebra/src/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand Down