2
\$\begingroup\$

This algorithm, which I have included its flowchart as well, takes an array of A[n], which is unsorted and unmutatable and an array of B[k], which is sorted and includes n zeroes and then merges, and sorts them together. The entire code in Python and C# can be found here and on this very post.

In C#:

class Program
{
    static void Main(string[] args)
    {
        var inputA = Console.ReadLine();
        var inputB = Console.ReadLine();

        char[] delimiter = { ' ', ',', '-' };
        string[] inputASplit = inputA.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
        string[] inputBSplit = inputB.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

        int[] listA = new int[inputASplit.Length];
        int[] listB = new int[inputBSplit.Length];

        for (int i = 0; i < inputASplit.Length; i++)
        {
            listA[i] = int.Parse(inputASplit[i]);
        }

        for (int i = 0; i < inputBSplit.Length; i++)
        {
            listB[i] = int.Parse(inputBSplit[i]);
        }

        int[] concatinatedList = MergeSort(listA, listB);

        foreach (var number in concatinatedList)
        {
            Console.WriteLine(number);
        }

        Console.ReadKey();
    }

    static int[] ParseListA(int[] ListA)
    {
        List<int> newListA = ListA.ToList();
        newListA.Sort();

        return newListA.ToArray();

    }

    static int CountZeros(int[] ListB)
    {
        int zeroCount = 0;

        for (int i = 0; i < ListB.Length; i++)
        {
            if (ListB[i] == 0)
            {
                zeroCount += 1;
            }
        }

        if (zeroCount == 0)
        {
            throw new Exception("List B doesn't contain any Zeroes.");
        }

        return zeroCount;
    }

    static int[] MergeSort(int[] listA, int[] listB)
    {
        int[] newListA = ParseListA(listA);
        int zeroCount = CountZeros(listB);
        int[] newListB = new int[listB.Length - zeroCount];

        if (newListA.Length != zeroCount)
        {
            throw new Exception("There weren't enough buffers!");
        } 

        for (int i = 0; i < listB.Length; i++)
        {
            if (listB[i] != 0)
            {
                for (int j = 0; j < newListB.Length; j++)
                {
                    newListB[j] = listB[i];
                }
            }
        }

        int[] concatenatedList = newListA.Concat(newListB).ToArray();
        Array.Sort(concatenatedList);

        return concatenatedList;
    }

In Python:

def ParseListA(listA):
    newListA = sorted(listA);

    return newListA;


def MergeSort(listA, listB):
    newListA = ParseListA(listA);    
    zeroCount = 0;

    for number in listB:
        if number == 0:
            zeroCount += 1;

    if zeroCount == 0:
        raise ValueError("Needs zeros!");
        return;

    newlistB = [i for i in listB if i != 0];


    return sorted(newListA+newlistB);

def Main():
    listAInput = input("Enter List A: ").split(' ');
    listBInput = input("Enter List B: ").split(' ');

    listA = [int(i) for i in listAInput];
    listB = [int(i) for i in listBInput];

    print(MergeSort(listA, listB));

Main()
\$\endgroup\$
3
  • 3
    \$\begingroup\$ If you want visits to your repo and contributors, post an ad on Stack Overflow. If you want your code peer reviewed, include the code you want peer reviewed in your post. It's perfectly fine to include a link to your repository, but that's for additional context - the code that's up for review is the code you're embedding in your post. \$\endgroup\$ Jul 8, 2016 at 20:38
  • \$\begingroup\$ This is a very strange algorithm. I guess this isn't anything real but an exercise, is it? You'll get the same result if you just concatenate both lists and sort them. \$\endgroup\$
    – t3chb0t
    Jul 8, 2016 at 21:42
  • \$\begingroup\$ @t3chb0t: Yes, this is an exercise (albeit in futility). \$\endgroup\$
    – SamiDena
    Jul 9, 2016 at 6:37

1 Answer 1

2
\$\begingroup\$
  1. Python doesn't use semicolons at the end of a line.
  2. The function ParseListA does nothing that the built-in function sorted doesn't do already. Remove it.
  3. In MergeSort, you can use the built-in function any to make sure you have at least one zero in ListB, like this:

    if not any(i==0 for i in listB):
        raise ValueError("Needs zeros")
    

    These two lines replace all the code beginning with zeroCount=0 and ending with return;. The return statement is, incidentally, unreachable code since it follows a raise statement.

  4. At the end of MergeSort, you concatenate the two lists and then sort them. If you are going to do that, the initial sort of listA is redundant.

When all is said and done, the function ParseListA is gone and MergeSort is only four lines long. Only one sort operation is required:

def MergeSort(listA, listB):
    if not any(i==0 for i in listB):
        raise ValueError("Needs zeros")
    newB = [i for i in listB if i != 0]
    return sorted(listA + newB)
\$\endgroup\$
4
  • \$\begingroup\$ What does raise do and how is it different from 'except'? They seem to include the same methods. \$\endgroup\$
    – SamiDena
    Jul 9, 2016 at 6:38
  • \$\begingroup\$ They are more or less the opposite of each other. raise throws an exception and except handles it, using the try:except: syntax. I find your question a little strange, for two reasons: (1) you say they "seem to include the same methods" but they don't have any methods; they aren't objects but statements; (2) the raise statement was in your original code, so why was it there if you didn't know what it did? \$\endgroup\$ Jul 9, 2016 at 8:23
  • \$\begingroup\$ I googled "Python throw exception" thinking that it has a similar syntax to C# dealing with exceptions, and it came up with raise. I was under the impression that raise throws and exception, and I was kinda correct. Only kinda. \$\endgroup\$
    – SamiDena
    Jul 9, 2016 at 8:41
  • \$\begingroup\$ Ah. Java (for example) uses throw and catch but Python uses raise and except. At least they both agree on 'try' :-) \$\endgroup\$ Jul 9, 2016 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.