diff --git a/PhilBoothe/Math_Dojo/math-dojo-1.py b/PhilBoothe/Math_Dojo/math-dojo-1.py new file mode 100644 index 0000000..b2a456b --- /dev/null +++ b/PhilBoothe/Math_Dojo/math-dojo-1.py @@ -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 \ No newline at end of file diff --git a/PhilBoothe/Math_Dojo/math-dojo-2.py b/PhilBoothe/Math_Dojo/math-dojo-2.py new file mode 100644 index 0000000..a1d642e --- /dev/null +++ b/PhilBoothe/Math_Dojo/math-dojo-2.py @@ -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 \ No newline at end of file diff --git a/PhilBoothe/Math_Dojo/math-dojo-3.py b/PhilBoothe/Math_Dojo/math-dojo-3.py new file mode 100644 index 0000000..a9676b0 --- /dev/null +++ b/PhilBoothe/Math_Dojo/math-dojo-3.py @@ -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 \ No newline at end of file diff --git a/PhilBoothe/Underscore/test_uderscore.py b/PhilBoothe/Underscore/test_uderscore.py index a1157b6..0a88fbe 100644 --- a/PhilBoothe/Underscore/test_uderscore.py +++ b/PhilBoothe/Underscore/test_uderscore.py @@ -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] diff --git a/PhilBoothe/Underscore/underscore.py b/PhilBoothe/Underscore/underscore.py index 567c2df..0f95833 100644 --- a/PhilBoothe/Underscore/underscore.py +++ b/PhilBoothe/Underscore/underscore.py @@ -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: diff --git a/PhilBoothe/Underscore/underscore.pyc b/PhilBoothe/Underscore/underscore.pyc index efb8fef..3f9c845 100644 Binary files a/PhilBoothe/Underscore/underscore.pyc and b/PhilBoothe/Underscore/underscore.pyc differ