-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from pfb3cn/master
added Math_Dojo; improved _.reduce in underscore.py
- Loading branch information
Showing
6 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.