132 lines
3.7 KiB
Java
132 lines
3.7 KiB
Java
package utils;
|
|
|
|
import java.util.List;
|
|
import java.util.Arrays;
|
|
import java.util.stream.Collectors;
|
|
import java.util.Scanner;
|
|
import java.util.function.Function;
|
|
import java.util.function.BiFunction;
|
|
|
|
public class Utils {
|
|
|
|
public static final char[] ALPHABET = new char[] {
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
|
|
};
|
|
|
|
public static String[] CESAR_DATA_CLEAR = new String[] {
|
|
"quitte moi quittons nous juste un peu trop longtemps",
|
|
"pour que nous ressentions le bonheur d etre triste"
|
|
};
|
|
|
|
public static int[] CESAR_DATA_KEYS = new int[] {
|
|
7, 9
|
|
};
|
|
|
|
public static String[] CESAR_DATA_CRYPT = new String[] {
|
|
"xbpaal tvp xbpaavuz uvbz qbzal bu wlb ayvw svunaltwz",
|
|
"yxda zdn wxdb anbbnwcrxwb un kxwqnda m ncan carbcn"
|
|
};
|
|
|
|
public static int[] TEST_SOMME2 = new int[] {
|
|
0, 3, 6,
|
|
3, 7, 25,
|
|
7, 3, 25,
|
|
};
|
|
|
|
public static void testSomme2(BiFunction<Integer, Integer, Integer> func) {
|
|
for (int i=0; i<TEST_SOMME2.length; i+=3) {
|
|
int n=TEST_SOMME2[i];
|
|
int p=TEST_SOMME2[i+1];
|
|
int s=TEST_SOMME2[i+2];
|
|
|
|
afficher("n = ", n, "p = ", p);
|
|
afficher("*** SOMME: ", func.apply(n, p));
|
|
afficher(" Attendu: ", s);
|
|
}
|
|
}
|
|
|
|
public static final void afficher(Object... args) {
|
|
if (args == null) {
|
|
System.out.println("null");
|
|
} else {
|
|
String str = Arrays.stream(args)
|
|
.map(String::valueOf)
|
|
.collect(Collectors.joining(" "));
|
|
System.out.println(str);
|
|
}
|
|
System.out.print(' ');
|
|
}
|
|
public static final void afficherTabInt(int... args) {
|
|
if (args == null) {
|
|
System.out.println("null");
|
|
} else {
|
|
String str = Arrays.stream(args)
|
|
.boxed()
|
|
.map(i -> i+"")
|
|
.collect(Collectors.joining(" "));
|
|
System.out.println(str);
|
|
}
|
|
System.out.print(' ');
|
|
}
|
|
|
|
public static int lireEntier() {
|
|
try (Scanner reader = new Scanner(System.in)) {
|
|
System.out.println("Saisir un entier: ");
|
|
return reader.nextInt();
|
|
}
|
|
}
|
|
|
|
public static String lireChaine() {
|
|
try (Scanner reader = new Scanner(System.in)) {
|
|
System.out.println("Saisir une valeur et valider: ");
|
|
return reader.nextLine();
|
|
}
|
|
}
|
|
|
|
public static int position(char c) {
|
|
for (int i = 0; i<ALPHABET.length; i++) {
|
|
if (ALPHABET[i] == c) {
|
|
return i;
|
|
}
|
|
}
|
|
afficher("erreur", c);
|
|
return -1;
|
|
}
|
|
|
|
public static void testCesar(BiFunction<char[], Integer, char[]> func,
|
|
boolean chiffre) {
|
|
for (int i = 0; i<CESAR_DATA_KEYS.length; i++) {
|
|
int cle = CESAR_DATA_KEYS[i];
|
|
if (chiffre) {
|
|
String result = String.valueOf(func.apply(CESAR_DATA_CLEAR[i]
|
|
.toCharArray(), cle));
|
|
afficher("Clair: " + CESAR_DATA_CLEAR[i]);
|
|
afficher("Chiffré: " + result);
|
|
afficher("Succès: " + String.valueOf(CESAR_DATA_CRYPT[i]).equals(result));
|
|
} else {
|
|
String result = String.valueOf(func.apply(CESAR_DATA_CRYPT[i]
|
|
.toCharArray(), cle));
|
|
afficher("Chiffré: " + CESAR_DATA_CRYPT[i]);
|
|
afficher("Clair: " + result);
|
|
afficher("Succès: " + String.valueOf(CESAR_DATA_CLEAR[i]).equals(result));
|
|
}
|
|
afficher();
|
|
}
|
|
}
|
|
|
|
public static void testPalindrome(Function<char[], Boolean> func) {
|
|
testPalindrome(func, "palindrome", false);
|
|
for (String pal : List.of("elle", "kayak", "serre", "essayasse")) {
|
|
testPalindrome(func, pal, true);
|
|
}
|
|
}
|
|
|
|
public static void testPalindrome(Function<char[], Boolean> func,
|
|
String mot, boolean result) {
|
|
boolean val = func.apply(mot.toCharArray());
|
|
afficher(mot, result == val ? "OK" : "ERREUR");
|
|
}
|
|
|
|
}
|