fork download
  1. total = 10
  2.  
  3. print("RANDOM:")
  4. import random
  5.  
  6. for n in range(total):
  7. rest = random.randint(0,10**10-1)
  8. code = "{n:06}{rest:010}".format(n=n, rest=rest)
  9. formatted = code[0:4] + "-" + code[4:8] + "-" + code[8:12] + "-" + code[12:]
  10. print(formatted)
  11.  
  12.  
  13. print("TESTABLE:")
  14.  
  15. import hashlib
  16.  
  17. SALT = "some_salt"
  18.  
  19. def generate(n):
  20. _bytes = (str(n) + SALT).encode("ascii")
  21. _hash = hashlib.md5(_bytes).hexdigest()
  22. rest = int(_hash, base=16) % (10**10)
  23. code = "{n:06}{rest:010}".format(n=n, rest=rest)
  24. formatted = code[0:4] + "-" + code[4:8] + "-" + code[8:12] + "-" + code[12:]
  25. return formatted
  26.  
  27. def test(code):
  28. raw_code = code.replace("-","")
  29. n = int(raw_code[0:6])
  30. return code == generate(n)
  31.  
  32. for n in range(total):
  33. code = generate(n)
  34. print(code, test(code))
Success #stdin #stdout 0.02s 37616KB
stdin
Standard input is empty
stdout
RANDOM:
0000-0028-8395-2603
0000-0172-1205-9078
0000-0234-3972-4586
0000-0367-8974-9396
0000-0431-5568-7851
0000-0575-6697-8019
0000-0621-2770-0374
0000-0792-7325-4875
0000-0810-7935-0283
0000-0942-0678-5986
TESTABLE:
0000-0087-3493-5614 True
0000-0107-2475-4064 True
0000-0286-6524-4519 True
0000-0363-3440-9439 True
0000-0490-5080-7982 True
0000-0572-4359-6356 True
0000-0660-6697-7169 True
0000-0742-3195-8617 True
0000-0858-1846-3386 True
0000-0942-4511-1504 True