human
Give me a code to rearrange string such that no pair of adjacent characters are of the same type and the other steps required given the following:
Rearrange string such that no pair of adjacent characters are of the same type | Function to rearrange given alphanumeric such that no two adjacent characters are of the same type.
Stores alphabets and digits.
Store the alphabets and digits separately in the strings.
Stores the count of alphabets and digits.
If respective counts differ by 1.
Desired arrangement not possible.
Stores the indexes.
Check if first character should be alphabet or digit.
Place alphabets and digits alternatively.
If current character needs to be alphabet.
If current character needs to be a digit.
Flip flag for alternate arrangement.
Return resultant string.
Driver Code.
Given String.
Function call"
How can the above be solved in Python?
assistant
#Here is code to rearrange string such that no pair of adjacent characters are of the same type and the other steps required.
 def rearrange ( s ) :
	s1 = [ ]
	s2 = [ ]
	for x in s :
		if x . isalpha ( ) :
			s1 . append ( x )
		else :
			s2 . append ( x )
	n = len ( s1 )
	m = len ( s2 )
	if ( abs ( n - m ) > 1 ) :
		return " - 1"
	i = 0
	j = 0
	k = 0
	flag = 0
	if ( n >= m ) :
		flag = 1
	else :
		flag = 0
	while ( i < n and j < m ) :
		if ( flag ) :
			s [ k ] = s1 [ i ]
			k += 1
			i += 1
		else :
			s [ k ] = s2 [ j ]
			k += 1
			j += 1
		flag = not flag
	return " " . join ( s )
if __name__ == ' _ _ main _ _ ' :
	str = " geeks2020"
	str1 = [ i for i in str ]
	print ( rearrange ( str1 ) )