Created
November 7, 2014 03:18
-
-
Save roadmanfong/aee476f1cdd1088dc441 to your computer and use it in GitHub Desktop.
findOnlyOncePair
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
function remainFreqenceEqual(data, frequence){ | |
console.log('remainFreqenceEqual'); | |
frequence = frequence || 1; | |
return _.chain(data) | |
.reduce(function (acc, freq) { | |
console.log('acc'); | |
console.log(acc); | |
acc[freq] = typeof acc[freq] == 'undefined' ? 1 : acc[freq]+1; | |
return acc; | |
}, {}) | |
.map(function(num, key) { | |
console.log(num + ',' + key); | |
return { | |
key: key, | |
frequence: num | |
} | |
}) | |
.filter(function(obj) { | |
console.log('obj'); | |
console.log(obj); | |
return obj.frequence === frequence; | |
}) | |
.pluck('key') | |
.value(); | |
} | |
function parsePairToString(data){ | |
console.log('parsePairToString'); | |
return _.chain(data) | |
.flatten(data,true) | |
.map(function(obj) { | |
return obj.toString(); | |
}) | |
.value(); | |
} | |
function parseStringToPair(data){ | |
console.log('parseStringToPair'); | |
return _.map(data, function(obj) { | |
var arr = obj.split(','); | |
return [parseFloat(arr[0]), parseFloat(arr[1])] | |
}); | |
}; | |
function findOnlyOncePair(data){ | |
data = parsePairToString(data); | |
console.log(data); | |
data = remainFreqenceEqual(data); | |
console.log(data); | |
data = parseStringToPair(data); | |
console.log(data); | |
return data; | |
} | |
var data = [ | |
[[1,1],[1,2]], | |
[[1,2],[2,4]], | |
[[1,1],[2,2]] | |
]; | |
findOnlyOncePair(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment