human
Find shortest unique prefix for every word in a given list | Set 2 ( Using Sorting ) | Python3 program to prshortest unique prefixes for every word ..
Create an array to store the results.
Sort the array of strings.
Compare the first with its only right neighbor.
Store the unique prefix of a [ 1 ] from its left neighbor.
Compute common prefix of a [ i ] unique from its right neighbor.
Compare the new prefix with previous prefix.
Store the prefix of a [ i + 1 ] unique from its left neighbour.
Compute the unique prefix for the last in sorted array.
Driver Code"
How can the above be solved in Python?
 What is a code to find shortest unique prefix for every word in a given list and the other steps required?
assistant
#Here is code to find shortest unique prefix for every word in a given list and the other steps required.
 def uniquePrefix ( a ) :
	size = len ( a )
	res = [ 0 ] * ( size )
	a = sorted ( a )
	j = 0
	while ( j < min ( len ( a [ 0 ] ) - 1 , len ( a [ 1 ] ) - 1 ) ) :
		if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) :
			j += 1
		else :
			break
	ind = 0
	res [ ind ] = a [ 0 ] [ 0 : j + 1 ]
	ind += 1
	temp_prefix = a [ 1 ] [ 0 : j + 1 ]
	for i in range ( 1 , size - 1 ) :
		j = 0
		while ( j < min ( len ( a [ i ] ) - 1 , len ( a [ i + 1 ] ) - 1 ) ) :
			if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) :
				j += 1
			else :
				break
		new_prefix = a [ i ] [ 0 : j + 1 ]
		if ( len ( temp_prefix ) > len ( new_prefix ) ) :
			res [ ind ] = temp_prefix
			ind += 1
		else :
			res [ ind ] = new_prefix
			ind += 1
		temp_prefix = a [ i + 1 ] [ 0 : j + 1 ]
	j = 0
	sec_last = a [ size - 2 ]
	last = a [ size - 1 ]
	while ( j < min ( len ( sec_last ) - 1 , len ( last ) - 1 ) ) :
		if ( sec_last [ j ] == last [ j ] ) :
			j += 1
		else :
			break
	res [ ind ] = last [ 0 : j + 1 ]
	return res
if __name__ == ' _ _ main _ _ ' :
	input = [ " zebra " , " dog " , " duck " , " dove " ]
	output = uniquePrefix ( input )
	print ( " The ▁ shortest ▁ unique ▁ prefixes ▁ " + " in ▁ sorted ▁ order ▁ are ▁ : ▁ " )
	for i in output :
		print ( i , end = " ▁ " )