using UnityEditor; using UnityEngine; public class Algos2 : MonoBehaviour { ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 1 : //Ecrit une fonction qui renvoi toujours un entier de valeur 2; // // => 2 static int Deux() { return 2; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 2 : //Ecrit une fonction qui renvoi toujours une string de valeur "bonjour"; // // => "bonjour" static string Bonjour() { return "bonjour"; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 3 : //Ecrit une fonction qui prend un paramètre de type int et renvoi un int du double de sa valeur // // 2 => 4 // 123 => 246 static int Double(int valeur) { return valeur * 2; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 4 //Ecrit une fonction qui prend un paramètre de type int et renvoi sa valeur + 10 // // 23 => 33 static int Ajoute10(int valeur2) { return valeur2 + 10; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 5 //Ecrit une fonction qui prend un paramètre de type int et renvoi sa valeur * 10 + 5 // // 10 => 105 static int Fois10Plus5(int valeur3) { return valeur3 * 10 + 5; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 6 //Ecrit une fonction qui prend un paramètre de type string et qui lui ajoute la chaine "David" à la fin avant de la renvoyer // // "Coucou" => "CoucouDavid" // "David" => "DavidDavid" static string AjouteDavid(string chaine) { return chaine + "David"; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 7 //Ecrit une fonction qui prend 2 paramètres de type int et qui renvoit leur somme // // 3, 5 => 8 // 12, 32 => 44 static int Somme(int valeur5, int valeur6) { return valeur5 + valeur6; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 8 //Ecrit une fonction qui prend un paramètre de type string et qui renvoi sa taille // // "Toto" => 4 // "" => 0 static int CharacterSize(string chaine) { return chaine == null ? 0 : chaine.Length; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 9 //Ecrit une fonction qui prend un paramètre de type string et qui supprime le 4e caractere // "Bonjour" => "Bonour" // "Ok" => "Ok" static string CharacterCut4(string chaine) { if (string.IsNullOrEmpty(chaine) || chaine.Length < 4) return chaine; return chaine.Remove(3, 1); } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 10 //Ecrit une fonction qui prend un paramètre de type string et qui remplace le premier caractère avec 'X' // // "Bonjour" => "Xonjour" static string CharacterX(string resultat8) { if (string.IsNullOrEmpty(resultat8)) return resultat8; return 'X' + resultat8.Substring(1); } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 11 //Ecrit une fonction qui indique si la chaine est un palindrome // "radar" => true // "hello" => false // "Kayak" => true (en ignorant la casse) // "abba" => true static bool EstPalindrome(string word) { if (string.IsNullOrEmpty(word)) return false; string normalized = word.ToLower(); int len = normalized.Length; for (int i = 0; i < len / 2; i++) { if (normalized[i] != normalized[len - 1 - i]) return false; } return true; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 12 //Indiquer si un tableau contient au moins un doublon. // [1, 5, 2, 5] => true // [1, 2, 3, 4] => false static bool ContientDoublon(int[] tableau) { if (tableau == null || tableau.Length <= 1) return false; var set = new System.Collections.Generic.HashSet(); foreach (var val in tableau) { if (!set.Add(val)) return true; } return false; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 13 // Calculer la factorielle d'un nombre entier positif $n$ ($n!$). // 0 => 1 // 3 => 6 (3 * 2 * 1) // 5 => 120 static int Factorielle(int n) { if (n < 0) return 0; int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 14 //Ecrit une fonction qui prend un paramètre val de type float et qui renvoi un float qui vaut val divisé par 2; // // 2.8f => 1.4f static float DivisePar2(float val) { return val / 2f; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 15 //Ecrit une fonction qui prend un paramètre val de type float et qui renvoi un float qui vaut racine carré de val; // // 9.0f => 3.0f static float Racine(float val) { return (float)System.Math.Sqrt(val); } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 16 //Ecrit une fonction qui prend 2 paramètre de type int et qui renvoi le plus grand; // // -52, 12 => 12 static int PlusGrand(int val1, int val2) { return val1 > val2 ? val1 : val2; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 17 //Ecrit une fonction qui prend 3 paramètres de type int et qui renvoi le plus grand; // // 1, 21, 8 => 21 static int PlusGrand(int val1, int val2, int val3) { int max = val1; if (val2 > max) max = val2; if (val3 > max) max = val3; return max; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 18 //Ecrit une fonction qui prend un paramètre val de type int et qui renvoi la somme de nombre de 1 à val; // // 0 => 0 // 3 => 6 // 4 => 10 // 5 => 15 static int SommeListe(int val) { int sum = 0; for (int i = 1; i <= val; i++) { sum += i; } return sum; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Exo 19 //Additionner La somme des nombres pairs entre 0 et le paramètre // // 0 => 0 // 1 => 0 // 3 => 2 // 4 => 6 // 10 => 30 static int AdditionNbPairs(int val) { int sum = 0; for (int i = 0; i <= val; i += 2) { sum += i; } return sum; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 20 // Renvoyer une chaine de caractère qui remplace chaque lettre par sa suivante // // "AAA" => "BBB" // "abcd" => "bcde" // "Bonjour" => "Cpokprs" static string NextString(string chaine) { if (string.IsNullOrEmpty(chaine)) return chaine; char[] chars = chaine.ToCharArray(); for (int i = 0; i < chars.Length; i++) { chars[i] = (char)(chars[i] + 1); } return new string(chars); } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 21 // Renvoyer la taille d'un tableau passé en paramètre // // [1, 3, 45] => 3 static int TailleTableau(int[] tableau) { return tableau == null ? 0 : tableau.Length; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 22 // Renvoyer la somme des nombres d'un tableau passé en parametre // // [2, 8, 200] => 210 static int SommeTableau(int[] tableau) { if (tableau == null) return 0; int sum = 0; foreach (var val in tableau) sum += val; return sum; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 23 // Renvoyer la moyenne des nombres d'un tableau passé en parametre // // [8, 10, 12] => 10 static int MoyenneTableau(int[] tableau) { if (tableau == null || tableau.Length == 0) return 0; int sum = 0; foreach (var val in tableau) sum += val; return sum / tableau.Length; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 24 // Renvoyer le plus grand des nombres d'un tableau passé en parametre // // [8, 19, 12, 5, -654] => 19 static int PlusGrand(int[] tableau) { if (tableau == null || tableau.Length == 0) return 0; int max = tableau[0]; for (int i = 1; i < tableau.Length; i++) { if (tableau[i] > max) max = tableau[i]; } return max; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 25 // Renvoyer le nombre de voyelles d'une chaine de caractère // // Bonjour => 3 // Animal => 3 static int NbVoyelles(string word) { if (string.IsNullOrEmpty(word)) return 0; int count = 0; string vowels = "aeiouyAEIOUY"; foreach (char c in word) { if (vowels.Contains(c)) count++; } return count; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 26 // Indiquer le nombre de pair de lettres concecutives // // "aa" => 1 // "aabb" => 2 // "aaa" => 2 // "xxxx" => 3 // "abababa" => 0 static int LettreIdentiqueConcecutive(string word) { if (string.IsNullOrEmpty(word) || word.Length < 2) return 0; int count = 0; for (int i = 0; i < word.Length - 1; i++) { if (word[i] == word[i + 1]) { count++; } } return count; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 27 // Trier un tableau passé en paramètre // // [32, 2, 456, 13] => [2, 13, 32, 456] static int[] Trier(int[] tableau) { if (tableau == null) return null; int[] clone = (int[])tableau.Clone(); System.Array.Sort(clone); return clone; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 28 // Ecrire une methode qui renvoie le n-ème élément de la suite de fibonacci // // 0 => 1 // 1 => 1 // 2 => 2 // 3 => 3 // 4 => 5 // 5 => 8 static int Fibo(int nb) { if (nb < 0) return 0; if (nb == 0 || nb == 1) return 1; int a = 1; int b = 1; for (int i = 2; i <= nb; i++) { int temp = a + b; a = b; b = temp; } return b; } ///////////////////////////////////////////////////////////////////////////////////////////////// // Exo 29 // Ecrire une methode qui compte le nombre d'occurence de 2 dans un entier // // 0 => 0 // 2 => 1 // 2222 => 4 // 12345 => 1 // 20202 => 3 // -122 => 2 static int NbDeux(int nb) { string s = nb.ToString(); int count = 0; foreach (char c in s) { if (c == '2') count++; } return count; } ///////////////////////////////////////////////////////////////////////////////////////////////// static int[] tableau; [UnityEditor.MenuItem("KCM/Algos2")] public static void UnityTest2() { tableau = new int[5] { 12, 10, 35, 90, 23 }; string result = ""; if (!(Deux() == 2)) result += "Exercice 01\n"; if (!(Bonjour() == "bonjour")) result += "Exercice 02\n"; if (!(Double(12) == 24)) result += "Exercice 03\n"; if (!(Ajoute10(9) == 19 && Ajoute10(12) == 22)) result += "Exercice 04\n"; if (!(Fois10Plus5(2) == 25)) result += "Exercice 05\n"; if (!(AjouteDavid("Hello ") == "Hello David")) result += "Exercice 06\n"; if (!(Somme(3, 5) == 8)) result += "Exercice 07\n"; if (!(CharacterSize("12345") == 5 && CharacterSize("123456789") == 9)) result += "Exercice 08\n"; if (!(CharacterCut4("123456") == "12356" && CharacterCut4("123") == "123")) result += "Exercice 09\n"; if (!(CharacterX("Kacem") == "Xacem")) result += "Exercice 10\n"; if (EstPalindrome("Kacem") || EstPalindrome("") || !EstPalindrome("radar") || !EstPalindrome("Kayak") || !EstPalindrome("aaaabbaaaa") || EstPalindrome("aaababbbb")) result += "Exercice 11\n"; if (ContientDoublon(new int[5] { -12, -10, -35, -90, -23 }) || !ContientDoublon(new int[5] { -12, -23, -35, -90, -23 }) || !ContientDoublon(new int[2] { 1, 1 }) || !ContientDoublon(new int[4] { 1, 1, 1, 2 }) || ContientDoublon(new int[1] { 1 })) result += "Exercice 12\n"; if (Factorielle(1) != 1 || Factorielle(3) != 6 || Factorielle(5) != 120) result += "Exercice 13\n"; if (!(DivisePar2(12.25f) == 6.125f)) result += "Exercice 14\n"; if (!(Racine(289.0f) == 17.0)) result += "Exercice 15\n"; if (!(PlusGrand(11, 41) == 41 && PlusGrand(27, 11) == 27)) result += "Exercice 16\n"; if (!(PlusGrand(12, 33, 21) == 33 && PlusGrand(-12, -33, -21) == -12)) result += "Exercice 17\n"; if (!(SommeListe(10) == 55 && SommeListe(14) == 105)) result += "Exercice 18\n"; if (!(AdditionNbPairs(10) == 30 && AdditionNbPairs(1) == 0 && AdditionNbPairs(7) == 12)) result += "Exercice 19\n"; if (!(NextString("abcd") == "bcde" && NextString("BBBYYY") == "CCCZZZ" && NextString("") == "")) result += "Exercice 20\n"; if (!(TailleTableau(tableau) == 5)) result += "Exercice 21\n"; if (!(SommeTableau(tableau) == 170)) result += "Exercice 22\n"; if (!(MoyenneTableau(tableau) == 34)) result += "Exercice 23\n"; if (!(PlusGrand(tableau) == 90 && PlusGrand(new int[5] { -12, -10, -35, -90, -23 }) == -10)) result += "Exercice 24\n"; if (!(NbVoyelles("Bonjour") == 3 && NbVoyelles("Elephant") == 3 && NbVoyelles("ouiNON") == 4 && NbVoyelles("0UI") == 2)) result += "Exercice 25\n"; if (!(LettreIdentiqueConcecutive("accident") == 1 && LettreIdentiqueConcecutive("aaaabbb") == 5 && LettreIdentiqueConcecutive("d") == 0 && LettreIdentiqueConcecutive("d") == 0)) result += "Exercice 26\n"; if (!(Trier(tableau)[0] == 10 && Trier(tableau)[1] == 12 && Trier(tableau)[2] == 23 && Trier(tableau)[3] == 35 && Trier(new int[4] { 2, 5, 20, -23 })[0] == -23)) result += "Exercice 27\n"; if (!(Fibo(0) == 1 && Fibo(1) == 1 && Fibo(2) == 2 && Fibo(3) == 3 && Fibo(22) == 28657)) result += "Exercice 28\n"; if (!(NbDeux(2) == 1 && NbDeux(-12) == 1 && NbDeux(2322) == 3 && NbDeux(12342) == 2 && NbDeux(222) == 3 && NbDeux(13456789) == 0)) result += "Exercice 29\n"; if (result.Length > 0) Debug.LogError(result); else Debug.Log("GG!"); } }