Created
September 13, 2016 14:19
-
-
Save jbnv/4c5c2811d6010b90dd228ea94da05fcf to your computer and use it in GitHub Desktop.
Function to parse domino strings.
This file contains 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. | |
*/ | |
function domino(s) { | |
var a = s.split(","); | |
if (a.length == 0) return 0; | |
var length = 0; | |
var currentLength = 1; | |
var lastR = 0; | |
a.forEach(function(d) { | |
var da = d.split("-"); | |
var l = da[0]; | |
var r = da[1]; | |
if (l == lastR) { | |
currentLength++; | |
} else { | |
if (currentLength > length) length = currentLength; | |
currentLength = 1; | |
} | |
lastR = r; | |
if (currentLength > length) length = currentLength; | |
//console.log(d,currentLength,length); | |
}); | |
return length; | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment