1
$\begingroup$

I need to create an id generation function that takes 4-digit number and returns a unique 3-letter identifier.

I already have a function that generates a 2-letter id from 3-digit number with some limitations (between 100 and 775), but I'm not sure how to change it to meet the new requirement.

if order_id < 775:
  alphabet = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  alpha_index = ''
  _i_1 = 0
  _i_2 = 0
  if order_id >=100 and order_id < 775:
    increment = 4*int((order_id-100)/100)
    _mod_100 = order_id%100
    if _mod_100 > 25 :
      _i_1 = (_mod_100/25)+increment
      _i_2 = _mod_100%25
      alpha_index += alphabet[int(_i_1-1)]
      alpha_index += alphabet[int(_i_2-1)]
    else:
      index = str(order_id)[1]+str(order_id)[2]
      index = int(index)
      if increment:
        alpha_index = alphabet[increment-1]
      alpha_index += alphabet[index] 
    alpha_order_id = alpha_index+str(order_id)[3:]
else:
  alpha_order_id = order_id

Any help is appreciated.

$\endgroup$
8
  • $\begingroup$ $26^3 = 17576 > 9999$. just multiply by $x$ take mod 17576? $\endgroup$
    – kelalaka
    Nov 30 '21 at 17:09
  • 1
    $\begingroup$ This is an elementary programming question, with no relevance to cryptography. $\endgroup$
    – fgrieu
    Nov 30 '21 at 17:33
  • $\begingroup$ @fgrieu No relevance at all? Nothing? Hashing - no? Uniqueness of response - no? Introduction to cryptographic hashes (the solution) - no? Universal hashing - no? This isn't how to grow the user base fgrieu. $\endgroup$
    – Paul Uszak
    Dec 1 '21 at 0:13
  • 1
    $\begingroup$ @fgrieu I'm not sure what's elementary about it and why it was marked as "off-topic". I don't have experience in cryptography and was hoping to get some help. I clearly explained the problem and showed attempt. Please advise on where to ask this question if this place is not the suitable place for it (DBA, SO...) $\endgroup$ Dec 1 '21 at 7:01
  • 1
    $\begingroup$ The question as asked is not about cryptography: the only mention of hash is in the title, not in the problem statement. This is about a change of numeration basis: from 4 digits in base 10 to 3 digits|characters in base 26. Base change is elementary math/computer science, not cryptography. Hint: for the reason in the first comment, any combination of 4 digits in base 10 can be expressed as 3 digits|characters in base 26. $\endgroup$
    – fgrieu
    Dec 1 '21 at 7:06

Browse other questions tagged or ask your own question.