diff --git a/src/fix.py b/src/fix.py index 4bac4c264..d198c4ce1 100644 --- a/src/fix.py +++ b/src/fix.py @@ -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 = [] diff --git a/test/test_fix.py b/test/test_fix.py index c22a2aadd..703b9bd26 100644 --- a/test/test_fix.py +++ b/test/test_fix.py @@ -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):