Jump to content

Recommended Posts

Ok, so backstory:

 

I had originally posted asking how a function would work, to convert a string into a number, like below:

a = 1

b = 2

z = 26

aa = 27

zz = 702

aaa = 703

etc...

 

The trick, is that I cannot simply keep incrementing the string in a loop, until it gets the input, and returning how many iterations have run. It needs to be in a "single action", liek below:

 

function let2num3($input)
{
$inputarray = str_split($input);
$inputarray = array_reverse($inputarray, false);
$sumT       = 0;
$i          = 1;

$range = array('a' => 1,  'b' => 2,  'c' => 3,
			   'd' => 4,  'e' => 5,  'f' => 6,
			   'g' => 7,  'h' => 8,  'i' => 9,
			   'j' => 10, 'k' => 11, 'l' => 12,
			   'm' => 13, 'n' => 14, 'o' => 15,
			   'p' => 16, 'q' => 17, 'r' => 18,
			   's' => 19, 't' => 20, 'u' => 21,
			   'v' => 22, 'w' => 23, 'x' => 24,
	           'y' => 25, 'z' => 26
			   );

foreach ($inputarray as $key=>$val)
{
	if ($i==1)
		$sumL = $range[$val];
	else
		$sumL = 26  * $range[$val];
	$sumT = $sumT + $sumL;
	$i++;
}

return $sumT;
}

 

This issue is, that it works up to zz perfectly, but past hat it doesn't return the right number.

 

Help?

 

Thanks,

Chris

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/
Share on other sites

Is it on less, or one more?

 

I am using this function to verify that my "single action" function is correct.

function slow($input)
{
$string = 'a';
$i = 1;
while ($string != $input)
{
	$string++;
	$i++;
}
return $i;
}

 

When aaa is run through it, it outputs 703.

 

Thanks!

Chris

 

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700780
Share on other sites

NVM, I got it!!!!!!

 

4 days of work... Into this...

 

function let2num3($input)
{
$inputarray = str_split($input);
$inputarray = array_reverse($inputarray, false);
$sumT       = 0;
$i          = 0;

$range = array('a' => 1,  'b' => 2,  'c' => 3,
			   'd' => 4,  'e' => 5,  'f' => 6,
			   'g' => 7,  'h' => 8,  'i' => 9,
			   'j' => 10, 'k' => 11, 'l' => 12,
			   'm' => 13, 'n' => 14, 'o' => 15,
			   'p' => 16, 'q' => 17, 'r' => 18,
			   's' => 19, 't' => 20, 'u' => 21,
			   'v' => 22, 'w' => 23, 'x' => 24,
	           'y' => 25, 'z' => 26
			   );

foreach ($inputarray as $key=>$val)
{
	$sumL = pow(26, $i) * $range[$val];
	$sumT = $sumT + $sumL;
	$i++;
}

return $sumT ;
}

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700784
Share on other sites

this is what i got:

 

<?php
function let2num3($input)
{
  $letters = range('a','z');
  $total = 0;
  foreach(array_reverse(str_split($input)) as $n=>$letter){
    $sum = array_search($letter,$letters) + 1;
    if($n)
      $sum = (pow(26,$n) * $sum);
    $total += $sum;
  }
  return $total;
}
print let2num3('aa');
?>

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700788
Share on other sites

yours + mine got me this simpler version:

function let2num3($input)
{
  $letters = range('a','z');
  $sum = 0;
  foreach(array_reverse(str_split($input)) as $i=>$letter)
    $sum += pow(26, $i) * (array_search($letter,$letters) + 1);
  return $sum;
}
print let2num3('zz');

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700791
Share on other sites

or

<?php
function str2int($in){
$out = round(pow(26, strlen($in))/25);
$l = range('a', 'z');
$l = array_flip($l);
$tmp = 0;
for ($i = 0; $i<strlen($in); $i++){
	$tmp = $tmp * 26 + $l[$in[$i]];
}
return $out + $tmp;
}

function int2str($in){
$l = range('a', 'z');
$len = 0;
$base = 1;
while ($in >= $base){
	$len++;
	$in -= $base;
	$base *= 26;
}
$out = '';
for ($i = 0; $i < $len; $i++){
	$out = $l[$in % 26]. $out;
	$in = (int) $in / 26;
}
return $out;
}

echo int2str(335115), "\n";
echo str2int('zz') 
?>

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700867
Share on other sites

Unless I'm missing something, these still all seem long and complex using ranges of A-Z values and powers and reversing the string:

<?php

function columnIDToColumnIndex($columnID) {
$columnIndex = 0;
foreach(str_split(strtoupper($columnID)) as $value) {
	$columnIndex = $columnIndex * 26 + (ord($value) - 64);
}
return $columnIndex;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700883
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.