Alphabet¶
Utility functions to convert integer into a base-26 “number”, and vis versa.
- benker.alphabet.alphabet_to_int(letters, alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ')¶
Convert a base-26 “number” using uppercase ASCII letters into an integer.
>>> from benker.alphabet import alphabet_to_int >>> alphabet_to_int("A") 1 >>> alphabet_to_int("B") 2 >>> alphabet_to_int("AA") 27 >>> alphabet_to_int("AB") 28 >>> alphabet_to_int("ZZZ") 18278 >>> alphabet_to_int("") 0 >>> alphabet_to_int("AA@") Traceback (most recent call last): ... ValueError: AA@
- Parameters
letters – string representing a “number” in the base-26.
alphabet – alphabet to use for the conversion.
- Returns
Integer value of the “number”.
- benker.alphabet.int_to_alphabet(value, alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ')¶
Convert a non-nul integer into a base-26 “number” using uppercase ASCII letters.
Usage:
>>> from benker.alphabet import int_to_alphabet >>> int_to_alphabet(1) 'A' >>> int_to_alphabet(2) 'B' >>> int_to_alphabet(26) 'Z' >>> int_to_alphabet(27) 'AA' >>> int_to_alphabet(28) 'AB' >>> int_to_alphabet(52) 'AZ' >>> int_to_alphabet(53) 'BA' >>> int_to_alphabet(18278) 'ZZZ' >>> int_to_alphabet(-5) Traceback (most recent call last): ... ValueError: -5
- Parameters
value (int) – value to convert
alphabet – alphabet to use for the conversion.
- Returns
string representing this “number” in the base-26.