human
What is a code to repeat last occurrence of each alphanumeric character to their position in character family times and the other steps required given the following:
Repeat last occurrence of each alphanumeric character to their position in character family times | Function to encode the given string.
Variable string to store the result.
Arrays to store the last occuring index of every character in the string.
Length of the string.
Iterate over the range.
If str [ i ] is between 0 and 9.
If str [ i ] is between a and z.
If str [ i ] is between A and Z.
Iterate over the range.
If str [ i ] is between a and z and i is the last occurence in str.
If str [ i ] is between A and Z and i is the last occurence in str.
If str [ i ] is between 0 and 9 and i is the last occurence in str.
Print the result.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to repeat last occurrence of each alphanumeric character to their position in character family times and the other steps required.
 def encodeString ( str ) :
	res = " "
	small = [ 0 for i in range ( 26 ) ]
	capital = [ 0 for i in range ( 26 ) ]
	num = [ 0 for i in range ( 10 ) ]
	n = len ( str )
	for i in range ( n ) :
		if ( str [ i ] >= '0' and str [ i ] <= '9' ) :
			num [ ord ( str [ i ] ) - 48 ] = i
		elif ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) :
			small [ ord ( str [ i ] ) - 97 ] = i
		elif ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) :
			capital [ ord ( str [ i ] ) - 65 ] = i
	for i in range ( n ) :
		if ( ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) and small [ ord ( str [ i ] ) - 97 ] == i ) :
			occ = ord ( str [ i ] ) - 96
			while ( occ > 0 ) :
				res += str [ i ]
				occ -= 1
		elif ( ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) and capital [ ord ( str [ i ] ) - 65 ] == i ) :
			occ = ord ( str [ i ] ) - 64
			while ( occ > 0 ) :
				res += str [ i ]
				occ -= 1
		elif ( ( str [ i ] >= '0' and str [ i ] <= '9' ) and num [ ord ( str [ i ] ) - 48 ] == i ) :
			occ = ord ( str [ i ] ) - 48
			while ( occ > 0 ) :
				res += str [ i ]
				occ -= 1
		else :
			res += str [ i ]
	print ( res )
if __name__ == ' _ _ main _ _ ' :
	str = " Ea2 , ▁ 0 , ▁ E "
	encodeString ( str )