From d08ac5167a2d8e78527a183aed98824547d71b05 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sat, 24 May 2014 06:00:41 -0300 Subject: [PATCH] add the shuffle pattern - 1.1.3 --- .../UNIT-01/03-problem-solving/.loops.py.swp | Bin 12288 -> 12288 bytes .../UNIT-01/03-problem-solving/loops.py | 33 ++++++++++++++++++ 2 files changed, 33 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 d08176986a7487d26ef2a773c73efc7031044f9d..930693d7c37e721ed6145e59de936fc6f203732a 100644 GIT binary patch delta 1027 zcmb`FKX21O7{*^mEghn&WdpIiQW$I{s_JAwikgjufdxIj`q8*Du#r-_?jdE!kQu@ytmtR*1fUGjIxy zz%#G~TA&H);O7mZ@8B~y1t*{nx}Xi};Kwr2H*gNlz*{f^yWlBsK^^>pgbM&E^P2O% zmNFZeOpIn>jCj%ErXBb@s@pFo?QBAfM{%>qem=DI8zPFN9=Q1ob$zdD6CLx-Nt}Uy-f7{K{sY?Ciw5ZDaKpu~rEEm(D_DsjK28Qa$hTueoe+(_s3=FA7naPtG8C5pAoZ)9o+AJvW znV&a|je+48J47u|NMSM~(+~E|j4WSR6BMWp3W_rGN)%EnN{SMb845Q4QfK1;02Ns! A{Qv*} 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 9988023..e091dc6 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 @@ -96,3 +96,36 @@ def increment(value): someValues = [1, 2, 3, 4, 5, 6, 7] print map(increment, someValues) + +# The shuffle pattern +## arrays with the same size +def shuffle(array1, array2): + array3 = [] + for i in range(0, len(array1), 1): + array3.append(array1[i]) + array3.append(array2[i]) + return array3 + +## arrays without the same size +def shuffle2(array1, array2): + array3 = [] + if len(array1) < len(array2): + for i in range(len(array1), 1): + array3.append(array1[i]) + array3.append(array2[i]) + return array3 + array2[i + 1:] + else: + for i in range(0, len(array2), 1): + array3.append(array1[i]) + array3.append(array2[i]) + return array3 + array1[i + 1:] + +## using a while loop instead +def shuffle3(array1, array2): + array3 = [] + i = 0 + while (i < len(array1) and i < len(array2)): + array3.append(array1[i]) + array3.append(array2[i]) + i += 1 + return array3 + array1[i:] + array2[i:]