Wednesday 30 October 2013

Interview Series - Delegates & LINQ Extensions



Introduction & Description: Coming shorty..

Please check out the 7 video links where I go through the demo in full detail.


 C# Experiments: Delegates & LINQ Extensions (Part 1)



C# Experiments: Delegates & LINQ Extensions (Part 2)



C# Experiments: Delegates & LINQ Extensions (Part 3)



C# Experiments: Delegates & LINQ Extensions (Part 4)



C# Experiments: Delegates & LINQ Extensions (Part 5)



C# Experiments: Delegates & LINQ Extensions (Part 6)



C# Experiments: Delegates & LINQ Extensions (Part 7)



The code typed-in during the interview series is as follows for your reference:-

    //num = 100 => 10 * 10, 5 * 20, 25 * 4, etc.
    //var numbers = new List<int>();

    //A Delegate is a pointer to a method.
    //It is a type that defines a method signature
    //Delegates are used to pass methods as arguments to other methods.

    //Func: 0 to 16 Inputs; 1 Output
    //Select, Where, GroupBy, Average, etc.

    //Action: 1 to 16 Inputs, 0 Output
    //ForEach, etc.

    //Predicate: 1 Input; 1 bool Output
    //Func<T, bool>
    //Find, FindAll, Exists, etc.

    //Lambda Expressions are nameless functions
    //numbers.Exists(x => x > 5);

    //numbers.Exists((x) =>
    //{
    //    return x > 5;
    //});

    var primeFunc = new Func<int, bool>(num =>
    {
        if (num <= 1 || (num != 2 && num % 2 == 0))
            return false;

        for (int i = 3; i <= Math.Sqrt(num); i += 2)
            if (num % i == 0)
                return false;

        return true;
    });


    //int num = 215;
    //bool isPrime = true;

    //if (num <= 1 || (num != 2 && num % 2 == 0))
    //    isPrime = false;

    //for (int i = 3; i <= Math.Sqrt(num); i += 2)
    //{
    //    if (num % i == 0)
    //        isPrime = false;
    //}

    //MessageBox.Show(String.Concat(num, " is ", isPrime ? "Prime" : "not Prime"));

    //var digits = 0;

    //for (; num != 0; digits++)
    //    num /= 10;

    var digitFunc = new Func<int, int>(num =>
    {
        var digits = 0;

        for (; num != 0; digits++)
            num /= 10;

        return digits;
    });

    //MessageBox.Show(digits.ToString());

    //Input: 1 million random numbers between 0 & 10 million

    //Problem Statement:-
    //Find all the prime numbers in the Input.
    //Group them by the number of digits they have.
    //Order the groups in Ascending Order.
    //Order the numbers in each group in Ascending Order.

    //Output: List<List<int>> primes

    var rand = new Random();
    var numbers = new HashSet<int>();

    while (numbers.Count != 1000000)
        numbers.Add(rand.Next(0, 10000000));

    var sw = Stopwatch.StartNew();
    var primes = numbers.AsParallel().Where(primeFunc).GroupBy(digitFunc).
                    OrderBy(x => x.Key).Select(group => group.OrderBy(x => x).ToList()).ToList();

    sw.Stop();
    MessageBox.Show("Elapsed Milliseconds: " + sw.ElapsedMilliseconds);


Thank you for reading this post and watching the videos. Please Subscribe, Comment and Rate the Channel if you liked the videos.

Goto C# Experiments to access more of such content! Thanks again!

No comments:

Post a Comment