Created
July 19, 2025 16:41
-
-
Save vladyn/d9b511db4fe014fbf7dfbd0c6fcf8afc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pattern = ["red", "green", "yellow"]; | |
function isValidTrafficSequence(matcher: string[]) { | |
const target = new Array(matcher.length).fill(pattern).flatMap(x => x); | |
const reducer = (acc: [], item: never, index: number) => { | |
if (item === target[index]) acc.push(item); | |
return acc; | |
} | |
const result = matcher.reduce(reducer, []); | |
return matcher.length === result.length; | |
} | |
isValidTrafficSequence(["red", "green", "yellow", "red", "green"]); | |
isValidTrafficSequence(["red", "yellow", "green"]); | |
isValidTrafficSequence([]); | |
isValidTrafficSequence(["red", "yellow", "green", "red", "yellow", "green", "red", "yellow", "green", "green"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple task for validating a traffic light 🚦 sequence, I've got from a challenge of the week.