# Sequential Generation from an Index Value#

I needed to be able to generate a sequence of letters from a specific index value. Basically, I wanted to loop through a sequence of values and retrieve the corresponding string value. For example, 0 would be A; 4 would be E; 36 would be AK; etc.

    0 = A
1 = B
2 = C
3 = D
4 = E
5 = F
21 = V
22 = W
23 = X
24 = Y
25 = Z
26 = AA
27 = AB
28 = AC
36 = AK
37 = AL
38 = AM
39 = AN


I have created a small python script as a proof of concept that does the calculation using two different methods.

## Method 1#

Method 1, uses a recursive function to build up the correct sequence from the index value. Essentially the code is treating the index integer as a value that has the index into a large 2D array encoded within it. We use the modulus method to extract the index for the particular row. We then subtract the difference from the index to see if there are any more letters stored in the encoded index. Method 1 returns the value of the sequence in the correct order.

def get_name(index):
"""
Translate the index to a set of letters in sequential order.
"""

#termination criteria
if index < 0:
return ''

#Take the modulus of the current index and translate it into one of the
#characters stored in the letters string.
selected_index = index % lettersCount

return letters[selected_index] + get_name(math.floor((index - lettersCount)/lettersCount))


## Method 2#

Method 2 works in a similar manor, yet simpler, to Method 1. However it calculates the sequence in reverse order. So in order for the result to be useful, the sequence must be reversed.

def get_name2(index, values = []):
"""
Translate the index to a set of letters in sequential order, but reversed.
"""

if not values:
values = []

#termination criteria
if index < 0:
return values

#Take the modulus of the current index and translate it into one of the
#characters stored in the letters string.
selected_index = index % lettersCount
values.append(letters[selected_index])

return get_name2(math.floor((index - lettersCount)/lettersCount), values)


## Demonstration Script#

Here is a script to demonstrate the methods:

  1#!/usr/bin/encodednv python3
2#-*- coding:utf-8 -*-
3"""
4index = 0, value = A
5index = 1, value = B
6index = 2, value = C
7index = 3, value = D
8index = 4, value = E
9index = 5, value = F
10index = 6, value = G
11index = 7, value = H
12index = 8, value = I
13index = 9, value = J
14index = 10, value = K
15index = 11, value = L
16index = 12, value = M
17index = 13, value = N
18index = 14, value = O
19index = 15, value = P
20index = 16, value = Q
21index = 17, value = R
22index = 18, value = S
23index = 19, value = T
24index = 20, value = U
25index = 21, value = V
26index = 22, value = W
27index = 23, value = X
28index = 24, value = Y
29index = 25, value = Z
30index = 26, value = AA
31index = 27, value = AB
32index = 28, value = AC
33index = 29, value = AD
34index = 30, value = AE
35index = 31, value = AF
36index = 32, value = AG
37index = 33, value = AH
38index = 34, value = AI
39index = 35, value = AJ
40index = 36, value = AK
41index = 37, value = AL
42index = 38, value = AM
43index = 39, value = AN
44"""
45
46import math
47import sys
48
49letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
50# letters = 'ABCDEF'
51lettersCount = len(letters)
52
53def get_name(index):
54    """
55    Translate the index to a set of letters in sequential order.
56    """
57
58    #termination criteria
59    if index < 0:
60        return ''
61
62    #Take the modulus of the current index and translate it into one of the
63    #characters stored in the letters string.
64    selected_index = index % lettersCount
65
66    return letters[selected_index] + get_name(math.floor((index - lettersCount)/lettersCount))
67
68def test1():
69    print(letters)
70    print('index = 8:  ', get_name(8)[::-1])
71    print('index = 15: ', get_name(15)[::-1])
72    print('index = 17: ', get_name(17)[::-1])
73    print('index = 18: ', get_name(18)[::-1])
74    print('index = 75: ', get_name(75)[::-1])
75    print('index = 1000: ',get_name(1000)[::-1])
76    print()
77
78    for i in range(100):
79        print('index = {}, value = {}'.format(i, get_name(i)[::-1]))
80
81def get_name2(index, values = []):
82    """
83    Translate the index to a set of letters in sequential order, but reversed.
84    """
85
86    if not values:
87        values = []
88
89    #termination criteria
90    if index < 0:
91        return values
92
93    #Take the modulus of the current index and translate it into one of the
94    #characters stored in the letters string.
95    selected_index = index % lettersCount
96    values.append(letters[selected_index])
97
98    return get_name2(math.floor((index - lettersCount)/lettersCount), values)
99
100
101def test2():
102    print(letters)
103
104    for i in range(1000):
105        print('index = {}, value = {}'.format(i,
106                                              ''.join(reversed(get_name2(i)))))
107
108
109def main():
110    """
111    This runs the rest of the functions in this module
112    """
113
114    print('test1')
115    test1()
116
117    print('test2')
118    test2()
119
120    return 0 # success
121
122
123if __name__ == '__main__':
124    status = main()
125    sys.exit(status)