fork download
  1. import base64
  2. import math
  3.  
  4.  
  5. def rank(comb):
  6. return sum([
  7. math.comb(ci, i + 1)
  8. for i, ci in enumerate(comb)])
  9.  
  10.  
  11. def unrank(N, k):
  12. comb = []
  13. n = N
  14. for r in range(k, 0, -1):
  15. ci = r - 1
  16. while math.comb(ci + 1, r) <= n:
  17. ci += 1
  18. comb.append(ci)
  19. n -= math.comb(ci, r)
  20. return list(reversed(comb))
  21.  
  22.  
  23. def int_to_base64(integer: int) -> str:
  24. """Converts an integer to a base64 string."""
  25.  
  26. num_bytes = (integer.bit_length() + 7) // 8
  27. integer_bytes = integer.to_bytes(num_bytes, 'big')
  28. base64_bytes = base64.b64encode(integer_bytes)
  29. base64_string = base64_bytes.decode('utf-8')
  30. return base64_string
  31.  
  32. # Example usage
  33. number = 1234567890
  34. base64_result = int_to_base64(number)
  35. print(f"The base64 representation of {number} is: {base64_result}")
  36.  
  37. # To reverse the process
  38. def base64_to_int(base64_string: str) -> int:
  39. """Converts a base64 string back to an integer."""
  40.  
  41. base64_bytes = base64_string.encode('utf-8')
  42. integer_bytes = base64.b64decode(base64_bytes)
  43. integer = int.from_bytes(integer_bytes, 'big')
  44. return integer
  45.  
  46.  
  47. a = list(sorted([42, 182, 31, 410, 171, 283, 52, 149]))
  48.  
  49. r = rank(a)
  50. print((r, unrank(r, len(a))))
  51.  
  52. i = 45544194540887359955917413870427129419278019524063864840334
  53.  
  54. b = int_to_base64(i)
  55. print(b)
  56. print(base64_to_int(b))
  57. print(i)
Success #stdin #stdout 0.15s 15708KB
stdin
Standard input is empty
stdout
The base64 representation of 1234567890 is: SZYC0g==
(18515612689511717, [31, 42, 52, 149, 171, 182, 283, 410])
B0Fvk+sgBZYXKrvJvoCMNxr6DFFYGrRgjg==
45544194540887359955917413870427129419278019524063864840334
45544194540887359955917413870427129419278019524063864840334