DEV Community

dev.to staff
dev.to staff

Posted on

10 2

Daily Challenge #291 - Extended Weekend

If the first day of the month is a Friday, it is likely that the month will have an Extended Weekend. That is to say, it could have five Fridays, five Saturdays and five Sundays.

In this Kata, you will be given a start year a and an end year b(1000 <= a <= b <= 275760). Your task will be to find months that have extended weekends and return:

The first and last month in the range that has an extended weekend
The number of months that have extended weekends in the range, inclusive of start year and end year.
If, there is no Extended Weekend, just return ["","",0].

Example

solve(2016,2020) = ("Jan","May",5).
The months are: Jan 2016, Jul 2016, Dec 2017, Mar 2019, May 2020

Tests

solve(2016,2020)
solve(1900,1950)
solve(1800,2500)
solve(1000,275760)

Good luck!


This challenge comes from myjinxin2015 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Redis image

Short-term memory for faster
AI agents 🤖💨

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

Top comments (2)

Collapse
 
_bkeren profile image
'' • Edited
  • To create a range as an array in JS with inclusive borders , you can use Array(end + 1).keys , then slice it from the start number.
  • For extended month, only look for months which takes 31 days.
  • In JS Date object, month index starts from 0. Jan => 0 , Feb => 1 and so on.
  • In JS Date object, week day index starts from 0 with Sunday, so Friday's index is 5.
const solve = (a,b) => {
 const FRIDAY = 5, JAN = 0, MAR = 2 , MAY = 4, JUL = 6, AUG = 7, OCT = 9, DEC = 11
 const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
 const extendedMonths = [...Array(b+1).keys()].slice(a).map(year => ([JAN,MAR,MAY,JUL,AUG,OCT,DEC].filter(monthIndex => new Date(year,monthIndex,1).getDay() === FRIDAY)).map(monthIndex => `${months[monthIndex]} ${year}`)).flat()
 const firstExtendedMonth = extendedMonths.length > 0 ? extendedMonths[0].split(" ")[0] : ""
 const lastExtendedMonth = extendedMonths.length > 0 ? extendedMonths[extendedMonths.length - 1].split(" ")[0] : ""
 return [firstExtendedMonth,lastExtendedMonth,extendedMonths.length]
}

Collapse
 
greymd profile image
Yasuhiro Yamada • Edited

Solution in Bash and some general commands.

#!/bin/bash
solve () {
  seq $1 $2 |
    sed -n -e'h;s/$/'{01,03,05,07,08,10,12}'01/;p;x' |
    LANG=C date -f- +'%b %a' |
    grep Fri |
    awk 'NR==1{printf $1}END{print " "$1" "NR}'
}

solve 2016 2020   # => Jan May 5
solve 1900 1950   # => Mar Dec 51
solve 1800 2500   # => Aug Oct 702
solve 1000 275760 # => Aug Aug 274761
solve 1973 1973 #=> [empty] [empty] 0

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay