(sequence, n)
| 1 | def form_ngrams(sequence, n): |
| 2 | history = [] |
| 3 | # build the first ngram, yielding only when we have a full ngram |
| 4 | while n > 1: |
| 5 | try: |
| 6 | next_item = next(sequence) |
| 7 | except StopIteration: |
| 8 | # no more data, terminate the generator |
| 9 | return |
| 10 | history.append(next_item) |
| 11 | n -= 1 |
| 12 | |
| 13 | # yield each ngram we have, then add the next item and repeat |
| 14 | for item in sequence: |
| 15 | history.append(item) |
| 16 | yield tuple(history) |
| 17 | del history[0] |
no outgoing calls
no test coverage detected