vendredi 12 février 2021

challenge: conditions on date and weekday using only IFs

This is a real-life problem I'm facing gamified into a coding challenge.


  • Goal: A program should run exactly once per month - let's arbitrarily pick the 10:th.
  • Goal: If the 10:th is a Monday, it should run on the following day - Tuesday the 11:th - instead.
  • Constraint: The program is launched once per day by a scheduler that is beyond our control.
  • Constraint: The only way to stop the program from running is to write if (condition) return clauses. If the last if doesn't result in a return, the program will run today.

We have the variables DIM for Day-In-Month, an integer in [1,31] and DOW for Day-of-Week, an integer in [0,6].


Naive attempt provided as an example:

if ( (DIM != 10) and (DIM != 11) ) return
if (DOW == 1) return

This is no good since it will run the program on the both the 10:th and the 11:th, unless the 10:th is a Monday.

Clearly more conditions are needed.


An unnecessarily convoluted way:

Let P be the first seven primes 2 3 5 7 11 13 17
Let Q be the next 31 primes 19 ... 163
Let R be P[DOW] × Q[DIM]
if ( (R!=177) and (R!=295) and (R!=413) and (R!=649) and (R!=767) and (R!=1003) and (R!=122) ) return

Please help me find a concise way of doing this with a few easy-to-read conditions.

Aucun commentaire:

Enregistrer un commentaire