Created
January 5, 2019 16:44
-
-
Save rigobertocontreras/54f4b1aa5996bdf1f9fb451b6f6b6eae to your computer and use it in GitHub Desktop.
Function to parse domino strings recursive.
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
/* | |
Given a string, representing domino tiles chain. Each tile has L-R format, where L and R are numbers from 1..6 range. Tiles are separated by the comma. Some examples of valid S chain are: | |
1. 1-4,4-2,3-1 | |
2. 6-3 | |
3. 4-3,5-1,2-2,1-3,4-4 | |
Devise a function that, give a domino string, returns the number of tiles in the longest matching group within S. A matching group is a set of tiles that match and that are subsequent in S. Domino tiles match, if the right side of a tile is the same as the left side of the following tile. 2-4,4-1 are matching tiles, but 5-2,1-6 are not. | |
domino("1-1,3-5,5-2,2-3,2-4") should return 3. | |
*/ | |
#!/usr/bin/env node | |
console.time('timerName'); | |
const domino = (value: string, max: number = 1, count: number = 1) => { | |
if (count > max) max = count; | |
if (value[4] == undefined) { | |
return max; | |
} else { | |
return domino(value.slice(4), max, (value[2] == value[4]) ? count + 1 : 1); | |
} | |
} | |
console.log(domino("1-1")); // 1 | |
console.log(domino("1-2,1-2")); // 1 | |
console.log(domino("3-2,2-1,1-4,4-4,5-4,4-2,2-1")); // 4 | |
console.log(domino("5-5,5-5,4-4,5-5,5-5,5-5,5-5,5-5,5-5,5-5")); // 7 | |
console.log(domino("1-1,3-5,5-5,5-4,4-2,1-3")); // 4 | |
console.log(domino("1-2,2-2,3-3,3-4,4-5,1-1,1-2")); // 3 | |
console.timeEnd('timerName'); | |
export { }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment