From 061de8a68739eb62470fa59eb1448c69500866e5 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sat, 24 May 2014 05:34:14 -0300 Subject: [PATCH] add the map pattern - 1.1.3 --- .../UNIT-01/03-problem-solving/.loops.py.swp | Bin 12288 -> 12288 bytes .../UNIT-01/03-problem-solving/loops.py | 30 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/.loops.py.swp b/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/.loops.py.swp index 33ad8d0c0cb2f4f5274863b0645080ed839bdd4d..d08176986a7487d26ef2a773c73efc7031044f9d 100644 GIT binary patch delta 425 zcmYk1!Ak-`6vp3TSwWGW?eM_B-5Nx*5`yf~r9+o8BgAmk*<5hPWp?aRq=)Vu_SU}; z1hL0%o%>^iI#dVg%?jnf4<6r}@8f%;=BSCSLha}n%lQ(}O99+mj81Ksz&*;EF3$T{l3#YAeO1mPS82ZQtjjV=8Hj!0AhmcdU3lBTzBNbQSl4y4$eATPs(XnswtwTty$~FpNLx9$4*i ReKq>i3gPRK28QskhTueoe+(_s3=FA7naPtG8C5pAtY&5m-TZ-B zfd@zl3Vi10-Neqopu`E$0u)jRXJk_1+|0;gz&3f7dfa4nEs@C!)h$$u^K(%lm0IV%D2LJ#7 diff --git a/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/loops.py b/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/loops.py index 5176536..9988023 100644 --- a/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/loops.py +++ b/archives/01-introduction-to-computer-science-and-programming/archives/UNIT-01/03-problem-solving/loops.py @@ -66,3 +66,33 @@ def extractEvens(items): ## Add isEven function def isEven(value): + if value % 2 == 0: + return True + return False + +someValues = [1,2,4,3,5,7,8,6,9] + +print extractEvens(someValues) + +## Using append method because array concatenation is very slowly for large arrays +def extractEvens(items): + evens = [] + for i in range(0, len(items), 1): + if isEven(items[i]): + evens.append(items[i]) # method invocation + return evens + +# The map pattern +def map(f, items): + result = [] + for i in range(0, len(items), 1): + transformed = f(items[i]) + result.append(transformed) + return result + +## Using this pattern +def increment(value): + return value + 1 + +someValues = [1, 2, 3, 4, 5, 6, 7] +print map(increment, someValues)