Skip to content
Snippets Groups Projects
Commit d60946d9 authored by René Nussbaumer's avatar René Nussbaumer
Browse files

utils.algo: Add a function to insert a list into a list


Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 3f006be3
No related branches found
No related tags found
No related merge requests found
......@@ -148,6 +148,27 @@ def InvertDict(dict_in):
return dict(zip(dict_in.values(), dict_in.keys()))
def InsertAtPos(src, pos, other):
"""Inserts C{other} at given C{pos} into C{src}.
@note: This function does not modify C{src} in place but returns a new copy
@type src: list
@param src: The source list in which we want insert elements
@type pos: int
@param pos: The position where we want to start insert C{other}
@type other: list
@param other: The other list to insert into C{src}
@return: A copy of C{src} with C{other} inserted at C{pos}
"""
new = src[:pos]
new.extend(other)
new.extend(src[pos:])
return new
class RunningTimeout(object):
"""Class to calculate remaining timeout when doing several operations.
......
......@@ -236,6 +236,16 @@ class TestInvertDict(unittest.TestCase):
{ 1: "foo", 2: "bar", 5: "baz"})
class TestInsertAtPos(unittest.TestCase):
def test(self):
a = [1, 5, 6]
b = [2, 3, 4]
self.assertEqual(algo.InsertAtPos(a, 1, b), [1, 2, 3, 4, 5, 6])
self.assertEqual(algo.InsertAtPos(a, 0, b), b + a)
self.assertEqual(algo.InsertAtPos(a, len(a), b), a + b)
self.assertEqual(algo.InsertAtPos(a, 2, b), [1, 5, 2, 3, 4, 6])
class TimeMock:
def __init__(self, values):
self.values = values
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment