c# - Facebook 面试问题 : Formatting a collection of times for a movie show time output (using Linq is preferred)

标签 c# linq facebook date

class TimeObject
{
    DateTime time;
    bool isMatinee;
}

Given: {8:00, 9:30, 11:00, 12:10, 2:00, 4:00, 5:20} -- a collection of TimeObjects

Output: (8:00AM, 9:30, 11:00, 12:10PM, 2:00), 4:00, 5:20 -- return a string, oh and AM/PM should be picked up from localization strings

Caveats: AM/PM only shown for first time, ( ) encloses those elements whose matinee bool was set to true.

问题是:我必须弄清楚如何输出上面的字符串。

我提到过,我知道 C#,面试官坚持要知道如何用最少的可读代码行来做到这一点,最好是使用 LINQ。他说,我可以将它写入控制台,但我必须记住本地化 AM 和 PM。

显然,我创建了一堆临时集合,还有废话,完全搞砸了。他声称这只是几行 LINQ。我尝试了其他东西,尽管他一直引导我使用 LINQ。

帮助?任何机构有想法?这真的让我一整天都感到畏缩。

更新 - 我找到工作了!他们还问我编辑距离 {HELLO} --> {HLO},告诉最小值。到达最终字符串所需的编辑/更新次数。世界上有蜜蜂和蜂蜜,有 1 个蜂王,获取蜂蜜的唯一途径是通过蜂王。构造一个可以支持这个的make belief计算机世界,并判断这个世界是否违反了——Graph,根节点是Queen Bee,节点是Honey和Bee,运行BiPartite测试看是否世界违反了。

最佳答案

[编辑]
我敢肯定有一种更简洁的方法可以做到这一点,但这就是我所拥有的。如果不是时间格式限制,一个简单的分组会更容易。我会尝试提出另一个版本。

var amTimes = times.Where(to => to.time.Hour < 12)
                   .Select((to, i) => new
                   {
                       to.isMatinee,
                       repr = i == 0 ? to.time.ToString("h:mmtt")
                                     : to.time.ToString("h:mm")
                   });
var pmTimes = times.Where(to => to.time.Hour >= 12)
                   .Select((to, i) => new
                   {
                       to.isMatinee,
                       repr = i == 0 ? to.time.ToString("h:mmtt")
                                     : to.time.ToString("h:mm")
                   });
var stimes = amTimes.Concat(pmTimes);
var mats = String.Join(", ", stimes.Where(t => t.isMatinee).Select(t => t.repr));
var nonmats = String.Join(", ", stimes.Where(t => !t.isMatinee).Select(t => t.repr));

var output = string.Format("({0}), {1}", mats, nonmats);

[编辑2]
好的,这很可能是面试官想要的答案。

var output = String.Join(", ",
    times.Select(to => new
    {
        prefix = to == times.First(t => t.isMatinee) ? "(" : "",
        time = to.time,
        fmt = to.time.Hour < 12
            ? (to == times.First(t => t.time.Hour < 12) ? "h:mmtt" : "h:mm")
            : (to == times.First(t => t.time.Hour >= 12) ? "h:mmtt" : "h:mm"),
        suffix = to == times.Last(t => t.isMatinee) ? ")" : "",
    })
    .Select(x => String.Format("{0}{1}{2}", x.prefix, x.time.ToString(x.fmt), x.suffix)));

通常当我写 LINQ 表达式时,我总是尽量考虑性能。由于性能不是这里的一个因素。这应该是最容易编写和遵循的(但性能很糟糕)。

方法,考虑时间、格式以及打印时的任何前缀(左括号)或后缀(右括号)。只要 isMatinee 分组是连续的(我一直假设)且时间已排序,这应该始终有效。

它只有在第一次是日场时才有前缀。如果是最后一次,则后缀是日场。如果它是各自组中的第一次,则应使用 AM/PM 进行格式化。应该很容易理解。


如果只涉及日场分组,我可能会这样做:

var output = String.Join(", ",
    times.GroupBy(to => to.isMatinee, to => to.time.ToString("h:mm"))
         .Select(g => g.Key ? "(" + String.Join(", ", g) + ")"
                            : String.Join(", ", g)));

关于c# - Facebook 面试问题 : Formatting a collection of times for a movie show time output (using Linq is preferred),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3490525/

相关文章:

ruby-on-rails - Omniauth Facebook 重定向到注册屏幕而不是创建用户

C# paypal IPN通信

c# - 如何将唯一的行号添加到我的 Linq 选择中?

c# - Entity Framework -使用存储过程急于加载对象图

c# - 组合在一起的两个可枚举的数据模板

javascript - 如何安全地获取和使用 Facebook 应用程序访问 token 以使用 PHP 和 Javascript 发送通知

c# - 如何从 datagridview 组合框获取值(value)?

c# - 在使用 LINQ 时,我们是否为了实现代码可读性而牺牲了性能?

c# - 为 Unity3d 编写最简单的 newick 解析器(c# 或 Actionscript)

ios - 我可以在 native iOS 应用程序和后端之间共享 Facebook 身份验证吗?