Skip to content

Commit

Permalink
Merge pull request #6 from pfb3cn/master
Browse files Browse the repository at this point in the history
added Math_Dojo; improved _.reduce in underscore.py
  • Loading branch information
nguyenhmp authored Aug 4, 2016
2 parents 02f9ad1 + 9940db6 commit 1e769cd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
14 changes: 14 additions & 0 deletions PhilBoothe/Math_Dojo/math-dojo-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#basic MathDojo class with add and subtract methods
class MathDojo(object):
def __init__(self):
self.result = 0
def add(self, *args):
self.result += sum(args)
return self
def subtract(self, *args):
self.result -= sum(args)
return self

#test class methods with md instance
md = MathDojo()
print md.add(2).add(2,5).subtract(3,2).result
22 changes: 22 additions & 0 deletions PhilBoothe/Math_Dojo/math-dojo-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#modify MathDojo to accept integers or lists
class MathDojo(object):
def __init__(self):
self.result = 0
def add(self, *args):
for val in args:
if type(val) is int:
self.result += val
elif type(val) is list:
self.result += sum(val)
return self
def subtract(self, *args):
for val in args:
if type(val) is int:
self.result -= val
elif type(val) is list:
self.result -= sum(val)
return self

#test class methods with md instance
md = MathDojo()
print md.add([1],3,4).add([3, 5, 7, 8], [2, 4.3, 1.25]).subtract(2, [2,3], [1.1, 2.3]).result
26 changes: 26 additions & 0 deletions PhilBoothe/Math_Dojo/math-dojo-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#modify MathDojo to accept integers, lists, AND tuples
class MathDojo(object):
def __init__(self):
self.result = 0
def add(self, *args):
for val in args:
if type(val) is int:
self.result += val
elif type(val) is list:
self.result += sum(val)
elif type(val) is tuple:
self.result += sum(val)
return self
def subtract(self, *args):
for val in args:
if type(val) is int:
self.result -= val
elif type(val) is list:
self.result -= sum(val)
elif type(val) is tuple:
self.result -= sum(val)
return self

#test class methods with md instance
md = MathDojo()
print md.add([1],3,4).add([3, 5, 7, 8], [2, 4.3, 1.25]).subtract(2, [2,3], [1.1, 2.3]).result
2 changes: 1 addition & 1 deletion PhilBoothe/Underscore/test_uderscore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from underscore import _

print _.map([1,2,3], lambda x : x**2) # [1,4,9]
print _.reduce([2,4,6], lambda x : x*-1) # -12
print _.reduce([2,4,6], lambda x,memo : memo * x, 1) # -12
print _.find([1,3,6,8,9], lambda x : x%4==0) # 8
print _.filter([1,3,6,8,9], lambda x: x%5==0) # None
print _.reject([1,3,6,8,9], lambda x: x%3==0) # [1,8]
2 changes: 1 addition & 1 deletion PhilBoothe/Underscore/underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def map(self, lis, func):
def reduce(self, lis, func, memo = 0):
memo = memo
for val in lis:
memo += func(val)
memo = func(val, memo)
return memo
def find(self, lis, cond):
for val in lis:
Expand Down
Binary file modified PhilBoothe/Underscore/underscore.pyc
Binary file not shown.

0 comments on commit 1e769cd

Please sign in to comment.