How do you create voids that change the value of a variable without IF? (like Python methods)

Asked

Viewed 44 times

-1

To simplify my question, I will show the Bubble Sort in python:

def bubbleSort(self):
    for i in range(len(self) - 1):
        for j in range(len(self) - 1):
            if self[j] > self[j+1]:
                self[j], self[j+1] = self[j+1], self[j]

l = [2, 8, 3, 5, 4, 6, 0, 7, 1]

bubbleSort(l)

print(l)

EXIT: [0, 1, 2, 3, 4, 5, 6, 7, 8]

I created a code in C#:

using System;

public class ListFunction : Object
{
    public static int len(var l)
    {
        int len = 0;
        foreach (var i in l)
        {
            len++;
        }
        return len;
    }

    public static void bubbleSort(int[] l)
    {
        for (int i = 0; i < len(l); i++)
        {
            for (int j = 0; i < len(l); j++)
            {
                if (l[j] > l[j + 1])
                {
                    int temp = l[j];
                    l[j] = l[j + 1];
                    l[j + 1] = temp;
                }
            }
        }
    }
}

var l = new List { 2, 8, 3, 5, 4, 6, 0, 7, 1 };

bubbleSort(l);

I did not test because I had error. Error does not let test, right?

  • You are using var at all remember that [tag:c#] is a strongly typed language and which parameters should have their type explicitly stated. Language does not accept data processing outside of class declaration and out-of-method flow control.

  • Ahh, I get it. Can you show me how to create this kind of function?

  • There are many errors in your code I will not miss my Saturday correcting them. Example the function bubbleSort() does not return a value, has no return type and in the absence of this does not have a parameter output attribute, the function len() should not exist,......

  • Okay. But I also wanted to know how you see the size of a list

  • List<T>.Count. Example: var l = new List<int> { 2, 8, 3, 5, 4, 6, 0, 7, 1 }; Console.WriteLine(l.Count);

1 answer

4


Look at this, I won’t go into detail all the mistakes, because they are too many to write one or two paragraphs about all of them, so here are just a few remarks:

public class ListFunction : Object

No need to manually extend Object, every class extends Object.

public static int len(var l)

You need to define the type of variable you will receive here, C# only infer the type of the variable if it is initialized on the same line.

public static void bubbleSort(int[] l)

Do you want to sort an array or a list? Because here you are declaring that you receive an array, but then you invoke that function by passing a list.

for (int i = 0; i < len(l); i++)

Shouldn’t be len(l) - 1, just as in Python code?

for (int j = 0; i < len(l); j++)

Shouldn’t be j instead of i?

var l = new List { 2, 8, 3, 5, 4, 6, 0, 7, 1 };

You’re not passing the kind of list to the builder, it should be List<int>, additionally you have not added the namespace of List within its scope.

bubbleSort(l);

If bubbleSort is a class method ListFunction, then you need to call it like ListFunction.bubbleSort(l)


As it should be:

using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        var l = new List<int> { 2, 8, 3, 5, 4, 6, 0, 7, 1 };
        BubbleSort(l);
    }
    
    public static void BubbleSort(List<int> l)
    {
        for (int i = 0; i < l.Count -1; i++)
        {
            for (int j = 0; j < l.Count -1; j++)
            {
                if (l[j] > l[j + 1])
                {
                    int temp = l[j];
                    l[j] = l[j + 1];
                    l[j + 1] = temp;
                }
            }
        }
    }
}

Now an example of a function that sorts any type of list and any type of value:

using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        var l = new List<int> { 2, 8, 3, 5, 4, 6, 0, 7, 1 };
        BubbleSort(l);
    }
    
    public static void BubbleSort<T>(IList<T> l)
    {
        for (int i = 0; i < l.Count -1; i++)
        {
            for (int j = 0; j < l.Count -1; j++)
            {
                if (Comparer<T>.Default.Compare(l[j], l[j + 1]) > 0)
                {
                    T temp = l[j];
                    l[j] = l[j + 1];
                    l[j + 1] = temp;
                }
            }
        }
    }
}
  • I didn’t know that guy System.Array extend the interface System.Collection.Generic.IList<T>. Ah, and could put the keyword ref in l.

  • I was a Noob the day I posted the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.