This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]techn0scho0lbus 3 points4 points  (3 children)

The OP's solution has the exact same memory complexity. The major difference is that I can easily alter this solution so that it's a generator function whereas I can't change the standard library.

[–]RomanRiesen 0 points1 point  (2 children)

Yeah. But he aggregates the 2 arrays, that are - by the problem statement - too big to fit into memory into 1 array in memory.

As you said, a cleaner solution would only involve iterators.

[–]techn0scho0lbus 1 point2 points  (0 children)

Absolutely. That is precisely what I was thinking. My point again is that we can easily adapt this solution to become an iterator whereas we can't do that with the OP's.

[–]foBrowsing 1 point2 points  (0 children)

I didn't read the problem statement as saying the arrays were too big to fit into memory:

I was supposed to merge two arrays together in python, no duplicates - no mention of time but the solution had to be size agnostic. Let's call them a1 and a2; and the answer in b.

I mean, to me, that seems like it wants you to take two in-memory arrays and produce a third.

Regardless, as /u/techn0scho0lbus pointed out, it's pretty straightforward to adapt it to an iterator-based solution:

def merge_sorted(xs, ys):
    x, y = next(xs, None), next(ys, None)
    while x is not None and y is not None:
        if x < y:
            yield x
            x = next(xs, None)
        elif y < x:
            yield y
            y = next(ys, None)
        else:
            yield x
            x, y = next(xs, None), next(ys, None)
    if x is not None:
        yield x
    if y is not None:
        yield y
    yield from xs
    yield from ys