gpt4 book ai didi

algorithm - 查找日期是否在另一个日期之间的两个周末范围内

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:05:59 27 4
gpt4 key购买 nike

我正在制作一个为用户提供折扣的应用程序,其中一个折扣是在他的生日那天提供的。由于仅在您的生日那天很难使用折扣,我们决定整周都提供折扣,包括前后两个周末。

一些规则

  • 给定生日,折扣将从生日的前一个星期五开始;
  • 折扣将在开始 10 天后的星期日结束;
  • 如果生日是星期五,仍然取前一个星期五;

好的,所以我们需要一个接收 birthdayDate 的方法和 todayDate并返回 true 或 false,判断日期是否在生日范围内。

问题

在您开始查看接近年份变化的日期之前,一切看起来都很好。
如果您的生日是2018年12月31日,您可以在2019年1月6日使用优惠,同理,如果您的生日是2019年1月1日,则在2018年12月28日就可以使用优惠。
所以只看当年的生日是不够的,而且由于每年的星期几都在变化,所以今年你生日的最后一个星期五不会是下一个的同一天。

是否有一种优雅的方法可以轻松找到此范围并创建返回 true 或 false 的方法?

真正的问题是找到哪个生日是相关生日,因为您可以在上一个生日之后和下一个生日之前获得折扣。

原代码是PHP的,但是因为是算法题,我可以接受任何语言的答案

测试用例

| Today             | Birth             | Output |
|-------------------|-------------------|:------:|
| February 15, 2019 | February 22, 2000 | true |
| February 24, 2019 | February 22, 2000 | true |
| February 25, 2019 | February 22, 2000 | false |
| December 28, 2018 | January 03, 2000 | true |
| December 27, 2018 | January 03, 2000 | false |
| December 27, 2019 | January 03, 2000 | true |
| January 01, 2019 | January 03, 2000 | true |
| January 01, 2019 | December 31, 2000 | true |
| January 01, 2019 | December 28, 2000 | false |

最佳答案

这是一些使用我的 Noda Time 的 C# 代码通过您指定的所有测试的库。

基本思想很简单:

  • 从今天回溯三个月
  • 找到那个日期之后的生日
  • 构建生日前后的折扣期
  • 检查今天的日期是否在那个时期

三个月的选择有些武断;它只是为了解决您刚刚过生日的情况。 (有可能超过 10 天且少于 355 天都可以。我只是觉得 3 个月更容易推理。)

using System;
using NodaTime;
using NodaTime.Text;

class Test
{
static void Main()
{
RunTest("2019-02-15", "2000-02-22", true);
RunTest("2019-02-24", "2000-02-22", true);
RunTest("2019-02-25", "2000-02-22", false);
RunTest("2018-12-28", "2000-01-03", true);
RunTest("2019-01-01", "2000-01-03", true);
RunTest("2018-12-27", "2000-01-03", false);
RunTest("2019-12-27", "2000-01-03", true);
}

static void RunTest(string todayText, string birthdayText, bool expectedResult)
{
var pattern = LocalDatePattern.Iso;
RunTest(pattern.Parse(todayText).Value,
pattern.Parse(birthdayText).Value,
expectedResult);
}

static void RunTest(LocalDate today, LocalDate birthday, bool expectedResult)
{
// Work out "the birthday that comes after 3 months ago".
// That can be:
// - A recent birthday before the closest birthday discount period,
// in which case the *next* birthday will be after the current closest
// discount period
// - A birthday *in* the current closest birthday discount period
// - A birthday *after* the current closest birthday discount period,
// in which case the *previous* birthday was before the current
// closest discount period
LocalDate threeMonthsAgo = today.PlusMonths(-3);
int ageThreeMonthsAgo = Period.Between(birthday, threeMonthsAgo).Years;

// Note: this will use Feb 28th for a Feb 29th birthday in a non leap year.
LocalDate relevantBirthday = birthday.PlusYears(ageThreeMonthsAgo + 1);

// Find the strictly-previous Friday to start the discount interval
LocalDate discountStart = relevantBirthday.With(DateAdjusters.Previous(IsoDayOfWeek.Friday));
LocalDate discountEndInclusive = discountStart.PlusDays(9);
DateInterval discountInterval = new DateInterval(discountStart, discountEndInclusive);

bool actualResult = discountInterval.Contains(today);
Console.WriteLine($"{today} / {birthday} / {(actualResult == expectedResult ? "PASS" : "FAIL")}");
}
}

关于algorithm - 查找日期是否在另一个日期之间的两个周末范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com