# Resolved: Efficient method to find the index of 4-char string among all possible 4-char strings with given letters

## Question:

This:
A = b'abcdefghijklmnopqrstuvwxyz0123456789!'
n = len(A)

def f(s):
t = 0
for i, c in enumerate(s):
t += A.index(c) * n ** (3 - i)
return t

print(f(b'aaaa'))  # first possible 4-char string, should be 0
print(f(b'aaab'))  # 2nd possible 4-char string, should be 1
print(f(b'!!!!'))  # last possible 4-char string, should be 37^4 - 1
works to find the index of a 4-char string among all possible 4-char strings made with an alphabet A, but it does not seem efficient because of the many A.index() calls.
How to speed-efficiently find the index of a 4-char string among all possible 4-char strings made with an alphabet A?

I mean, the only way I can think of is using a stupid branchless formula to calculate the index as-is, but I have nothing else to offer you.
A = b'abcdefghijklmnopqrstuvwxyz0123456789!'
n = len(A)

def f(s,A):
t = 0
for i, c in enumerate(s):
t += A.index(c) * n ** (3 - i)
return t

def g(s,A):
t = 0
for i, c in enumerate(s):
t += (c - 97 + (c<97)*75 + (c<48)*25) * n ** (3 - i)
return t

print(f(b'aaaa',A),g(b'aaaa',A))  # first possible 4-char string, should be 0
print(f(b'aaab',A),g(b'aaab',A))  # 2nd possible 4-char string, should be 1
print(f(b'!!!!',A),g(b'!!!!',A))  # last possible 4-char string, should be 37^4 - 1

import timeit
ft = timeit.timeit(lambda: 'f(b"aop!")',number=1000000)
gt = timeit.timeit(lambda: 'g(b"aop!")',number=1000000)
print(f'f: {ft}, g: {gt}, factor: {ft/gt}')
Output:
0 0
1 1
1874160 1874160
f: 0.12712620000820607, g: 0.06313459994271398, factor: 2.0135741752312635
Edit: I tried one other thing:
def h(s):
arr = np.array(list(s))
return sum( (arr - 97 + (arr<97)*75 + (arr<48)*25) * n ** (3 - np.array([0,1,2,3])) )
Output:
0 0 0
1 1 1
1874160 1874160 1874160
f: 0.10835059999953955, g: 0.07444929995108396, h: 0.06341919989790767,
factor f/g: 1.4553608975602195, factor f/h: 1.7084826073801391