diff --git a/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej02_OrdenarArrayPorDivisores.java b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej02_OrdenarArrayPorDivisores.java new file mode 100644 index 0000000..3b43f83 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej02_OrdenarArrayPorDivisores.java @@ -0,0 +1,77 @@ +package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz; + +import tutorialJava.Utils; +import tutorialJava.UtilsArrays; + +public class Ej02_OrdenarArrayPorDivisores { + + public static void main(String[] args) { + int a[] = inicializaArray(); + UtilsArrays.imprimeArray(a); + + ordenaArrayPorDivisores(a); + + UtilsArrays.imprimeArray(a); + } + + + /** + * + * @return + */ + private static int[] inicializaArray () { + int a[] = new int[100]; + + for (int i = 0; i < a.length; i++) { + a[i] = Utils.obtenerNumeroAzar(0, 100); + } + + return a; +// return new int[] {11, 22, 20, 12, 8}; + } + + + /** + * + * @param n + * @return + */ + private static int countDivisores(int n) { + int contadorDivisores = 0; + for (int i = 2; i < n; i++) { + if (n % i == 0) { + contadorDivisores++; + } + } + return contadorDivisores; + } + + + /** + * + * @param a + */ + private static void ordenaArrayPorDivisores (int a[]) { + boolean hayIntercambios; + do { + hayIntercambios = false; + for (int i = 0; i < (a.length - 1); i++) { + if (countDivisores(a[i]) > countDivisores(a[i + 1])) { + int aux = a[i]; + a[i] = a[i + 1]; + a[i + 1] = aux; + hayIntercambios = true; + } + } + } while (hayIntercambios == true); + } +} + + + + + + + + + diff --git a/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej03_StringDeCamelCaseASnakeCase.java b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej03_StringDeCamelCaseASnakeCase.java new file mode 100644 index 0000000..d71584c --- /dev/null +++ b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/Ej03_StringDeCamelCaseASnakeCase.java @@ -0,0 +1,32 @@ +package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz; + +public class Ej03_StringDeCamelCaseASnakeCase { + + public static void main(String[] args) { + String str = "estoEsUnEjemplo"; + System.out.println("Cadena modificada: " + camelCaseToSnakeCase(str)); + } + + private static String camelCaseToSnakeCase (String str) { +// StringBuffer sb = new StringBuffer(); + String sb = ""; + + for (int i = 0; i < str.length(); i++) { + // Compruebo si es una mayúscula + if (str.charAt(i) >= 65 && str.charAt(i) <= 90) { +// sb.append("_"); + sb += "_"; + char minuscula = (char) (str.charAt(i) + 32); +// sb.append(minuscula); + sb += minuscula; + } + else { // No es una mayúscula +// sb.append(str.charAt(i)); + sb += str.charAt(i); + } + } + + return sb.toString(); + } + +} diff --git a/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/ElementosMasFrecuentesEnMatriz.java b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/ElementosMasFrecuentesEnMatriz.java new file mode 100644 index 0000000..22fad71 --- /dev/null +++ b/src/tutorialJava/examenes/examen20250619/ej01_elementosComunesEnMatriz/ElementosMasFrecuentesEnMatriz.java @@ -0,0 +1,152 @@ +package tutorialJava.examenes.examen20250619.ej01_elementosComunesEnMatriz; + +import java.util.HashMap; +import java.util.Iterator; + +import tutorialJava.Utils; +import tutorialJava.UtilsArrays; + +public class ElementosMasFrecuentesEnMatriz { + + /** + * + * @param args + */ + public static void main(String[] args) { + + int m[][] = inicializaMatriz(); + UtilsArrays.imprimeMatriz(m); + System.out.println(); + + HashMap hm = creaHashMapDeRepeticiones(m); + + int mRepeticiones[][] = getMatrizRepeticionesDesdeHashMap(hm); + UtilsArrays.imprimeMatriz(mRepeticiones); + System.out.println(); + + ordenarMatrizRepeticiones(mRepeticiones); + UtilsArrays.imprimeMatriz(mRepeticiones); + + // Sacamos en consola los x que más se repiten + int podium = 3; + for (int i = 0; i < podium; i++) { + System.out.println("Número más repetido: " + + mRepeticiones[i][0] + " con " + + mRepeticiones[i][1] + " repeticiones"); + } + } + + + + /** + * + * @return + */ + private static int[][] inicializaMatriz () { + int filas = Utils.obtenerEnteroConDescripcion( + "Introduzca filas de la matriz: "); + int columnas = Utils.obtenerEnteroConDescripcion( + "Introduzca columnas de la matriz: "); + + int m[][] = new int[filas][columnas]; + + for (int i = 0; i < m.length; i++) { + for (int j = 0; j < m[i].length; j++) { + m[i][j] = Utils.obtenerNumeroAzar(0, 20); + } + } +// int m[][] = new int[][] {{2, 3, 4, 4}, +// {2, 3, 5, 5}, +// {5, 4, 6, 7}, +// {6, 7, 4, 5}}; + + return m; + } + + /** + * + * @param m + * @return + */ + private static HashMap + creaHashMapDeRepeticiones(int m[][]) { + + HashMap hm = new HashMap(); + + for (int i = 0; i < m.length; i++) { + for (int j = 0; j < m[i].length; j++) { + int valorAComprobar = m[i][j]; + + // Compruebo si el valorAComprobar ya existe, o no, como + // key del hashmap + if (hm.get(valorAComprobar) == null) { + // En el HashMap no hay key que tenga el "valorAComprobar" + hm.put(valorAComprobar, 1); + } + else { + // Ya existe un key de "valorAComprobar" en el hm + int repeticionesDeValorAComprobar = hm.get(valorAComprobar); + hm.put(valorAComprobar, repeticionesDeValorAComprobar + 1); + } + } + } + + return hm; + } + + /** + * + * @param hm + * @return + */ + private static int[][] getMatrizRepeticionesDesdeHashMap( + HashMap hm) { + + int mRepeticiones[][] = new int[hm.values().size()][2]; + + Iterator keys = hm.keySet().iterator(); + int i = 0; + while (keys.hasNext()) { + int singleKey = keys.next(); + mRepeticiones[i][0] = singleKey; + mRepeticiones[i][1] = hm.get(singleKey); + i++; + } + return mRepeticiones; + } + + + /** + * + * @param m + */ + private static void ordenarMatrizRepeticiones (int m[][]) { + boolean hayIntercambios; + + do { + hayIntercambios = false; + for (int i = 0; i < (m.length - 1); i++) { + if (m[i][1] < m[i + 1][1]) { + int aux[] = m[i]; + m[i] = m[i + 1]; + m[i + 1] = aux; + hayIntercambios = true; + } + } + } while (hayIntercambios == true); + + } +} + + + + + + + + + + + + +