While I was coding the ‘Twitter Short Handles Finder‘ I needed an efficient Alpha-Numeric Strings generator in Python. I coded this from scratch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
def lastCase (lst): for i in range(0, len(lst)): if ( lst[i] != '_' ): return False return True l = [''] * 4 #change size here if needed. I used 4 here l[0] = '0' index = 0 while ( not lastCase(l) ): if ( ord(l[index]) > ord('_') ): l[index] = '0' index += 1 while( l[index] == '_' ): l[index] = '0' index += 1 if (l[index] == ''): l[index] = '0' #print or process generated string print(''.join(l)) l[index] = chr(ord(l[index]) +1) if ( ord(l[index]) > ord('9') and ord(l[index]) < ord('A') ): l[index] = 'A' elif ( ord(l[index]) > ord('Z') and ord(l[index]) < ord('_') ): l[index] = '_' index = 0 print (''.join(l)) |
There is also another version on StackOverFlow > here
Leave a reply