9
\$\begingroup\$

Given a MM/DD date (12/24) and a start/end date range (11/01 - 06/24), figure out if the date is within the listed date span.

Date ranges can be sequential (05/01 - 11/01) or wrap around to the next year (11/01 - 05/01).

Examples:

  • 12/24 is in 11/01 - 06/24 = True
  • 06/24 is in 11/01 - 06/24 = True
  • 06/24 is in 06/24 - 06/24 = True
  • 06/24 is in 11/01 - 06/23 = False
  • 07/24 is in 11/01 - 06/24 = False
  • 07/24 is in 05/01 - 11/01 = True
  • 07/24 is in 07/23 - 07/20 = True

Years do not mater. The date and/or date range is assumed to apply for any year past or future.

The end date will always be after the start date. If the end date is numerically less than the start date, we assume we are wrapping around to the next year.

The three input variables can be via any method (args, HTTP, stdin, etc..) and in any format (string, JSON, array, etc..)

Response can be boolean or any form of a yes/no/correct/etc.. string. Shortest code wins.

You cannot simply pass args to a built-in function.

\$\endgroup\$
13
  • \$\begingroup\$ Can we take the result as an array of strings instead? I.e ['MM', 'DD']? \$\endgroup\$
    – Mr. Xcoder
    Sep 11, 2017 at 17:38
  • \$\begingroup\$ Test case suggestion: 11/01 - 06/24 includes 06/24? \$\endgroup\$
    – Mr. Xcoder
    Sep 11, 2017 at 17:39
  • \$\begingroup\$ @Mr.Xcoder are you picturing something like ['06', '24'] if right else [] or what do you mean? \$\endgroup\$
    – Xeoncross
    Sep 11, 2017 at 17:42
  • 1
    \$\begingroup\$ For your second test case, suppose we're talking this year 2017 and next year 2018. How are we supposed to determine that 06/24 means 2018 and thus should be True rather than 2017 and thus False because it's before 11/01? \$\endgroup\$ Sep 11, 2017 at 17:57
  • 1
    \$\begingroup\$ "date > start & date < end" - Then some of your test cases are wrong. I think what you mean is start<=date<=end. \$\endgroup\$
    – Shaggy
    Sep 12, 2017 at 8:29

7 Answers 7

8
\$\begingroup\$

Python, 31 bytes

lambda a,d,e:(e<d)>=(a<d)+(e<a)

Try it online!

Takes inputs as MM/DD in order target, start, end.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 26 20 bytes

(a,b,c)=>b>c^b>a^a>c

Output is 0 if a lies within the range b...c, 1 if not. Edit: Saved 6 bytes thanks to @nwellnhof.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What about (a,b,c)=>b>c^b>a^a>c? \$\endgroup\$
    – nwellnhof
    Sep 11, 2017 at 23:09
  • \$\begingroup\$ @nwellnhof I can't believe I missed that - my very first attempt was b>a^a>c, but I then overlooked the simple fix... \$\endgroup\$
    – Neil
    Sep 11, 2017 at 23:42
  • \$\begingroup\$ You should update the byte count. \$\endgroup\$
    – nwellnhof
    Sep 11, 2017 at 23:59
  • \$\begingroup\$ @nwellnhof Sorry, I was still reeling from the shock! \$\endgroup\$
    – Neil
    Sep 12, 2017 at 0:00
4
\$\begingroup\$

Haskell, 28 bytes

(a%d)e=((e<a)/=(a<d))==(e<d)

Try it online!

Thanks to Leo for the trick of xor'ing the Booleans with /=.


Haskell, 33 bytes

(a%d)e=show[e>=d,d>a,a>e]!!16>'a'

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Your show trick is cool, but you can XOR the three elements in a more direct way with foldl1: tio.run/… \$\endgroup\$
    – Leo
    Sep 12, 2017 at 1:01
  • 1
    \$\begingroup\$ @Leo Nice idea, it's even shorter expanded out. \$\endgroup\$
    – xnor
    Sep 12, 2017 at 1:09
2
\$\begingroup\$

JavaScript (ES6), 31 bytes

Takes input as 3 strings in MM/DD format: date, range_from, range_to. Returns a boolean.

(a,b,c)=>!(b<c?a<b|a>c:a<b&a>c)

Test cases

let f =

(a,b,c)=>!(b<c?a<b|a>c:a<b&a>c)

console.log(f('12/24','11/01','06/24')) // True
console.log(f('06/24','11/01','06/24')) // True
console.log(f('06/24','06/24','06/24')) // True
console.log(f('06/24','11/01','06/23')) // False
console.log(f('07/24','11/01','06/24')) // False
console.log(f('07/24','05/01','11/01')) // True
console.log(f('07/24','07/23','07/20')) // True

\$\endgroup\$
1
  • \$\begingroup\$ Thanks to MM/DD format you can just use string sorting. Good idea. \$\endgroup\$
    – Xeoncross
    Sep 11, 2017 at 18:08
2
\$\begingroup\$

Haskell, 38 37 bytes

(b#e)d=(last$and:[or|b>e])[d>=b,d<=e]

Try it online!

Saved 1 byte thanks to Laikoni

\$\endgroup\$
1
  • \$\begingroup\$ You can use infix notation to save a byte: (d#b)e=. \$\endgroup\$
    – Laikoni
    Sep 11, 2017 at 21:16
1
\$\begingroup\$

Perl 6, 33 31 29 bytes

{.[0]>.[2]??![>] $_!![<=] $_}

Try it online!

Takes a list containing start, date, and end.

-2 bytes thanks to Ramillies.

\$\endgroup\$
1
  • \$\begingroup\$ And here, you can save 2 bytes by taking 1 list argument instead. Then @_[0] can be written as .[0] and [(whatever)] @_ as [(whatever)] $_. You just will need to call it like $f(($s,$d,$e)). \$\endgroup\$
    – Ramillies
    Sep 11, 2017 at 21:33
1
\$\begingroup\$

Perl 5, 60 bytes

59 bytes of code + 1 -a

$F[1]=~s/./2/if$F[1]le$F[0];say($F[2]le$F[1]&&$F[2]ge$F[0])

Try it online!

Input: start_date end_date target_date

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.