diff --git a/Faker.php b/Faker.php index 0576c3cd38dd4dbd79ba07fe8cc150c6716387b4..97a1c083e65c2aedc95d9e6b34b4deb61ce0da9e 100644 --- a/Faker.php +++ b/Faker.php @@ -2,10 +2,84 @@ class Faker { - private array $code = ['Filter a array and exclude element if key is "chapeau"', 'Check if word is a palindromic word', 'return a number of occurence for word into a text']; - private array $test = ["If you're using JavaScript, you can use the filter method to exclude elements with a key of 'chapeau'. Here's an example:", - "A palindromic word is a word that reads the same backward as forward. To check if a word is a palindromic word, you can compare its reverse to the original word. Here's an example in JavaScript:", - "To return the number of occurrences of a word in a text, you can use the split method to convert the text into an array of words, and then use the filter method to count the number of elements in the array that match the target word. Here's an example in JavaScript:"]; + private array $code = [' + public static int calculateSum(int[] nums) { + int sum = 0; + for (int num : nums) { + sum += num; + } + return sum; + } + ', + ' + public String[] filterArray(String[] array) { + // Create a new array to hold the filtered elements + List<String> filtered = new ArrayList<String>(); + + // Iterate over the input array and exclude elements with the key "chapeau" + for (String element : array) { + if (!"chapeau".equals(element)) { + filtered.add(element); + } + } + + // Convert the filtered list back to an array + String[] result = new String[filtered.size()]; + filtered.toArray(result); + return result; + ', + ' + public boolean isPalindrome(String word) { + // Convert the word to lowercase and remove non-alphanumeric characters + String cleanWord = word.toLowerCase().replaceAll("[^a-z0-9]", ""); + + // Compare the original word with its reversed version + String reversedWord = new StringBuilder(cleanWord).reverse().toString(); + return cleanWord.equals(reversedWord); + } + ']; + private array $test = [' + public void testIsPalindrome() { + String word1 = "racecar"; + boolean expected1 = true; + boolean actual1 = isPalindrome(word1); + assertEqual(expected1, actual1); + + String word2 = "hello"; + boolean expected2 = false; + boolean actual2 = isPalindrome(word2); + assertEqual(expected2, actual2); + } + ', + + ' + public void testFilterArray() { + String[] input1 = {"apple", "banana", "chapeau", "durian"}; + String[] expected1 = {"apple", "banana", "durian"}; + String[] actual1 = filterArray(input1); + assertArrayEqual(expected1, actual1); + + String[] input2 = {"a", "b", "c", "d"}; + String[] expected2 = {"b", "c"}; + String[] actual2 = filterArray(input2); + assertArrayEqual(expected2, actual2); + } + ', + ' + public void testCalculateSum() { + int[] nums1 = {1, 2, 3, 4, 5}; + int expected1 = 15; + int actual1 = calculateSum(nums1); + assertEqual(expected1, actual1); + + int[] nums2 = {-1, 0, 1}; + int expected2 = 0; + int actual2 = calculateSum(nums2); + assertEqual(expected2, actual2); + } + + + ']; public int $number;