Wednesday, March 13, 2013

Palindromes search

The task is to find all the palindromes (spells the same from right and left) in the given text and print them with the number of occurrences. Minimum length of string is 2 since a character is a palindrome.

Here is an updated solution (thx Denis & Tim for pointing it out.):


<?php
// Given a string, test it for Palindromes, then output them along with the number of times they occur?
//sample string
$s = 'cd111 ana 876 qqqqqwq 00112 čćčćććččć把ŠŠđđđđđĐĐĐĐĐđččć';


//array to store results
$res = array();

mb_regex_encoding('UTF-8');
mb_internal_encoding('UTF-8');

//borrowed from http://www.codigomanso.com/en/2009/02/convert-utf8-string-to-array-in-php/
// using mb_check_encoding instead of mb_substr ;)
function getCharArray ($jstring) {
  if (mb_strlen($jstring, 'UTF-8') == 0)
    return array();

  $ret = array();
  $alen = strlen($jstring);
  $char = '';
  for ($i = 0; $i < $alen; $i++) {
    $char .= $jstring[$i];
    if (mb_check_encoding($char, 'UTF-8')) {
      array_push($ret, $char);
      $char = '';
    }
  }
  return $ret;
}

//mimumum lenght is 2
for ($l = 2; $l < mb_strlen($s) + 1; $l++) {
  //get all the posible subsrings with lenght above 2
  for ($i = 0; $i < (mb_strlen($s) + 1 - $l); $i++) {
    $g = mb_substr($s, $i, $l);

    //reverse $g string,
    //could use strrev but it is not multibyte safe
    //$rg = strrev($g);
    //multibyte safe version
    $t = getCharArray($g);
    $t = array_reverse($t);
    $rg = implode($t);

    //compare and store to result array
    if ($g == $rg) {
      //if match
      if (array_key_exists($g, $res)) {
        $res[$g] = $res[$g] + 1;
      }
      else {
        $res[$g] = 1;
      }
    }
  }
}

//output the results
foreach ($res as $r => $v) {
  echo "$r:$v\n";
}
?>

Reverse order of words

Here is a simple programming problem. Take the string "Done is better than perfect" and reverse the order of the words. Final output should be "Perfect than better is done"

Here is a very simple solution written in PHP.


<?php 

//original string
$s= 'Done is better than perfect';
//put all the words to lowercase
$s= mb_strtolower($s); 

//split the string by spaces and put in in an array
$arr = explode(" ", $s);
//reverse sort the array
$arr = array_reverse($arr);


//Put the first char of first word to uppercase
$arr[0]=ucfirst($arr[0]);

//print the output
echo implode(' ', $arr);
?>

Tuesday, March 5, 2013

PHP getopt alternative

Are you using getopt. It works but if you are stuck in legacy version of PHP you will miss long options. And what if you do not wan't to use '-' as separator. We could use the idea from Bash and array_shift parameters. See the sample bellow.

<?php
function DisplayHelp () {
  echo "Usage:\n";
  echo "     -f, --folder folder to produce output to  \n";
  echo "     -s, --server ftp server to use \n";
  echo "     -u, --user ftp server user  \n";
  echo "     -p, --pass ftp server password  \n";
  echo "     --port ftp server port, defaults to 21  \n";
  echo "     -d, --debug debug mode \n";
  echo "     -l, --log location";
  echo "     -h, --help display this help and exit";
  echo "\n";
  exit();
}

$options = array();

if (!(php_sapi_name() == 'cli')) {
  echo 'You need to run this from console.';
  exit();
}

//remove the name of the executing script
array_shift($argv);

//no options chosen
if (!is_array($argv))
  DisplayHelp();

//parse parameters
while (($param = array_shift($argv)) !== NULL) {
  switch ($param) {
    case '--folder':
    case '-f':
      $options['folder'] = array_shift($argv);
      break;
    case '--server':
    case '-s':
      $options['server'] = array_shift($argv);
      break;
    case '--user':
    case '-u':
      $options['user'] = array_shift($argv);
      break;
    case '--pass':
    case '-p':
      $options['pass'] = array_shift($argv);
      break;
    case '--port':
      $options['port'] = array_shift($argv);
      break;
    case '--debug':
    case '-d':
      $options['debug'] = TRUE;
      break;
    case '--log':
    case '-l':
      $options['log'] = array_shift($argv);
      break;
    case '--help':
    case '-h':
      DisplayHelp();
      break;
    default:
      DisplayHelp();
      break;
  }
}

//show options
print_r($options)
?>