Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found a solution by myself. </p> <p>Actually I'm checking only "qwerty" keyboards.</p> <p>The <code>getMaxPathLength</code> function gives the maximum path of adjacent keys searching in all the directions.</p> <pre><code>&lt;?php /* * Find the max path in a qwerty keyboard giving an input string Copyright (C) 2013 Danilo Rossini &lt;rossinidan@gmail.com&gt; 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 3 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, see &lt;http://www.gnu.org/licenses/&gt;. USAGE: // Examples $array = array("qwertyui"); //get 8 as result $array = array("quedc"); //get 5 as result $array = array("qaz"); //get 3 as result $array = array("zxcdfvppp"); //get 6 as result $fp = new Path_length("qwertyu"); echo $fp-&gt;getMaxPathLength(); */ class Path_length{ private $maxPathLength = 1; /* * Keyboard layout: QUERTY */ private $matrix = array( array("1","2","3","4","5","6","7","8","9","0"), array("q","w","e","r","t","y","u","i","o","p"), array("a","s","d","f","g","h","j","k","l"), array("z","x","c","v","b","n","m"), ); private $colSize = 10; private $rowSize = 4; private $arrayInput = array(); private $arraySize = null; public function __construct($inputString) { if(!isset($inputString)) die("NULL input array!"); $this-&gt;splitStringInArray($inputString); $this-&gt;cycleMatrix(); } public function getMaxPathLength(){ return $this-&gt;maxPathLength; } /** * Split each element of the string into an array and store it, with his length * in global variables * @param type $string */ private function splitStringInArray($string){ $length = strlen($string); $tmpArray = array(); for ($i=0; $i&lt;$length; $i++) { $tmpArray[$i] = $string[$i]; } $this-&gt;arraySize = $length; $this-&gt;arrayInput = $tmpArray; } /** * Iterate each element of the matrix, calling the function $this-&gt;findPath */ private function cycleMatrix(){ for($i=0;$i&lt;$this-&gt;colSize;$i++){ for($j=0;$j&lt;$this-&gt;rowSize;$j++){ if(isset($this-&gt;matrix[$j][$i]) &amp;&amp; $this-&gt;arrayInput[0]==$this-&gt;matrix[$j][$i]){ $this-&gt;findPath($j, $i, $this-&gt;maxPathLength, 1); } } } } /** * Recursive function that search if the closest element in the matrix (up, down, left, right) * is contained in the input array at the cursor pointer. * It save into $this-&gt;maxPathLength the maximum length path found. * * @param int $a -&gt; x position on the matrix * @param int $b -&gt; y position on the matrix * @param int $max -&gt; max path lenght found until now by the recursive call * @param int $c -&gt; array cursor on the input array * @return int */ private function findPath($a, $b, $max, $c){ $this-&gt;maxPathLength = max(array($this-&gt;maxPathLength, $max)); if($a&gt;=($this-&gt;rowSize-1) &amp;&amp; $b&gt;=($this-&gt;colSize-1)) { return 1; } if($c===$this-&gt;arraySize) { return 1; } /* Search next right key */ if(isset($this-&gt;matrix[$a+1][$b]) &amp;&amp; $this-&gt;matrix[$a+1][$b]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a+1, $b, $max+1, $c+1)){ return 1; } /* Search next bottom key */ if(isset($this-&gt;matrix[$a][$b+1]) &amp;&amp; $this-&gt;matrix[$a][$b+1]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a, $b+1, $max+1, $c+1)){ return 1; } /* Search next left key */ if(isset($this-&gt;matrix[$a-1][$b]) &amp;&amp; $this-&gt;matrix[$a-1][$b]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a-1, $b, $max+1, $c+1)){ return 1; } /* Search next up key */ if(isset($this-&gt;matrix[$a][$b-1]) &amp;&amp; $this-&gt;matrix[$a][$b-1]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a, $b-1, $max+1, $c+1)){ return 1; } return 0; } } </code></pre> <p><strong>Edit</strong></p> <p>New version of the previous code with detection of shifted keys.</p> <pre><code>&lt;?php /* * Find the max path in a qwerty keyboard giving an input string Copyright (C) 2013 Danilo Rossini &lt;rossinidan@gmail.com&gt; 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 3 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, see &lt;http://www.gnu.org/licenses/&gt;. USAGE: // Examples $string = "qwertyui"; //get 8 as result $string = "qwedc"; //get 5 as result $fp = new Path_length("qwertyu"); echo $fp-&gt;getMaxPathLength(); */ class Path_length{ private $maxPathLength = 1; /* * Keyboard layout: QUERTY */ private $keyLower = array( array("1","2","3","4","5","6","7","8","9","0"), array("q","w","e","r","t","y","u","i","o","p"), array("a","s","d","f","g","h","j","k","l"), array("z","x","c","v","b","n","m"), ); private $keyUpper = array( array("1","2","3","4","5","6","7","8","9","0"), array("Q","W","E","R","T","Y","U","I","O","P"), array("A","S","D","F","G","H","J","K","L"), array("Z","X","C","V","B","N","M"), ); private $matrix = array(); private $colSize = 10; private $rowSize = 4; private $arrayInput = array(); private $arraySize = null; public function __construct($inputString) { if(!isset($inputString) || !is_string($inputString)) die("Invalid input string!"); $this-&gt;initKeyboard(); $this-&gt;splitStringInArray($inputString); $this-&gt;cycleMatrix(); } private function initKeyboard(){ $this-&gt;matrix[0] = $this-&gt;keyLower; $this-&gt;matrix[1] = $this-&gt;keyUpper; } public function getMaxPathLength(){ return $this-&gt;maxPathLength; } /** * Split each element of the string into an array and store it, with his length * in global variables * @param type $string */ private function splitStringInArray($string){ $length = strlen($string); $tmpArray = array(); for ($i=0; $i&lt;$length; $i++) { $tmpArray[$i] = $string[$i]; } $this-&gt;arraySize = $length; $this-&gt;arrayInput = $tmpArray; } private function isUpper($chr){ return ctype_upper($chr) ? 1 : 0; } /** * Iterate each element of the matrix, calling the function $this-&gt;findPath */ private function cycleMatrix(){ for($i=0;$i&lt;$this-&gt;colSize;$i++){ for($j=0;$j&lt;$this-&gt;rowSize;$j++){ for($c=0; $c&lt;$this-&gt;arraySize; $c++) { $isUp = $this-&gt;isUpper($this-&gt;arrayInput[$c]); if( isset($this-&gt;matrix[$isUp][$j][$i]) &amp;&amp; $this-&gt;arrayInput[$c]===$this-&gt;matrix[$isUp][$j][$i]){ $this-&gt;findPath($j, $i, 1, $c+1, $isUp); } } } } } /** * Recursive function that search if the closest element in the matrix (up, down, left, right) * is contained in the input array at the cursor pointer. * It save into $this-&gt;maxPathLength the maximum length path found. * * @param int $a -&gt; x position on the matrix * @param int $b -&gt; y position on the matrix * @param int $max -&gt; max path lenght found until now by the recursive call * @param int $c -&gt; array cursor on the input array * @return int */ private function findPath($a, $b, $max, $c, $isUp){ $this-&gt;maxPathLength = max(array($this-&gt;maxPathLength, $max)); if($a&gt;=($this-&gt;rowSize-1) &amp;&amp; $b&gt;=($this-&gt;colSize-1)) { return 1; } if($c===$this-&gt;arraySize) { return 1; } /* Search next right key */ if(isset($this-&gt;matrix[$isUp][$a+1][$b]) &amp;&amp; $this-&gt;matrix[$isUp][$a+1][$b]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a+1, $b, $max+1, $c+1, $isUp)){ return 1; } /* Search next bottom key */ if(isset($this-&gt;matrix[$isUp][$a][$b+1]) &amp;&amp; $this-&gt;matrix[$isUp][$a][$b+1]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a, $b+1, $max+1, $c+1, $isUp)){ return 1; } /* Search next left key */ if(isset($this-&gt;matrix[$isUp][$a-1][$b]) &amp;&amp; $this-&gt;matrix[$isUp][$a-1][$b]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a-1, $b, $max+1, $c+1, $isUp)){ return 1; } /* Search next up key */ if(isset($this-&gt;matrix[$isUp][$a][$b-1]) &amp;&amp; $this-&gt;matrix[$isUp][$a][$b-1]===$this-&gt;arrayInput[$c] &amp;&amp; $this-&gt;findPath($a, $b-1, $max+1, $c+1, $isUp)){ return 1; } return 0; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload