I need to find the number of iterations need to reach the given string using character incrementing in php. I have code the below approach but it is a resource and time-consuming.
$x = 'a';
$num = 1;
while ($x != 'zzzz'){
$x ;
$num ;
echo "$num: $x <br>";
}
above code starts lookup from character a
and goes through all the way to zzzz
, is somehow good for small string but it blasts the memory for long strings, is there any shorter way to calculate this?
CodePudding user response:
A simplified answer which reduces the amount of loops to 1 (comments in code)....
// String we want to find the value from
$end = 'ae';
// The list of all possible characters in the alphabet
$alphabet = range('a', 'e');
// Flip this array so that the letter becomes the key and the value is index (0 based)
$alphabetSearch = array_flip($alphabet);
$result = 0;
$endLen = strlen($end);
// Use the target string as an array an iterate over each char
for ($i = 0; $i < $endLen; $i ) {
// Multiply the result by the number of chars in the alphaber to maintain the scale of each char
$result *= count($alphabet);
// Add on the index of the new char ( 1 as it's 0 based)
$result = ($alphabetSearch[$end[$i]] ?? 0) 1;
}
echo $result;
Which in this example (ae with a,b,c,d,e as the alphabet) gives
10
CodePudding user response:
I have implemented this using PHP, the full description is included below but the basic idea is
- find the position of every character of the string in the alphabet table.
- power the 26 to the total number of characters(a-z) - 1.
- multiply the step 2 result by the position of the character on the alphabet table.
code.
/**
* find iteration level from a string using the incrementing approach.
*
* @param $string string
* @return number
*/
public function findCharacterLevelFromString(($string)){
$array = $this->characterLookupOnAlphabetByString($string);
$count = count($array);
$result = 0;
foreach ( $array as $index => $each){
foreach ($each as $rowIndex => $eachRow){
$result = pow(26,$count - 1);
$array[$index]['result'] = $result * $eachRow;
$count--;
}
}
return array_sum(array_column($array,'result'));
}
/**
* do lookup for every character of a string to find their position on the alphabet table.
*
* @param $string string
* @return array
*/
public function characterLookupOnAlphabetByString($string)
{
$x = $string;
$array = str_split($x);
$result = [];
foreach ($array as $indexOfArrary => $eachArray){
foreach ($this->alphabet as $indexOfAlphabet => $eachAlphabet){
if($eachArray == $eachAlphabet){
$result[$indexOfArrary] = [$eachArray => $indexOfAlphabet 1];
}
}
}
return $result;
}
Hi Guys, it might worth to mention it because this operation is needed in many programs and codes such as cryptography, brute force attacks, web scrapping, and many other operations, here is the idea behind the code.
using method characterLookupOnAlphabetByString
we are finding the location of the alphabet character on the alphabet table for example A
is the number 1
st character on the alphabet table Z
is 26
th character on the alphabet table, the method return results in this form.
[
{
"x": 24
},
{
"b": 2
},
{
"c": 3
}
]
I have given the string xbc
to the characterLookupOnAlphabetByString
method and it returned the above array. until now we just find the position of each character of string in the alphabet table. let's go to the main operation
next, we are going to find the total iteration for the string characters. using findCharacterLevelFromString
method. this method will receive a string and then pass it to the characterLookupOnAlphabetByString
method and receive back the above array-like result, next will need to find the number of characters in the given array.
next, we do foreach
loop on the returned array, next, since the index of number for every character is that character we do not know the character which is entered in the string we do another inner foreach
loop for each character(please check the array structure), inside the inner loop we find the power of 26
(which is total number of alphabet character) on (total character - 1) then the result is going to multiply to the position of the character in alphabet table