Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created August 16, 2011 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/1149534 to your computer and use it in GitHub Desktop.
Save joseanpg/1149534 to your computer and use it in GitHub Desktop.
GEJS Vocales (Con RegEx)
// FUNCIONES AUXILIARES
/*
* La siguiente función cuenta las propiedades
* (tanto propias como hereradas) de un objeto
*/
function contarPropiedades(obj) {
var recuento = 0;
for (propiedad in obj) recuento ++;
return recuento;
}
/*
* La siguiente función permite realizar "estudio estadístico"
* de las vocales de un texto.
*
*/
var rxs= {'a':/[aáàâä]/g,
'e':/[eéèêë]/g,
'i':/[iíìîï]/g,
'o':/[oóòôö]/g,
'u':/[uúùûü]/g};
function estadisticaDeVocales(texto) {
texto = texto.toLowerCase();
var tablaDeFrecuencias = {};
for (vocal in rxs) {
var aux = texto.match(rxs[vocal]);
if (aux !== null) tablaDeFrecuencias[vocal] = aux.length;
}
return tablaDeFrecuencias;
}
// RESOLUCIÓN DEL PROBLEMA
/*
* vocalizeitor recorre el árbol de strings
* buscando el máximo número de vocales distintas
* por nodo. Devuelve dicho número y las string que lo tienen
*
*/
function vocalizeitor(soa, campeones) {
if (soa instanceof Array) {
for (var j=0, len = soa.length;j<len;j++) {
vocalizeitor(soa[j],campeones);
}
}
else {
var numeroDeVocalesDistintas = contarPropiedades(estadisticaDeVocales(soa));
if (numeroDeVocalesDistintas == campeones.numeroDeVocalesDistintas) {
campeones.nodos.push(soa);
}
else if (numeroDeVocalesDistintas > campeones.numeroDeVocalesDistintas) {
campeones.numeroDeVocalesDistintas = numeroDeVocalesDistintas;
campeones.nodos = [soa];
}
}
}
/*
* Esta vez le toca a Ferb poner en marcha el invento
*
*/
function rxferb(a) {
var campeones = {numeroDeVocalesDistintas:0,nodos:[]};
vocalizeitor(a,campeones);
return campeones;
}
//TEST
var ej = rxferb(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengoóaiua", ["diiiiineeeeroooooo"] ] ]);
console.log(ej.numeroDeVocalesDistintas,ej.nodos.join());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment