Added group and test for group.

This commit is contained in:
Jeremy Fincher 2003-08-11 07:12:41 +00:00
parent eb6fb90801
commit 2760c1bce0
2 changed files with 23 additions and 15 deletions

View File

@ -233,21 +233,22 @@ def ilen(iterator):
i += 1
return i
## def group(seq, groupSize):
## L = []
## LL = []
## i = groupSize
## for elt in seq:
## if i > 0:
## LL.append(elt)
## i -= 1
## else:
## L.append(LL)
## i = groupSize
## LL = []
## if LL:
## L.append(LL)
## return L
def group(seq, groupSize):
ret = []
L = []
i = groupSize
for elt in seq:
if i > 0:
L.append(elt)
else:
ret.append(L)
i = groupSize
L = []
L.append(elt)
i -= 1
if L:
ret.append(L)
return ret
def itersplit(iterable, isSeparator, yieldEmpty=False):
acc = []

View File

@ -49,6 +49,13 @@ class FunctionsTest(unittest.TestCase):
for elt in reviter([]):
self.fail('reviter caused iteration over empty sequence')
def testGroup(self):
s = '1. d4 d5 2. Nf3 Nc6 3. e3 Nf6 4. Nc3 e6 5. Bd3 a6'
self.assertEqual(group(s.split(), 3)[:3],
[['1.', 'd4', 'd5'],
['2.', 'Nf3', 'Nc6'],
['3.', 'e3', 'Nf6']])
def testWindow(self):
L = range(10)
def wwindow(*args):