1

I am trying to generate some SKU numbers and I came to an issue that has made me think, and as I slept less than 2 hours I decided to ask you guys, Stackoverflowers.

Let's say I've got an array of the alphabet excluding commonly mistaken letters.

$alphabet = array("A","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z");

I am trying to generate 2 letters based on consistent number. Let's say I've got sub-products that I want to have that suffix in the end of their SKU. For the first sub-product the SKU will have suffix - AA, for the 24th - AZ, 25th - CA, 26th - CC and so on. The thing is that we don't want to have repeating suffixes, but AC and CA are acceptable.

Thank you for doing the dirty job for a sleep needing programmer.

Making it clear: I want to get a combination based on irritation. Let's say:

$i = 1, then $suffix = AA; 
$i = 2, then $suffix = AC; 
$i = 24, then $suffix = ZZ; 
$i = 25 (one above the count of the array), then $suffix = CA; 
$i = 26, then $suffix = CC;
$i = 49, then $suffix = DA (**I suppose**)

Let's say I have sub-products for product 1 and sub-products for product 2. Product 1's sub-products' suffixes should be:

AA, AC, AD, AE .... AZ, CA, CC, CD .... CZ .... ZA, ZC ... ZY.

Product 2's sub-products' suffixes can also be the same!

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
Dimitar Mitov
  • 37
  • 2
  • 7
  • 1
    Your spec is unclear. Provide a few examples of input and expected output. – Jason McCreary Mar 13 '15 at 13:35
  • There's no possibly way of knowing if they're duplicates or not unless you cross reference them to suffixes already created. – Andrei P. Mar 13 '15 at 13:36
  • Let's say I have sub-products for product 1 and sub-products for product 2. Product 1's sub-products' suffixes should be: AA, AC, AD, AE .... AZ, CA, CC, CD .... CZ .... ZA, ZC ... ZY. Product 2's sub-products' suffixes can also be the same! – Dimitar Mitov Mar 13 '15 at 13:38
  • Your question is not clear. Please add concrete example cases. Shouldn't you be saying: 1st is AA, 24th is AZ (not ZZ), 25th is CA (not AC), 26 is CC (not AD) ? – nl-x Mar 13 '15 at 13:40
  • That's right, @nl-xm I appologize. – Dimitar Mitov Mar 13 '15 at 13:53
  • Why is 2 `AC`. Following your rule it should be `AB`, right? – ʰᵈˑ Mar 13 '15 at 13:58
  • B is excluded from the array, as it's commonly mistaken with 8 in some fonts. – Dimitar Mitov Mar 13 '15 at 13:59

2 Answers2

0

I think that's not a good conception because you will be limited in the time with this solution.. what I mean is that it's not unlimited.

If you really want to do this, I think you have to construct a kind of array with all the solution and index it with a number and when you create a new product, just know the number of product you have and take the next one.

djoosi
  • 236
  • 2
  • 9
  • I am aware of that. We certainly don't plan to have more sub-products than the possible 2 letter combinations. The idea is their SKU to be [ProductSKU][2Letters] – Dimitar Mitov Mar 13 '15 at 13:52
0

I would use the product number to pick two indexes from the list of available letters The following can be done in one line, but I expand it so I can explain what it does.

function get2letter($num)
{
    $alphabet = array("A","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z");
    $code = $alphabet[$num%sizeof($alphabet)]; // Get the first letter
    $num = floor($num/sizeof($alphabet)); // Remove the value used to get the first letter
    $code.= $alphabet[$num%sizeof($alphabet)]; // Get the second letter
    return $code;
}

for($i=0; $i<10; $i++)
    print get2letter($i)."\n";

This will work for small values. You have collisions when you surpass the number of unique values you can represent with your alphabet.

kainaw
  • 4,256
  • 1
  • 18
  • 38
  • That is a good example, except that the second generated string should be "AC", not "CA" and so on. – Dimitar Mitov Mar 13 '15 at 14:08
  • I didn't realize you wanted them in a particular order. Where it says "get the second letter", change it to: $code = $alphabet[$num%sizeof($alphabet)].$code; – kainaw Mar 13 '15 at 14:10