Untitled
unknown
plain_text
25 days ago
8.0 kB
2
Indexable
Never
package co.edu.umanizales.exercise_arrays.controller; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/arrays") public class ArraysController { @PostMapping("/exercise-1") public int exerciseOne(@RequestBody int[] array) { return findMaxIndex(array); } @PostMapping("/exercise-2") public int exerciseTwo(@RequestBody int[] array) { return findMinIndexPrime(array); } @GetMapping("/exercise-3") public int[] exercisetheree() { return primeNumbersInRange(100, 300); } @PostMapping("/exercise-4") public int[] exerciseFour(@RequestBody int[] array) { return findNumbersEndFour(array); } @PostMapping("/exercise-5") public int exerciseFive(@RequestBody int[] array) { return largesNumber(array); } @PostMapping("/exercise-6") public String exerciseSix(@RequestBody int[] array) { return averageCalculate(array); } @PostMapping("/exercise-7") public int exerciseSeven(@RequestBody int[] array) { return positionSumNumbersLargest(array); } @PostMapping("/exercise-8") public String exercisEigth(@RequestBody int[] array) { return paintNumberFactorial(array); } @PostMapping("/exercise-9") public String exerciseNine(@RequestBody int[] array) { return printRangeOfNumbers(array); } @PostMapping("/sustentation") public String sustentation(@RequestBody int[] array) { return verifyEqu(array); } @GetMapping("/{num1}/{num2}") public String getPrimesBetweenTwoNumbers(@PathVariable int num1, @PathVariable int num2) { if (validateNumberPositive(num1) && validateNumberPositive(num2)) { if (isPrime(num1)) { return "Número 1 es primo"; } else { return "luego seguimos"; } } else { return "Los números deben ser positivos"; } } private boolean validateNumberPositive(int number) { if (number > 0) { return true; } else { return false; } } private boolean isPrime(int number) { int contDiv = 0; int half = number / 2; for (int i = 2; i < half; i++) { if (number % i == 0) { contDiv++; } } if (contDiv == 0) { return true; } else { return false; } } private String verifyEqu(int[] array) { int sum = sumArray(array); boolean band = false; String cadena = ""; for (int i = 0; i < array.length; i++) { if (sum - array[i] == array[i]) { cadena += "Pos:" + i + " Número:" + array[i] + " \n"; band = true; } } if (!band) { return "No hay equivalencias"; } else { return cadena; } } private int sumArray(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum; } private int findMaxIndex(int[] array) { int max = array[0]; int position = 0; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; position = i; } } return position; } private int findMinIndexPrime(int[] array) { int maxprime = 0; int position = 0; for (int i = 0; i < array.length; i++) { if (isPrime(array[i])) { if (array[i] > maxprime) { maxprime = array[i]; position = i; } } } return position; } private int[] primeNumbersInRange(int oneposition, int lastposition) { int[] primes = new int[10]; int count = 0; for (int i = oneposition; i <= lastposition; i++) { if (isPrime(i) && count < 10) { primes[count] = i; count = count + 1; } } return primes; } private boolean numberFindModul(int number, int numbergo) { if (number % 10 == numbergo) { return true; } return false; } private int[] findNumbersEndFour(int[] array) { List<Integer> endsNumber = new ArrayList<>(); for (int i = 0; i < array.length; i++) { if (numberFindModul(array[i], 4)) { endsNumber.add(i); } } return endsNumber.stream().mapToInt(i -> i).toArray(); } private int largesNumber(int[] array) { int positionlargest = findMaxIndex(array); int count = 0; for (int i = 0; i < array.length; i++) { if (array[i] == array[positionlargest]) { count = count + 1; } } return count; } private String averageCalculate(int[] array) { int sum = sumArray(array); boolean band = false; int count = 0; String result = ""; for (int i = 0; i < array.length; i++) { if (sum == array[i]) { count++; result += sum / count; band = true; } } if (!band) { return result; } return "No se encuentra la equivalencia"; } private int totalNumbers(int number){ String numberToString = String.valueOf(number); int sum = 0; for (int i = 0; i < numberToString.length(); i++) { sum += Character.getNumericValue(numberToString.charAt(i)); } return sum; } private int positionSumNumbersLargest(int[] array) { int largest = totalNumbers(array[0]); int position = 0; for (int i = 1; i < array.length; i++) { if(totalNumbers(array[i])>largest){ largest = totalNumbers(array[i]); position = i; } } return position; } private int factorialCalculate(int number){ int factorial = 1; for (int i = 1; i <= number; i++) { factorial = factorial * i; } return factorial; } private int[] factorialToNewArray(int[] array) { List<Integer> numbersFactorial = new ArrayList<>(); for (int i = 0; i < array.length; i++) { numbersFactorial.add(factorialCalculate(array[i])); } return numbersFactorial.stream().mapToInt(i -> i).toArray(); } private String paintNumberFactorial(int[] array) { StringBuilder text = new StringBuilder(); int[] factorialArray = factorialToNewArray(array); for (int i = 0; i<array.length; i++) { text.append("el factorial de: ").append(array[i]).append(" es ").append(factorialArray[i]).append("\n"); } return text.toString(); } private String printNumberRanger(int start, int end) { StringBuilder text = new StringBuilder(); for (int i = start; i <= end; i++) { text.append(i).append(" - "); } return text.toString(); } private String printRangeOfNumbers(int[] array) { StringBuilder text = new StringBuilder(); for (int i = 0; i < array.length; i++) { text.append("Los enteros comprendidos desde 1 hasta " ).append(array[i]).append(" son: ").append(printNumberRanger(1, array[i])).append("\n"); } return text.toString(); } }
Leave a Comment