Created
August 16, 2011 09:18
-
-
Save felipernb/1148717 to your computer and use it in GitHub Desktop.
Given an array, normalize it to remove recursive arrays and return the number of distinct vowel in the word that has more distinct vowels
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
var merge_recursive = function(a, res) { | |
if (!Array.isArray(res)) res = []; | |
// Using BFS algorithm as if it was a tree | |
for (var i = 0; i < a.length; i++) { | |
if (Array.isArray(a[i])) { | |
merge_recursive(a[i], res); | |
} else { | |
res.push(a[i]); | |
} | |
} | |
return res; | |
} | |
var vowel_max_counter = function(arr) { | |
var tokens = merge_recursive(arr); | |
var count_different_vowels = function(str) { | |
var vowel_variations = [/[aáãàâä]/, /[eéêèë]/, /[iíîìï]/, | |
/[oóõòôö]/, /[uúùûü]/]; | |
var different_vowels = 0; | |
for (var i = 0; i < vowel_variations.length; i++) { | |
if (str.match(vowel_variations[i])) { | |
different_vowels++; | |
} | |
} | |
return different_vowels; | |
}; | |
var max = 0; | |
var different_vowels; | |
for (var i = 0; i < tokens.length; i++) { | |
max = Math.max(count_different_vowels(tokens[i]), max); | |
} | |
return max; | |
} | |
console.info(vowel_max_counter(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ])); |
En http://jsperf.com/gejs-estadistica-de-vocales/4 podemos apreciar lo inconveniente que resulta utilizar RegExp.prototype.exec
si lo único que queremos realizar es una comprobación de matching y no nos interesa ningún detalle de éste. En estos casos sin duda, lo que hay que utilizar es RegExp.prototype.test
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observa que escribí:
Uno de los propósitos del comentario anterior es servir de apoyo a otro comentario en http://jsperf.com/gejs-estadistica-de-vocales/3 (observa el título: Estadistica de Vocales ;)