Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.40/5 (5 votes)
See more:
As I was attending a developer conference last week, I saw a solution to a pretty usual problem. The example was given by Venkat Subramaniam, and I really liked his way about things.
So I challenge you guys to come up with a solution to the following problem:

Your boss has a range of numbers [1..9], and want you to find all the even numbers. You are about to go for the day, so you do it simple:
C#
static void Main()
{
    var intCollection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // Finding even numbers
    Console.Write("Even numbers:");
    foreach (var i in intCollection)
        if (i % 2 == 0)
            Console.Write(" " + i);
    Console.WriteLine("");
}


Just as you are about to go out for some beers, your boss comes to you again and says he wants the odd numbers as well. So you do some refactoring, and some copy and pasting as well.
C#
static void Main()
{
    var intCollection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    FindEvenNumbers(intCollection);
    FindOddNumbers(intCollection);
}

private static void FindEvenNumbers(List<int> intCollection)
{
    Console.Write("Even numbers:");
    foreach (var i in intCollection)
        if (i % 2 == 0)
            Console.Write(" " + i);
    Console.WriteLine("");
}
private static void FindOddNumbers(List<int> intCollection)
{
    Console.Write("Odd numbers:");
    foreach (var i in intCollection)
        if (i % 2 != 0)
            Console.Write(" " + i);
    Console.WriteLine("");
}


You put on your coat and head for the door when your boss asks you for all numbers over 6. You are thinking about your mates starting to drink beer, and start questioning your self if all your education is worth another copy and paste. So what do you do to stop repeating your self? GL HF ;) Solutions will be voted for, and mr. Subramaniam's solution will also be posted if no one match it.
Posted
Updated 11-Jun-12 9:17am
v2

You could do a simple Func<> statement like so:

C#
public static void TestArray(List<int> numbers, Func<int, bool> check)
{
    foreach (int i in numbers)
    {
        if (IsAMatch(i,check))
        {
            Console.Write(" " + i);
        }
    }
    Console.WriteLine();
}

public static bool IsAMatch(int valueToCheck, Func<int, bool> check) 
{
    return check(valueToCheck);
}


Then you could call it like so:

XML
var intCollection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
TestArray(intCollection, x => x % 2 == 0);
TestArray(intCollection, x => x % 2 != 0);
TestArray(intCollection, x => x {gt} 6); 


* Having issues with the greater than in the last statement
 
Share this answer
 
v3
Comments
Amund Gjersøe 11-Jun-12 17:14pm    
You're spot on :)
Added the original solution anyhow.
Cheers
Tim Corey 11-Jun-12 18:18pm    
Thanks. I like mine a bit better because the IsAMatch method can be used with one value without calling the TestArray method with a list of one value. :-)
hi,
One solution is to create a simple sequential workflow which will have your main logic, rehost the workflow designer in a windows/WPF application and let your boss to change the condition of the workflow whenever he wants. It will work, but it is a little bit complex to implement.

Cheers
 
Share this answer
 
v3
Original solution:
C#
static void Main()
{
    var intCollection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // Finding numbers
    FindNumbers(intCollection, "Even numbers:", i => i % 2 == 0);
    FindNumbers(intCollection, "Odd numbers:", i => i % 2 != 0);
    FindNumbers(intCollection, "Numbers above 6:", i => i > 6);
            
}

       
private static void FindNumbers(List<int> intCollection, string text, Func<int,> block)
{
    Console.Write(text);
    foreach (var i in intCollection)
        if (block(i))
            Console.Write(" " + i);
    Console.WriteLine("");
}


Prints:
Even numbers: 2 4 6 8
Odd numbers: 1 3 5 7 9
Numbers above 6: 7 8 9

Good night :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900