Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Get the date of the Nth day of week after the Xth day of week in a given year and month

Introduction

In the United States, national elections are normally held on the first Tuesday after the first Monday in November.

Challenge

Write a program that takes a Year as integer, and a pair of (day of week as character and ordinal as integer) and output the date as "yyyy-mm-dd" Remember, the second pair is after the first pair. use this for inspiration: https://codegolf.stackexchange.com/questions/178227/get-the-date-of-the-nth-day-of-week-in-a-given-year-and-month[][1]

Example Input and Output

Example input:

  • 2021 November Tuesday 1 Monday 1
  • 2020 November Tuesday 1 Monday 1
  • 2020 December Friday 2 Wednesday 1
  • 2019 April Wednesday 1 Friday 1
  • 2023 February Tuesday 4 Wednesday 1
  • 2023 February Wednesday 5 Thursday 1
  • 2022 December Sunday 5 Sunday 55

Expected output:

  • "2021年11月02日"
  • "2020年11月03日"
  • "2020年12月11日"
  • "2019年04月10日"
  • "2023年02月28日"
  • "NA"
  • "NA"

Note: the output indicated as "NA" could be any negative finding such as null, but not an error. [1]: Get the date of the nth day of week in a given year and month

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ Based on the last two test cases, if the calculated date exceeds the input year+month, it should output NA. But maybe it's good to verify with OP whether this is optional or mandatory, since just calculating beyond the current year+month works perfectly fine for larger input. 🤷 \$\endgroup\$ Commented Feb 2, 2023 at 17:02
  • 1
    \$\begingroup\$ Hopefully, "undefined behaviour" is sufficient in those cases, @KevinCruijssen. \$\endgroup\$ Commented Feb 2, 2023 at 17:11

AltStyle によって変換されたページ (->オリジナル) /