function isPalindrome($word) { echo "{$word}\n"; $wordLength = strlen($word); echo "word length: {$wordLength}\n"; $midPoint = (int) ceil($wordLength / 2) - 1; $midPointLetter = $word[$midPoint]; echo "mid point index: {$midPoint}, {$midPointLetter}\n"; $prevLetters = array(); for ($currLetIdx = 0; $currLetIdx < $wordLength; $currLetIdx++) { $currentLetter = $word[$currLetIdx]; $prevLetters[] = $currentLetter; $comparisonIndex = $wordLength - ($currLetIdx + 1); if ($currLetIdx > $midPoint) { echo "current index:" . $currLetIdx . "\n"; echo "current letter:" . $currentLetter . "\n"; echo "comparison index:" . $comparisonIndex . "\n"; echo "comparison letter:" . $prevLetters[$comparisonIndex] . "\n"; } if ($currLetIdx > $midPoint && $currentLetter != $prevLetters[$comparisonIndex]) { echo "stopping ...\n"; return false; } } return true; } $palindromes = array('noon', 'civic', 'reviver', 'word', 'racecar', 'redder'); foreach ($palindromes as $palindrome) { var_dump(isPalindrome($palindrome)); echo "\n"; }
<?php /** * Largest palindrome product * https://projecteuler.net/problem=4 */ require '../show_answer.php'; $startTime = microtime(true); $palindroms = []; for ($i = 100; $i <= 999; $i++) { for ($j = $i; $j <= 999; $j++) { $production = $i * $j; if (isPalindrome($production)) { $palindroms[] = $production; } } } $result = max($palindroms); showAnswer($result, $startTime); function isPalindrome($value) { return $value == strrev($value); }
//we join the letters with s space between them } function shuffleStr($str) { $arrChars = str_split($str); shuffle($arrChars); $shuffledStr = implode('', $arrChars); return $shuffledStr; } if (isset($_POST['submit'])) { if (isset($_POST['radios']) && isset($_POST['str'])) { $case = $_POST['radios']; $str = $_POST['str']; switch ($case) { case "0": echo isPalindrome($str) ? $str . " is a palindrome!" : $str . " is not a palindrome!"; break; case '1': echo strrev($str); break; case '2': echo splitStr($str); break; case '3': echo crypt($str, strrev($str)); break; case '4': echo shuffleStr($str); break; } } else {
<?php $message = $_POST['message']; echo "Vneseno besedilo: " . $message . "<br><br>"; if (isPalindrome($message) == true) { echo '"Vneseno besedilo JE PALINDROM"'; } else { echo ' Vneseno besedilo ni NI PLANINDROM '; } // funkcija - preveri ali je vneseno besedilo palindrom function isPalindrome($message) { // odstarni presledke iz besedila $input = stristr(' ', $message); // preveri ali se besedilo brez presledkov bere enako naprej kot nazaj if ($input == strrev($input)) { return true; } else { return false; } }
function isPalindrome($string) { if (substr($string, 0, 1) == substr($string, strlen($string) - 1, 1)) { if (strlen($string) > 3) { return isPalindrome(substr($string, 1, strlen($string) - 2)); } else { return true; } } else { return false; } }
function q36($max) { $sum = 0; for ($i = 0; $i < $max; ++$i) { $base2 = base_convert($i, 10, 2); $base10 = (string) $i; $isPalindromeInBase2 = isPalindrome($base2); $isPalindromeInBase10 = isPalindrome($base10); if ($isPalindromeInBase2 && $isPalindromeInBase10) { $sum += $i; } } return $sum; }
/** * Checks if a string is a palindrome. * * @param {string} $text - The text to test. * @return {boolean} true if $text is a palindrome, false otherwise. */ function isPalindrome($text) { // Let's cache the string length since we are using it more than once. $len = mb_strlen($text); // Matches rule 1. if ($len <= 1) { return true; } // Matches rule 2a. if (mb_substr($text, 0, 1) !== mb_substr($text, $len - 1, 1)) { return false; } // Matches rule 2b. Recursive invokes itself to verify rules 1 and 2a. return isPalindrome(mb_substr(1, $len - 2)); }
function largestPalindrome() { $lastProduct = 0; for ($i = 999; $i > 0; $i--) { for ($j = 999; $j > 0; $j--) { $thisProduct = $i * $j; if (isPalindrome($thisProduct)) { if ($thisProduct < $lastProduct) { break 2; } else { $lastProduct = $thisProduct; } } } } return $lastProduct; }
function largestPalindrome($max = 999, $min = 100) { $maxPalindrome = 0; for ($i = $max; $i > $min; $i--) { // To avoid checking both a*b and b*a, we assume one number is smaller // than the other for ($j = $max; $j > $i; $j--) { $product = $i * $j; // Early break if the product is already smaller than the max Pal if ($product < $maxPalindrome) { break; } if (isPalindrome($product)) { $maxPalindrome = max($maxPalindrome, $product); } } } echo $maxPalindrome; }
function getPalindrome($str) { if (isPalindrome($str)) { return $str; } $palLen = 1; $palText = mb_substr($str, 0, 1, "UTF-8"); $stLen = mb_strlen($str, "UTF-8"); for ($i = 0; $i < $stLen; $i++) { for ($j = $i + 1; $j <= $stLen - 1; $j++) { $part = trim(mb_substr($str, $i, $j - $i + 1, "UTF-8")); $partLen = isPalindrome($part); if ($partLen > $palLen) { $palLen = $partLen; $palText = $part; } } } return $palText; }
и Всем пока Перенос строк чому-то тут не работает тоже :c EOF ; echo $bigText; echo mb_strlen($bigText); */ $textPalindrome = "А роза упала на лапу Азора"; $textNonPalindrome = "Я совсем не палиндром"; function isPalindrome($string) { $string = str_replace(" ", "", mb_strtolower($string)); /* * $ebuchka = 1; ты была хорошим другом, ебучка. */ for ($i = 0; $i <= floor(mb_strlen($string) / 2); $i++) { if (mb_substr($string, $i - 1, 1) != mb_substr($string, -$i, 1)) { return false; break; } } return true; } echo isPalindrome($textNonPalindrome); echo "<br>"; echo isPalindrome($textPalindrome); echo "<br>";
case 'reverse': $output = strrev($str); break; case 'split': $letters = []; preg_match_all('/[a-zA-Z]/', $str, $letters); $output = implode(' ', $letters[0]); break; case 'hash': $output = crypt($str, 'defsalt'); break; case 'shuffle': $output = str_shuffle($str); break; default: $output = isPalindrome($str) ? $str . ' is palindrome!' : $str . ' is not a palindrome!'; break; } } ?> <!DOCTYPE html> <html> <head> <title>Modify String</title> </head> <body> <form> <input type="text" name="inputString"> <input type="radio" name="op" value="isPalindrome" id="isPalindrome" checked> <label for="isPalindrome">Check Palindrome</label> <input type="radio" name="op" value="reverse" id="reverse">
* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ function isPalindrome($n) { return strrev($n) == $n ? true : false; } $result = ""; for ($i = 100; $i <= 999; $i++) { for ($j = 100; $j <= 999; $j++) { if (isPalindrome($i * $j)) { $result = $i . ", " . $j . " = " . $i * $j; } } } echo $result . "\n";
$lastIndex = $len - 1; $result = true; if ($len % 2 == 0) { //if even $halfLen = $len / 2; for ($i = 0; $i < $halfLen; $i++) { if ($str[$i] == $str[$lastIndex - $i]) { $result = $result && true; } else { $result = $result && false; } } return $result; } else { //if odd $halfLen = floor($len / 2); for ($i = 0; $i < $halfLen; $i++) { if ($str[$i] == $str[$lastIndex - $i]) { $result = $result && true; } else { $result = $result && false; } } return $result; } } var_dump(isPalindrome('mom')); var_dump(isPalindrome('hannah')); var_dump(isPalindrome('not a palindrome')); var_dump(isPalindrome('really not a palindrome'));
// modifies it according to the selected option (radio button). You should support the following // operations: palindrome check, reverse string, split to extract leters only, hash the string // with the default PHP hashing algorithm, shuffle the string characters randomly. The result // should be displayed right under the input field. Styling the page is optional. Think about // which of the modification can be achieved with already built-in functions in PHP. // Where necessary, write your own algorithms to modify the given string. Hint: Use the crypt() // function for the "Hash String" modification. header("Content-Type: text/html; charset=utf8"); require './functions.php'; if (!empty($_POST['user-input'])) { $string = htmlentities($_POST['user-input']); $selection = htmlentities($_POST['option']); $result = ''; switch ($selection) { case 'palindrome': if (isPalindrome($string)) { $result = "{$string} is a palindrome!"; } else { $result = "{$string} is not a palindrome!"; } break; case "shuffle": $result = str_shuffle($string); break; case "reverse": $result = reverse($string); break; case "hash": $result = password_hash($string, PASSWORD_BCRYPT); break; case "split":
<?php function isPalindrome($word) { $word = strtolower(str_replace(" ", "", $word)); $stack = new SplStack(); $cnt = strlen($word); for ($i = 0; $i < $cnt; ++$i) { $stack->push($word[$i]); } $rword = ""; while ($stack->count() > 0) { $rword .= $stack->pop(); } return $word == $rword; } $word = "hello"; if (isPalindrome($word)) { print $word . " is a palindrome.\n"; } else { print $word . " is not a palindrome.\n"; }
if (isset($_POST['string'], $_POST['option'])) { function isPalindrome($string) { $a = strtolower(preg_replace("/[^A-Za-z0-9]/", "", $string)); return $a == strrev($a); } function splitLetters($string) { $letters = preg_replace('/[^A-Za-z]/', '', $string); return implode(' ', str_split($letters)); } $string = $_POST['string']; $option = $_POST['option']; switch ($option) { case "Check Palindrome": $output = "'{$string}' is " . (isPalindrome($string) ? "a palindrome." : "not a palindrome!"); break; case "Reverse String": $output = strrev($string); break; case "Split": $output = splitLetters($string); break; case "Hash String": $output = crypt($string); break; case "Shuffle String": $output = str_shuffle($string); break; } ?>
/* * Задача 8: Да се състави програма, чрез която се въвежда ред от символи (стринг, низ). Програмата да изведе на екрана дали въведения стринг е палиндром, т.е. дали четен отляво-надясно и отдясно-наляво е един и същ. Вход: капак Изход: да. Вход: тенджера Изход: не. */ require_once 'readline.php'; //require_once 'functions.php';с function isPalindrome(&$string) { $reverseString = strrev($string); $stringToCompare = md5($string); $reverseStringToCompare = md5($reverseString); if (strcmp($stringToCompare, $reverseStringToCompare) == 0) { return true; } else { return false; } } $input = readline('Enter some string: '); $result = isPalindrome($input); if ($result) { echo 'Yes!'; } else { echo 'No!'; }
*/ /** * Palindromes * racecar * * take a string, identify if palindrome or not * * Also do other homework */ function isPalindrome($word) { $wordLength = strlen($word); $lastIndex = $wordLength - 1; $k = $lastIndex; $reverse = ""; for ($i = 0; $i <= $lastIndex; $i++) { $reverse = $reverse . $word[$k]; $k = $k - 1; } if ($word == $reverse) { echo $word . ' is palindrome'; } else { echo $word . ' is not palindrome'; } } echo isPalindrome('abba'); echo '<br>'; echo isPalindrome('qwertyuiopl'); echo '<br>'; echo isPalindrome('racecar');
<?php $min = 100; $max = 999; $palindromes = []; for ($i = $max; $i >= $min; $i--) { for ($j = $max; $j >= $min; $j--) { $product = $i * $j; if (isPalindrome($product)) { $palindromes[] = $product; } } } sort($palindromes); var_dump(end($palindromes)); function isPalindrome($number) { $string = (string) $number; if (strrev($string) == $string) { return true; } return false; }
require_once "../functionExLib.php"; ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form method="POST"> Input String to see if it is a palindrome: <input type="text" name="input" autofocus /> <input type="submit" value="Submit" /> </form> <?php if (isset($_POST['input'])) { print isPalindrome($_POST['input']) . "<br />"; } ?> <!-- put a button on the form to reset it --> <a href="<?php echo $_SERVER['PHP_SELF']; ?> ">Reset</a> <?php ?> </body> </html>
<?php /** * Largest palindrome product * * A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. * Find the largest palindrome made from the product of two 3-digit numbers. */ $largest_3digit_nr = 999; $largest_3digit_nr_2nd = 999; $palindromes = array(); $largest_palindrome = 0; for ($i = $largest_3digit_nr; $i >= 100; $i--) { for ($y = $largest_3digit_nr_2nd; $y >= 100; $y--) { if (isPalindrome($i * $y)) { $palindromes[] = $i * $y; } } } $largest_palindrome = max($palindromes); echo $largest_palindrome; function isPalindrome($number) { $number_array = array(); $number_length = strlen($number); if ($number_length < 2) { return true; } else { for ($i = 0; $i < $number_length; $i++) { $last_digit = $number % 10; $number = floor($number / 10);
<?php require_once '../functions.php'; $nums1 = $nums2 = range(999, 100); $wanted = 0; foreach ($nums1 as $num1) { foreach ($nums2 as $num2) { $num = $num1 * $num2; if (isPalindrome($num) && $num > $wanted) { $wanted = $num; } } } echo $wanted;
<?php // duncaneuler36.php $start = microtime(true); $sum = 0; function isPalindrome($x) { return $x == strrev($x); } for ($i = 1; $i <= 999999; $i++) { if (isPalindrome($i) && isPalindrome(decbin($i))) { echo $i . " " . decbin($i) . "<br>"; $sum += $i; } } echo $sum . "<hr>"; $end = microtime(true); printf("Execution time: %dms", ($end - $start) * 1000);
<?php class Palindrome { public static function isPalindrome($str) { throw new Exception('Not implemented'); } } // TESTE echo isPalindrome('A grama e amarga.');