Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
7.5 kB
1
Indexable
Never
package co.edu.umanizales.exercises_arrays.controller;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.bitCount;
import static java.lang.Integer.parseInt;


@RestController
@RequestMapping("/arrays")
public class ArraysController {
    @PostMapping("/point-1")
    public int pointOne(@RequestBody int[] array) {
        return getMaxPosition(array);
    }
    @PostMapping("/point-2")
    public int pointTwo(@RequestBody int[] array) {
        return getMaxPositionPrime(array);
    }
    @GetMapping("/point-3")
    public int[]pointThree(){
        return findPrimeNumbers(100,300);
    }
    @PostMapping("/point-4")
    public int[] pointFour(@RequestBody int[] array) {
        return findLastNumber(array);
    }
    @PostMapping("/point-5")
    public int pontFive(@RequestBody int[] array) {
        return RepetitionNumber(array);
    }
    @PostMapping("/point-6")
    public String pontSix(@RequestBody int[] array) {
        return calculateAverage (array);
    }
    @PostMapping("/point-7")
    public int pontSeven(@RequestBody int[] array) {
        return positionSumDigits (array);
    }
    @PostMapping("/point-8")
    public String pontEight(@RequestBody int[] array) {
        return factorialOfNumber (array);
    }
    @PostMapping("/point-9")
    public String pontNine(@RequestBody int[] array) {
        return rangeNumbIs(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 getMaxPosition(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 getMaxPositionPrime(int[] array){
        int max = array[0];
        int position = 0;
        for(int i=0; i<array.length; i++) {
            if(isPrime(array[i])) {
                if(array[i] > max) {
                    max = array[i];
                    position = i;
                }
            }
        }
        return position;
    }
    private int[]findPrimeNumbers(int firstPosition, int lastPosition){
        int[]primes = new int[10];
        int count = 0;
        for(int i=firstPosition; i<=lastPosition; i++){
           if(isPrime(i) && count <10){
               primes[count] = i;
               count = count + 1;
           }
        }
        return primes;
    }
    private boolean findEndnumbers(int number, int numberfinal){
        if (number % 10 == numberfinal) {
            return true;
        }
    return false;
    }
    private int[]findLastNumber(int[]array){
        List<Integer> numberEnd = new ArrayList<>();
        for(int i=0; i<array.length; i++){
        if(findEndnumbers(array[i], 4)){
            numberEnd.add(i);
        }
        }
        return numberEnd.stream().mapToInt(Integer::intValue).toArray();
    }
private int RepetitionNumber(int[] array){
        int positionfinal = getMaxPosition(array);
        int count = 0;

        for(int i=0; i<array.length; i++) {
            if (array[i] == array[positionfinal]){
                count++;
        }
    }
     return count;
    }
    private String calculateAverage(int[] array){
        int sum =sumArray(array);
        boolean band=false;
        int count=0;
        int rest=0;
        String result="";
        for(int i=0; i<array.length; i++){
            if(array[i] == (sum / array.length)){;
             result += ("la posicion es"+i);
                band = true;
            }

        }
        if(!band){
            return "no hay equivalencia";
        } else{
        return result;
        }
    }
    private int sumDigits(int number){
        String numberString = String.valueOf(number);
        int sum = 0;
        for(int i=0; i<numberString.length(); i++){
            sum += Character.getNumericValue(numberString.charAt(i));
        }
        return sum;
    }
    private int positionSumDigits(int[] array){
        int may= sumDigits(array[0]);
        int count=0;
        for(int i=1; i<array.length; i++) {
            if(sumDigits(array[i]) > may){
                may = sumDigits(array[i]);
                count = i;
            }
        }
        return count;
    }
    private int isFactoNumber(int [] array){
        List<Integer> factorial = new ArrayList<>();
        for(int i=0; i<array.length; i++) {
            factorial.add(array[i]);
        }
        return factorial.stream().mapToInt(Integer::intValue).sum();
        }

    private String factorialOfNumber (int []array){
    StringBuilder text= new StringBuilder();
    int factorial= isFactoNumber(array);
    for(int i=0; i<array.length; i++) {
        text.append("el factorial es:").append(array[i]).append("es").append(factorial);
    }
    return text.toString();
    }
    private String NumberInRange (int start, int end){
        StringBuilder text = new StringBuilder();
        for(int i=start; i<=end; i++){
            text.append(i).append(",");
        }
        return text.toString();
    }
    private String rangeNumbIs(int [] array){
        StringBuilder text = new StringBuilder();
        for(int i=0; i<array.length; i++) {
            text.append("Los ins comprendidos desde 1:").append(array[i]).append("son:").append(NumberInRange(1, array[i]));
        }
        return text.toString();
    }
}
Leave a Comment