确定给定的日期范围是否有模式
我正在一个项目中处理一个算法,我需要遍历一系列日期(已排序),并且需要检查日期是否遵循特定模式。以下是要检测的所有可能模式的列表:
- 无(未检测到任何模式。只是随机的一组日期)
- 连续 4 个星期一连续
- 4 个星期二连续
- 4 个星期三连续
- 4 个星期四或更多连续
- 4 个周五或以上 连续
- 4 个周六或以上 连续
- 4 个周日或以上
- 周一、周二、周三和周四一起
- 周二、周三、周四和周五一起
- 周三、周四、周五和周六在一起
- 周四、周五、周六和周日在一起
- 每隔周一
- 每隔周二
- 每隔周三
- 每隔周四
- 每隔周五
- 每隔周六
- 每隔周日
所有这些模式都可以通过枚举来表示。我需要通过函数检测用户给定日期范围匹配的模式。
现在,每个模式还附加了一个条件。例如:
连续 4 个星期一要求日期集合必须至少有 4 个日期,如果超过 4 个,则日期总数必须为 true,如下所示 -> 日期数量 MOD 4 == 0
周一、周二、周三和周四一起要求日期收集至少有 4 个没有间隙的日期,如果超过 4 个,则日期总数必须为真,如 -> 日期数量 MOD 4 == 0
如有任何帮助,我们将不胜感激。问候。
I'm dealing with an algorithm in a project where I need to traverse a range of date (sorted) and I need to check if the dates follow a specific pattern. Here is the list of all possible patterns to detect:
- None (No pattern detected. Just a random bunch of dates)
- 4 Mondays in a row
- 4 Tuesdays in a row
- 4 Wednesdays in a row
- 4 Thursdays or more in a row
- 4 Fridays or more in a row
- 4 Saturdays or more in a row
- 4 Sundays or more in a row
- Mon, Tue, Wed, and Thu together
- Tue, Wed, Thu, and Fri together
- Wed, Thu, Fri, and Sat together
- Thu, Fri, Sat, and Sun together
- Every other Monday
- Every other Tuesday
- Every other Wednesday
- Every other Thursday
- Every other Friday
- Every other Saturday
- Every other Sunday
All these patterns can be a represent through an Enum. I need to detect through a function, the pattern that the user given date range matches to.
Now, each pattern has a condition attached as well. For example:
4 Mondays in a row requires that the date collection must have at least 4 dates and if there are over four, the total number of dates must be a true as -> number of dates MOD 4 == 0
Mon, Tue, Wed, and Thu together requires date collection to have at least 4 dates without gaps and if there are over four, the total number of dates must be a true as -> number of dates MOD 4 == 0
Any help would be appreciated. Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您想要匹配的每个模式创建一个类(所有类都从基类继承),使用类似“feed”的方法将日期传递给它,通过所有这些模式类运行数据,然后进行某种评估模式类上的方法会告诉您是否满足条件
Create a class for each pattern that you want to match (all classes inherit from a base), have something like a "feed" method to pass a date to it, run the data through all those pattern classes, then have some kind of evaluate method on the pattern classes that will tell you if the criteria was met