Commit 9f5a3645 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

utils: Add function to find duplicates in sequence


Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarAdeodato Simo <dato@google.com>
parent 049383d9
......@@ -2196,6 +2196,29 @@ def UniqueSequence(seq):
return [i for i in seq if i not in seen and not seen.add(i)]
def FindDuplicates(seq):
"""Identifies duplicates in a list.
Does not preserve element order.
@type seq: sequence
@param seq: Sequence with source elements
@rtype: list
@return: List of duplicate elements from seq
"""
dup = set()
seen = set()
for item in seq:
if item in seen:
dup.add(item)
else:
seen.add(item)
return list(dup)
def NormalizeAndValidateMac(mac):
"""Normalizes and check if a MAC address is valid.
......
......@@ -1337,6 +1337,37 @@ class TestUniqueSequence(unittest.TestCase):
self._test(["a", "b", "a"], ["a", "b"])
class TestFindDuplicates(unittest.TestCase):
"""Test case for FindDuplicates"""
def _Test(self, seq, expected):
result = utils.FindDuplicates(seq)
self.assertEqual(result, utils.UniqueSequence(result))
self.assertEqual(set(result), set(expected))
def test(self):
self._Test([], [])
self._Test([1, 2, 3], [])
self._Test([9, 8, 8, 0, 5, 1, 7, 0, 6, 7], [8, 0, 7])
for exp in [[1, 2, 3], [3, 2, 1]]:
self._Test([1, 1, 2, 2, 3, 3], exp)
self._Test(["A", "a", "B"], [])
self._Test(["a", "A", "a", "B"], ["a"])
self._Test("Hello World out there!", ["e", " ", "o", "r", "t", "l"])
self._Test(self._Gen(False), [])
self._Test(self._Gen(True), range(1, 10))
@staticmethod
def _Gen(dup):
for i in range(10):
yield i
if dup:
for _ in range(i):
yield i
class TestFirstFree(unittest.TestCase):
"""Test case for the FirstFree function"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment