Jump to content

Represent Numbers As Desired Alphabets


s4surbhi2218

Recommended Posts

Hi,

I want to code such that

it represents numbers from 0-9, a-z, A-Z and then from 10-19,1a-1z,1A-1Z and so on

example:

no change for numbers from 0-9

numbers 10,11,12......upto 36 are represented by alphabets a,b,c.....upto z

numbers 37,38,39......upto 62 are represented by alphabets A,B,C.....upto Z

numbers 63 , 64 ...upto 73 are repreneted by 10,11,12,13 and so on upto 19

from 74 ,,,,again 1a follows

 

Whats the best/wisest way to do that?Any suggestions would be great.

Thanks.

Link to comment
Share on other sites

Create an array using range () and array_merge (), and match the number with the position (index). Voilá, you got the correct "letter". ;)

 

Alternatively, if that array never (or almost never) changes you could print out the result. Then replace the generation code with the actual array. Shouldn't be necessary though, unless you're having a high load, and the benchmark shows that said code is taking up a lot of the time.

Edited by Christian F.
Link to comment
Share on other sites

Create an array using range () and array_merge (), and match the number with the position (index). Voilá, you got the correct "letter". ;)

 

Alternatively, if that array never (or almost never) changes you could print out the result. Then replace the generation code with the actual array. Shouldn't be necessary though, unless you're having a high load, and the benchmark shows that said code is taking up a lot of the time.

did this

<?php

$n = 35;//example

$arrSmall = array();

$arrSmallNum = array();

$arrSmall = range('a','z');

print_r($arrSmall);

$arrSmallNum = range('10','35');

print_r($arrSmallNum);

 

$index = array_search($n,$arrSmallNum);echo $index;

echo "-------".$arrSmall[$index]."------";

?>

 

But in this i would need to create an array again an again moreover how would i create an array for range('1a','1z')?? :(

Link to comment
Share on other sites

You seem to have missed most of my post, in particular the second paragraph.

 

As for generating an array with numbers and letters, this is one way to do it:

$l = array (); $r=0;
for ($c = ord ('a'); $c <= ord ('z'); $c++) {
   $l[++$r] = '1'.chr ($c);
}

 

PS: No need to use array_search () either. Not only won't it do what you've described, but isset ($arr[$index]) does the job a lot easier than array_key_exists ().

Edited by Christian F.
Link to comment
Share on other sites

did this

<?php

$n = 35;//example

$arrSmall = array();

$arrSmallNum = array();

$arrSmall = range('a','z');

print_r($arrSmall);

$arrSmallNum = range('10','35');

print_r($arrSmallNum);

 

$index = array_search($n,$arrSmallNum);echo $index;

echo "-------".$arrSmall[$index]."------";

?>

 

But in this i would need to create an array again an again because i want it for upto 1000 approx moreover how would i create an array for range('1a','1z')?? :(

Link to comment
Share on other sites

You seem to have missed most of my post, in particular the second paragraph.

 

As for generating an array with numbers and letters, this is one way to do it:

$l = array (); $r=0;
for ($c = ord ('a'); $c <= ord ('z'); $c++) {
$l[++$r] = '1'.chr ($c);
}

 

PS: No need to use array_search () either. Not only won't it do what you've described, but isset ($arr[$index]) does the job a lot easier than array_key_exists ().

 

 

I can see your point , also the snippet is able to generate one of the required arrays but my problem is that i have to represent numbers in different range differently ,as number in the range 10, 35 would be represented by alphabets in range a-z for this i would have to generate many such arrays moreover i want a representation for 1000 numbers approx .

 

Please correct me if i am getting you wrong.

Edited by s4surbhi2218
Link to comment
Share on other sites

In case you didn't understand my hint, in other words the first paragraph of my last post: I have corrected you. You need to re-read my first post. I've already covered all of your concerns.

 

Only, in this case I think you can safely forgo the benchmark, since your array is as large as it is.

Edited by Christian F.
Link to comment
Share on other sites

Maybe I'm just reading your question wrong or maybe it is just too early in the morning, but I cannot seem to grasp the logic of what you want to accomplish.

 

From what I understand, the letter a would represent 10?

The number 36 would be represented by z?

Afterthat I get completely lost?

 

What exactly would 3A represent? or 1A, 1G, etc..

 

It would help if you could elaborate more on what certain numbers would be represented by.

Link to comment
Share on other sites

Whats the best/wisest way to do that? Any suggestions would be great.

 

You are essentially looking to change the numbers from base 10 to base 62.  PHP already has tools available to do this work for you, in the GMP extension.

 

A basic example would look like:

 

echo gmp_strval(gmp_init(12345, 10), 62);
// Outputs: 3D7

 

Note that the above will use a slightly different map of characters than you mentioned in the first post (A-Z comes before a-z). If you want to keep your specified letter order then you could use something like strtr() to convert upper- to lower-case and vice versa.

 

P.S. Your math in the first post is a little off at the boundaries, for example decimal 35 (not 36) should be z.

Edited by salathe
Link to comment
Share on other sites

He wants the number to be the index of an array, which returns the desired letter, number or combination of such. Since the first ten items in the array is the numbers 0-9, the 10th index would give the lowercase letter "a". (Also since 26+10=36 index 36 would give the letter 'z', or at least if we account for the goalpost issue in the OPs description. :P )

 

That said, I do agree that the logic is really convoluted, and I'm almost positive that there has to be a better way to do what the OP is trying to do. Whatever that is. I cannot see any need for a 1000+ item array doing this kind of substitution.

Edited by Christian F.
Link to comment
Share on other sites

True, I did. After your post, I think you might be onto something. And if that what s/he wants to accomplish, then I agree that using GMP would be the best solution.

Other than that, a static array with the key-value associations is probably the "best" method for something like this.

Link to comment
Share on other sites

You are essentially looking to change the numbers from base 10 to base 62. PHP already has tools available to do this work for you, in the GMP extension.

 

A basic example would look like:

 

echo gmp_strval(gmp_init(12345, 10), 62);
// Outputs: 3D7

 

Note that the above will use a slightly different map of characters than you mentioned in the first post (A-Z comes before a-z). If you want to keep your specified letter order then you could use something like strtr() to convert upper- to lower-case and vice versa.

 

P.S. Your math in the first post is a little off at the boundaries, for example decimal 35 (not 36) should be z.

 

Yes i agree, it was really great knowing this.

Edited by s4surbhi2218
Link to comment
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.