human
Count of occurrences of a "1(0 + ) 1" pattern in a string | Returns count of occurrences of "1(0 + ) 1".
count = 0 Initialize result.
Check if encountered '1' forms a valid pattern as specified.
if 1 encountered for first time set oneSeen to 1.
Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to count of occurrences of a "1(0 + ) 1" pattern in a string and the other steps required.
 def countPattern ( s ) :
	length = len ( s )
	oneSeen = False
	for i in range ( length ) :
		if ( s [ i ] == '1' and oneSeen ) :
			if ( s [ i - 1 ] == '0' ) :
				count += 1
		if ( s [ i ] == '1' and oneSeen == 0 ) :
			oneSeen = True
		if ( s [ i ] != '0' and s [ i ] != '1' ) :
			oneSeen = False
	return count
s = "100001abc101"
print countPattern ( s )