Last active
December 16, 2015 22:09
-
-
Save iamshanto/5505104 to your computer and use it in GitHub Desktop.
This is another problem solving script. Problem detail can be found at http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=342
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getMirrorChar() | |
{ | |
$data = array( | |
'A' => 'A', | |
'E' => '3', | |
'H' => 'H', | |
'I' => 'I', | |
'J' => 'L', | |
'L' => 'J', | |
'M' => 'M', | |
'O' => 'O', | |
'S' => '2', | |
'T' => 'T', | |
'U' => 'U', | |
'V' => 'V', | |
'W' => 'W', | |
'X' => 'X', | |
'Y' => 'Y', | |
'Z' => '5', | |
'5' => 'Z', | |
'1' => '1', | |
'2' => 'S', | |
'3' => 'E', | |
'8' => '8', | |
); | |
return $data; | |
} | |
function isMirror($str) | |
{ | |
$strLen = strlen($str); | |
$matchChar = ceil($strLen/2); | |
$mirrorChar = getMirrorChar(); | |
for ($i = 0; $i < $matchChar; $i++) { | |
$f = $str[$i]; | |
$l = $str[$strLen - $i - 1]; | |
$mirror = (isset($mirrorChar[$f])) ? $mirrorChar[$f] : ''; | |
if($l != $mirror){ | |
return false; | |
} | |
} | |
return true; | |
} | |
function isPalindrom($str) | |
{ | |
return ($str == strrev($str)); | |
} | |
function getFileContent() | |
{ | |
$file = 'input.txt'; | |
if(!empty($argv[1])){ | |
$file = $argv[1]; | |
}elseif(!empty($_GET['file'])) { | |
$file = $_GET['file']; | |
} | |
if (file_exists($file)) { | |
$content = file_get_contents($file); | |
return array_filter(explode(PHP_EOL, $content)); | |
} | |
return array(); | |
} | |
function start() | |
{ | |
$strings = getFileContent(); | |
$output = array(); | |
$br = (PHP_SAPI == 'cli') ? PHP_EOL : '<br>'; | |
foreach($strings as $string){ | |
$string = strtoupper(preg_replace("([^A-Za-z0-9])", "", $string)); | |
$isMirror = isMirror($string); | |
$isPalindrom = isPalindrom($string); | |
if (!$isMirror && !$isPalindrom) { | |
$output[] = $string . ' -- is not a palindrome.'; | |
} elseif(!$isMirror && $isPalindrom){ | |
$output[] = $string . ' -- is a regular palindrome.'; | |
} elseif($isMirror && !$isPalindrom){ | |
$output[] = $string . ' -- is a mirrored string.'; | |
} elseif($isMirror && $isPalindrom){ | |
$output[] = $string . ' -- is a mirrored palindrome.'; | |
} | |
} | |
echo implode(str_repeat($br, 2), $output).$br; | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment