- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个似乎很简单的问题,我很难用代码 (C#) 建模 -
我正在尝试寻找参加 session 的人可以获得的最高潜在学分。类(class)有时间段,例如 Security 101 @ 9AM-10AM,Finance 202 @ 4PM-6PM 等。
主要规则是,您不能同时参加两门类(class) - 因此您将获得 9-10 和 10-11 类(class)的学分,但您不能同时获得 9-11 类(class)的学分.
我想做的是:
我想在一天内获取一组有效(有效意味着不重叠)路径。
因此,例如,一天的全套类(class)可能如下:
|---------------------------------------------------|
| COURSE | START | END |
|-------------------|---------------|---------------|
| FINANCE 101 | 9:00 AM | 10:00 AM |
| FINANCE 102 | 10:00 AM | 11:00 AM |
| PYTHON 300 | 10:00 AM | 11:00 AM |
| SECURITY 101 | 11:00 AM | 12:00 PM |
| ECONOMICS 101 | 9:00 AM | 12:00 PM |
| DATABASE 200 | 11:00 AM | 1:00 PM |
|---------------------------------------------------|
有人可能会在这一天走几条路:
财务 101 (9-10) -> 财务 102 (10-11) -> 安全 101 (11-12) -> 完成
金融 101 (9-10) -> PYTHON 300 (10-11) -> 安全 101 (11-12) -> 完成
财务 101 (9-10) -> 财务 102 (10-11) -> 数据库 200 (11-1) -> 完成
金融 101 (9-10) -> PYTHON 300 (10-11) -> 数据库 200 (11-1) -> 完成
经济学 101 (9-12)-> 完成
这是一个有点简单的场景,实际上可能有多个分支场景,例如三个朝九晚十的类(class)会在此基础上创建更多排列。
我想要一系列路径(而不是一个单一的最优路径)的原因是因为不一定有直接的 1 小时 = 1 学分相关性,会有基于路径集的二级计算对路径的学时值求和以确定什么是“最佳”。
我的问题是 - 是否有一种技术或软件模式可供我遵循以生成这些排列,以便我可以衡量结果以确定可为类(class)学习者带来最多学分的路径?
感谢大家的投入和帮助,这两个解决方案都来自 Bradley Uffner和 Xiaoy312搞定了!
最佳答案
答案改编自Ordered Permutation of List<Int> :
public static class CourseExtensions
{
public static IEnumerable<IEnumerable<Course>> GetPermutations(this IEnumerable<Course> courses)
{
return GetCoursesHelper(courses, TimeSpan.Zero);
}
private static IEnumerable<IEnumerable<Course>> GetCoursesHelper(IEnumerable<Course> courses, TimeSpan from)
{
foreach (var course in courses)
{
if (course.Start < from) continue;
yield return new[] { course };
var permutations = GetCoursesHelper(courses, course.End);
foreach (var subPermutation in permutations)
{
yield return new[]{ course }.Concat(subPermutation);
}
}
}
}
完整代码:
void Main()
{
foreach (var courses in GetCourses().GetPermutations())
{
Console.WriteLine(string.Join(" -> ", courses
.Select(x => x.ToString())
.Concat(new [] { "DONE" })));
}
}
// Define other methods and classes here
public class Course
{
public string Name { get; set; }
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
public override string ToString()
{
return string.Format("{0} ({1:hhmm}-{2:hhmm})",
Name, Start, End);
}
}
IEnumerable<Course> GetCourses()
{
var data = @"
| FINANCE 101 | 9:00 AM | 10:00 AM |
| FINANCE 102 | 10:00 AM | 11:00 AM |
| PYTHON 300 | 10:00 AM | 11:00 AM |
| SECURITY 101 | 11:00 AM | 12:00 PM |
| ECONOMICS 101 | 9:00 AM | 12:00 PM |
| DATABASE 200 | 11:00 AM | 1:00 PM |
".Trim();
return data.Split('\n')
.Select(r => r.Split('|').Select(c => c.Trim()).ToArray())
.Select(x => new Course
{
Name = x[1],
Start = DateTime.ParseExact(x[2], "h:mm tt", CultureInfo.InvariantCulture).TimeOfDay,
End = DateTime.ParseExact(x[3], "h:mm tt", CultureInfo.InvariantCulture).TimeOfDay
});
}
public static class CourseExtensions
{
public static IEnumerable<IEnumerable<Course>> GetPermutations(this IEnumerable<Course> courses)
{
return GetCoursesHelper(courses, TimeSpan.Zero);
}
private static IEnumerable<IEnumerable<Course>> GetCoursesHelper(IEnumerable<Course> courses, TimeSpan from)
{
foreach (var course in courses)
{
if (course.Start < from) continue;
yield return new[] { course };
var permutations = GetCoursesHelper(courses, course.End);
foreach (var subPermutation in permutations)
{
yield return new[]{ course }.Concat(subPermutation);
}
}
}
}
输出:
FINANCE 101 (0900-1000) -> DONE
FINANCE 101 (0900-1000) -> FINANCE 102 (1000-1100) -> DONE
FINANCE 101 (0900-1000) -> FINANCE 102 (1000-1100) -> SECURITY 101 (1100-1200) -> DONE
FINANCE 101 (0900-1000) -> FINANCE 102 (1000-1100) -> DATABASE 200 (1100-1300) -> DONE
FINANCE 101 (0900-1000) -> PYTHON 300 (1000-1100) -> DONE
FINANCE 101 (0900-1000) -> PYTHON 300 (1000-1100) -> SECURITY 101 (1100-1200) -> DONE
FINANCE 101 (0900-1000) -> PYTHON 300 (1000-1100) -> DATABASE 200 (1100-1300) -> DONE
FINANCE 101 (0900-1000) -> SECURITY 101 (1100-1200) -> DONE
FINANCE 101 (0900-1000) -> DATABASE 200 (1100-1300) -> DONE
FINANCE 102 (1000-1100) -> DONE
FINANCE 102 (1000-1100) -> SECURITY 101 (1100-1200) -> DONE
FINANCE 102 (1000-1100) -> DATABASE 200 (1100-1300) -> DONE
PYTHON 300 (1000-1100) -> DONE
PYTHON 300 (1000-1100) -> SECURITY 101 (1100-1200) -> DONE
PYTHON 300 (1000-1100) -> DATABASE 200 (1100-1300) -> DONE
SECURITY 101 (1100-1200) -> DONE
ECONOMICS 101 (0900-1200) -> DONE
DATABASE 200 (1100-1300) -> DONE
关于c# - 如何获得一系列时间 block 的所有非重叠排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005810/
我正在为我的雇主编写脚本,以从他们自己的站点获取某些数据。出于一长串原因,我需要从网站上获取数据,如图所示。我发现,其中一些数据是通过 js 调用检索的... 回想起来,我应该选择 Mechanize
我正在使用 python 和 cryptography.io 来签署和验证消息。我可以通过以下方式获得签名的 DER 编码字节表示: cryptography_priv_key.sign(messag
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否可以区分 ECDF?以下面得到的为例。 set.seed(1) a <- sort(rnorm(100)) b <- ecdf(a) plot(b) 我想对 b 求导以获得它的概率密度函数 (PD
我找到了如何从 navigator.mimeTypes 获取 mimetypes: function GetMimeTypes() { var message = ""; var mi
我在表单中使用单选按钮来隐藏/显示联系人表单中的成员 ID 字段。问题是,当 javascript 更改 html 中包含的隐藏 id 字段(该字段设置为“无”值)时,该字段将不再通过 post 可用
我正在做单元测试。我必须测试所有可能的if..else情况。但是在此if语句中: int32_t i32Res = snprintf(buffer, len, "The%d_String_%d", 0
我有一个 Facebook 应用程序,我想从中获取“喜欢”的总数。我想知道这是否可能。 其中 ID 是应用程序的 ID,ACCESS_TOKEN 是我尝试过的应用程序的当前访问 token : gra
如果我有多个计算实例尝试同时获取同一个 blob 的租约,则似乎经常会成功。我的印象是,一旦租约发出(并因此被客户获得),就不可能同时发出另一个租约? 我希望情况确实如此,我一直在 Azure 中使用
这是我的索引 POST /blogs/1 { "name" : "learn java", "popularity" : 100 } POST /blogs/2 { "name" : "l
我正在将 Symfony2 与 FOSUserBundle 一起使用。我需要为用户获得最高角色。 role_hierarchy: ROLE_CONTRIBUTOR: ROLE_USER
我正在向服务器发送基于 REST 的请求。我希望尽快得到答复,并希望了解可以进行的各种优化。 一种方法当然是在线程中并行发送这些请求。还有哪些其他选项可用于优化此功能? 在服务器上,可以添加哪些配置?
这可能是某种重复的问题,但我似乎找不到合适的解决方案。我正在使用 git4idea.history.GitHistoryUtils.history() 获取提交列表。如果 checkout 其中一个较
我正在做一个程序,可以输入每周的工资和那一周的总工作时间。它应该以小时工资率显示答案。但是我无法显示正确的“centavos/2 decimal places”公式并且它不想使用 float % fl
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我已经尝试了 mContext.getMainLooper() 和 Looper.getMainLooper()。两者都返回相同的结果,但我想知道哪种方法正确? 我还从 Android 开发人员链接中
我有一个“affiliates”表,其中包含“user”和“referredBy”列。 给定一个用户,我希望获得该用户推荐的所有“n 级”玩家。对于 n=1,我们只关心您直接推荐的玩家数量: SELE
我在 PostgreSQL 9.5 数据库中有两个表: project - id - name task - id - project_id - name - updated_
请帮助我怎样才能得到我预期的结果,在此先感谢并抱歉我的英语不好。 PHP: $dog = implode(',', $data['dogbreed']); $query .= "AND `do
我有 let impulse = CGVectorMake(CGFloat(Constants.impulse), 0) 如何在不创建另一个 CGVector 的情况下得到它的负值? 我正在考虑在 C
我是一名优秀的程序员,十分优秀!