Trouver les triplets de pythagore - Solution

On se retrouve aujourd'hui pour la solution du précédent #KataOfTheWeek proposé par Chetana en début de semaine !

Voici la solution proposée :

import java.util.*;

public class Main {

    /**
     * Kata
     *
     * @param args en entrée une liste de nombre séparé d'espace
     *             exemple 37 0 30 21 29 12 13 5 12 35 37 donnera
     *             (5, 12, 13)
     *             (12, 35, 37)
     */
    public static void main(String[] args) {
        List<String[]> result = findPythagoreTriplet(args);
        Collections.sort(result, Comparator.comparingInt(a -> Integer.valueOf(a[0])));
        if (result.size() < 1) {
            System.out.println("");
        } else {
            for (String[] triplet : result) {
                StringBuilder toPrint = new StringBuilder();
                toPrint.append('(').append(triplet[0]).append(", ")
                        .append(triplet[1]).append(", ").append(triplet[2]).append(')');
                System.out.println(toPrint);
            }
        }
    }

    public static List<String[]> findPythagoreTriplet(String[] args) {
        List<String[]> result = new ArrayList<>();
        if (args.length < 3) {
            return result;
        } else {
            int size = args.length;
            for (int i = 0; i <= size - 2; i++) {
                for (int j = 1; j <= size - 1; j++) {
                    saveIfNotAlreadyExist(args, result, size, i, j);
                }
            }
        }
        return result;
    }

    private static void saveIfNotAlreadyExist(String[] args, List<String[]> result, int size, int i, int j) {
        for (int k = 2; k <= size - 1; k++) {
            // calcul le carré de chaque élément
            int squareOfi = Integer.valueOf(args[i]) * Integer.valueOf(args[i]);
            int squareOfj = Integer.valueOf(args[j]) * Integer.valueOf(args[j]);
            int squareOfk = Integer.valueOf(args[k]) * Integer.valueOf(args[k]);
            if (isNotZero(squareOfi, squareOfj, squareOfk)
                    &&
                    checkIfTripletIsValid(squareOfi, squareOfj, squareOfk)) {
                if (result.isEmpty()) {
                    addTripletToResult(args, result, i, j, k);
                } else {
                    boolean canSave = true;
                    for (String[] saved : result) {
                        boolean contains = checkIfTripletAlreadySaved(
                                existInTriplet(saved, args[i]),
                                existInTriplet(saved, args[j]),
                                existInTriplet(saved, args[k]));
                        if (contains) {
                            canSave = false;
                            break;
                        }
                    }
                    if (canSave) {
                        addTripletToResult(args, result, i, j, k);
                    }
                }
            }
        }
    }

    private static boolean checkIfTripletIsValid(int squareOfi, int squareOfj, int squareOfk) {
        return (squareOfi == (squareOfj + squareOfk))
                || (squareOfj == (squareOfi + squareOfk))
                || (squareOfk == (squareOfi + squareOfj));
    }

    private static boolean isNotZero(int squareOfi, int squareOfj, int squareOfk) {
        return (squareOfi != 0) && (squareOfj != 0) && (squareOfk != 0);
    }

    private static boolean checkIfTripletAlreadySaved(boolean b, boolean b2, boolean b3) {
        return b && b2 && b3;
    }

    private static boolean existInTriplet(String[] saved, String arg) {
        return Arrays.stream(saved).anyMatch(String.valueOf(arg)::equals);
    }

    private static void addTripletToResult(String[] args, List<String[]> result, int i, int j, int k) {
        String[] triplet = new String[3];
        triplet[0] = String.valueOf(args[i]);
        triplet[1] = String.valueOf(args[j]);
        triplet[2] = String.valueOf(args[k]);
        Arrays.sort(triplet, Comparator.comparingInt(a -> Integer.valueOf(a)));
        result.add(triplet);
    }
}

A bientôt pour un nouveau #KataOfTheWeek !

TakiVeille

TakiVeille