Advertisement
koki2000

Look And Say

Jan 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1. <?php
  2. $nums = '11225422';
  3.  
  4. function lookandsay($num)
  5. {   $output = '';
  6.     $firstCharPos = 0;
  7.     $endCharPos = 0;
  8.     for ($i = 0; $i < strlen($num); $i++) {
  9.         if( substr($num, $firstCharPos, 1) != substr($num, $i, 1) )
  10.         {
  11.             $digitsTotal = substr($num, $firstCharPos, $endCharPos);
  12.             $output .= strlen($digitsTotal) . ':' . substr($num, $firstCharPos, 1). ' ';
  13.             $firstCharPos = $i;
  14.             $endCharPos = 0;
  15.         }
  16.         $endCharPos = $endCharPos + 1;
  17.     }
  18.     $digitsTotal = substr($num, $firstCharPos, strlen($num));
  19.     $output .= strlen($digitsTotal) . ':' . substr($num, $firstCharPos, 1). ' ';
  20.     return $output;
  21. }
  22.  
  23. echo lookandsay($nums);
  24. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement