implement 'util.unique()'

This commit is contained in:
Mike Fährmann 2020-10-29 23:26:11 +01:00
parent 558cde139c
commit c3f01dc4e6
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 21 additions and 1 deletions

View File

@ -48,12 +48,22 @@ def bdecode(data, alphabet="0123456789"):
def advance(iterable, num):
""""Advance the iterable by 'num' steps"""
""""Advance 'iterable' by 'num' steps"""
iterator = iter(iterable)
next(itertools.islice(iterator, num, num), None)
return iterator
def unique(iterable):
"""Yield unique elements from 'iterable' while preserving order"""
seen = set()
add = seen.add
for element in iterable:
if element not in seen:
add(element)
yield element
def raises(cls):
"""Returns a function that raises 'cls' as exception"""
def wrap(*args):

View File

@ -446,6 +446,16 @@ class TestOther(unittest.TestCase):
self.assertCountEqual(
util.advance(util.advance(items, 1), 2), range(3, 5))
def test_unique(self):
self.assertSequenceEqual(
list(util.unique("")), "")
self.assertSequenceEqual(
list(util.unique("AABBCC")), "ABC")
self.assertSequenceEqual(
list(util.unique("ABABABCAABBCC")), "ABC")
self.assertSequenceEqual(
list(util.unique([1, 2, 1, 3, 2, 1])), [1, 2, 3])
def test_raises(self):
func = util.raises(Exception)
with self.assertRaises(Exception):