Skip to main content
added 147 characters in body
Source Link
Bruno Costa
  • 5.6k
  • 19
  • 42
public static IEnumerable<int> ReadIntegers(string file)
{
    using (var stream = File.OpenText(file))
    {
        string line;
        while ((line = stream.ReadLine()) != null)
        {
            yield return int.Parse(line);
        }
    }
}

public static IEnumerable<int> UnionSorted(IEnumerable<int> first, IEnumerable<int> second)
{
    var it1 = first.GetEnumerator();
    var it2 = second.GetEnumerator();

    whilebool (it1HasNext = it1.MoveNext());
    {
 bool it2HasNext = it2.MoveNext();
    ifwhile (!it2.MoveNext()it1HasNext && it2HasNext) 
 break;   {
        if (it1.Current > it2.Current)
        {
            yield return it2.Current;
            it2HasNext = it2.MoveNext();
        }
        else
        {
            yield return it1.Current;
            it1HasNext = it1.MoveNext();
        }
    }

    while (it1.MoveNext())
    {
        yield return it1.Current;
    }


    while (it2.MoveNext())
    {
        yield return it2.Current;
    }
}

public static void MergeFiles(string file1, string file2, string ouputFile)
{
    var merged = UnionSorted(ReadIntegers(file1), ReadIntegers(file2));
    using (var writer = File.AppendText(ouputFile))
    {
        foreach (var number in merged)
        {
            writer.WriteLine(number);
        }
    }
}
public static IEnumerable<int> ReadIntegers(string file)
{
    using (var stream = File.OpenText(file))
    {
        string line;
        while ((line = stream.ReadLine()) != null)
        {
            yield return int.Parse(line);
        }
    }
}

public static IEnumerable<int> UnionSorted(IEnumerable<int> first, IEnumerable<int> second)
{
    var it1 = first.GetEnumerator();
    var it2 = second.GetEnumerator();

    while (it1.MoveNext())
    {
        if (!it2.MoveNext()) break;
        if (it1.Current > it2.Current)
        {
            yield return it2.Current;
        }
        else
        {
            yield return it1.Current;
        }
    }

    while (it1.MoveNext())
    {
        yield return it1.Current;
    }


    while (it2.MoveNext())
    {
        yield return it2.Current;
    }
}

public static void MergeFiles(string file1, string file2, string ouputFile)
{
    var merged = UnionSorted(ReadIntegers(file1), ReadIntegers(file2));
    using (var writer = File.AppendText(ouputFile))
    {
        foreach (var number in merged)
        {
            writer.WriteLine(number);
        }
    }
}
public static IEnumerable<int> ReadIntegers(string file)
{
    using (var stream = File.OpenText(file))
    {
        string line;
        while ((line = stream.ReadLine()) != null)
        {
            yield return int.Parse(line);
        }
    }
}

public static IEnumerable<int> UnionSorted(IEnumerable<int> first, IEnumerable<int> second)
{
    var it1 = first.GetEnumerator();
    var it2 = second.GetEnumerator();

    bool it1HasNext = it1.MoveNext();
    bool it2HasNext = it2.MoveNext();
    while (it1HasNext && it2HasNext) 
    {
        if (it1.Current > it2.Current)
        {
            yield return it2.Current;
            it2HasNext = it2.MoveNext();
        }
        else
        {
            yield return it1.Current;
            it1HasNext = it1.MoveNext();
        }
    }

    while (it1.MoveNext())
    {
        yield return it1.Current;
    }


    while (it2.MoveNext())
    {
        yield return it2.Current;
    }
}

public static void MergeFiles(string file1, string file2, string ouputFile)
{
    var merged = UnionSorted(ReadIntegers(file1), ReadIntegers(file2));
    using (var writer = File.AppendText(ouputFile))
    {
        foreach (var number in merged)
        {
            writer.WriteLine(number);
        }
    }
}
Source Link
Bruno Costa
  • 5.6k
  • 19
  • 42

First some more general remarks on your code not yet identified:

temp = inputFile1.ReadLine();
if (temp == null)
{
    endOfFile1 = true;
}

The most reasonable way to tell if there is a new line or not usually comes into this code:

string line;
while ((line = stream.ReadLine()) != null)

Saves space, is readable, is concise. When I first started programming I always had those issues when reading line from files, from the moment I saw that I don't want another thing.

pathInputFile1 We normally suggest to use descriptive names but I admit that file1 is plenty for me.


You are having some trouble because you are doing three different tasks on your algorithm. I can identify at least three steps to achieve a solution for your problem:

  • Read the integers from the files
  • Merge the integers
  • Write the merged integers to a new file

The major problem here is that we may not want to use additional memory, because of that we need to implement all algorithms on a lazily fashion.

public static IEnumerable<int> ReadIntegers(string file)
{
    using (var stream = File.OpenText(file))
    {
        string line;
        while ((line = stream.ReadLine()) != null)
        {
            yield return int.Parse(line);
        }
    }
}

public static IEnumerable<int> UnionSorted(IEnumerable<int> first, IEnumerable<int> second)
{
    var it1 = first.GetEnumerator();
    var it2 = second.GetEnumerator();

    while (it1.MoveNext())
    {
        if (!it2.MoveNext()) break;
        if (it1.Current > it2.Current)
        {
            yield return it2.Current;
        }
        else
        {
            yield return it1.Current;
        }
    }

    while (it1.MoveNext())
    {
        yield return it1.Current;
    }


    while (it2.MoveNext())
    {
        yield return it2.Current;
    }
}

public static void MergeFiles(string file1, string file2, string ouputFile)
{
    var merged = UnionSorted(ReadIntegers(file1), ReadIntegers(file2));
    using (var writer = File.AppendText(ouputFile))
    {
        foreach (var number in merged)
        {
            writer.WriteLine(number);
        }
    }
}