human
Write me a code to number of flips to make binary string alternate and the other steps required given the following:
Number of flips to make binary string alternate | Set 1 | Utility method to flip a character.
Utility method to get minimum flips when alternate string starts with expected char.
if current character is not expected , increase flip count.
flip expected character each time.
method return minimum flip to make binary string alternate.
return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1.
Driver code to test above method"
How can the above be solved in Python?
assistant
#Here is code to number of flips to make binary string alternate and the other steps required.
 def flip ( ch ) :
	return '1' if ( ch == '0' ) else '0'
def getFlipWithStartingCharcter ( str , expected ) :
	flipCount = 0
	for i in range ( len ( str ) ) :
		if ( str [ i ] != expected ) :
			flipCount += 1
		expected = flip ( expected )
	return flipCount
def minFlipToMakeStringAlternate ( str ) :
	return min ( getFlipWithStartingCharcter ( str , '0' ) , getFlipWithStartingCharcter ( str , '1' ) )
if __name__ == " _ _ main _ _ " :
	str = "0001010111"
	print ( minFlipToMakeStringAlternate ( str ) )