Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //zadacata u kratko glasi vaka, imame vgnezdena niza (so bezkonechna dubina) i treba da ispintame kolku zborovi od nizata se palindromi. reshena e skroz i se padnala na ispitot vo juni.
- <?php
- // 121156, Nikola Furnadjiski
- function isPalindrome($word)
- {
- $reverse = strrev($word); // reverse the word
- if ($word == $reverse) // compare if the original word is same as the reverse of the same word
- return true;
- else
- return false;
- }
- function getPalindromes($a)
- {
- $palindromes=array();
- $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
- foreach ($it as $v) {
- if(isPalindrome($v))
- array_push($palindromes, $v);
- }
- return array_unique($palindromes);
- }
- // Test Cases!!!
- $arr = array(
- array(5, '' , 'kalabalak'),
- array('agatha', 0.75 , 5,
- array(2, 'a', 1.15)
- ),
- array(
- array('txt'),
- 1.15 , 'this is not a palindrome')
- );
- $arr1 = array('zz', 'txxt', 3);
- print_r(getPalindromes($arr));
- echo "<br>";
- echo "<br>";
- print_r(getPalindromes($arr1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement