您能提出一个明智的解决方案吗? [英] Can you come up with a slick solution?

查看:111
本文介绍了您能提出一个明智的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我参加一个开发人员会议时,我看到了一个非常常见的问题的解决方案.该示例由Venkat Subramaniam给出,我真的很喜欢他的处理方式.
因此,我向大家挑战,提出以下问题的解决方案:

您的老板有一个范围为[1..9]的数字,希望您找到所有偶数.您将要参加这一天,因此您可以轻松做到这一点:

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:

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.

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("");
}



当老板要求您提供6个以上的所有数字时,您穿上外套,直奔门.您正在考虑您的同伴开始喝啤酒,并开始质疑自己的自我,如果您所有的教育都值得另一次抄袭和粘贴.那么,您该怎么做才能停止自我重复? GL HF;)解决方案将被投票,先生.如果没有人匹配,Subramaniam的解决方案也会被发布.



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.

推荐答案

您可以执行一个简单的Func<>语句,如下所示:

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

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:

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


hi,
一种解决方案是创建一个简单的顺序工作流,该工作流将具有您的主要逻辑,将工作流设计器重新托管在Windows/WPF应用程序中,并让您的老板随时更改工作流的条件.它可以工作,但是实现起来有点复杂.

干杯

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


原始解决方案:
Original solution:
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 :)


这篇关于您能提出一个明智的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆