Score:1

Hashing function: Generate unique 3 letters Identifier

cn flag

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.

kelalaka avatar
in flag
$26^3 = 17576 > 9999$. just multiply by $x$ take mod 17576?
fgrieu avatar
ng flag
This is an elementary programming question, with no relevance to cryptography.
Paul Uszak avatar
cn flag
@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.
Majed Badawi avatar
cn flag
@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...)
fgrieu avatar
ng flag
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.
Majed Badawi avatar
cn flag
@fgrieu how should the third letter be generated in the above solution to guarantee a unique output every time from a 4-digit number? I'm looking for a way to change the above solution to support this. Any other hints or algorithms to research are appreciated.
fgrieu avatar
ng flag
I'll give an example and let you write the code (that I suggest you do from scratch). Say you want to convert 9876 to 3 digits|characters in base 26. We compute 9796%26=22, coded by character `W`. There remains to encode 9796//26=379. We compute 379%26=15, coded by character `P`. There remains to encode 379//26=14 (we know that outcome is less than 26 for the reason kelalaka gave in comment), coded by character `O`. Often, in base change, we write the result with the first obtained character on the right: `OPW`. Again, that's base change or encoding.
Paul Uszak avatar
cn flag
Rather than your code, how about a cryptographic hash function, e.g. SHA-x/ MD5? And so `H(input) >> n` where n is an appropriate number of shift rights to get 3 characters. Since you're going 4 to 3, look up Pigeon Hole principle, and what a surjection is. And the difference between a simple hash function like Java's hashCode and a cryptographic one.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.