A Binary String Game | Function to return the result of the game ; Length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1 ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Given string S ; Function call
def gameMax ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE list = [ ] NEW_LINE one = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( one != 0 ) : NEW_LINE INDENT list . append ( one ) NEW_LINE DEDENT one = 0 NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( one != 0 ) : NEW_LINE INDENT list . append ( one ) NEW_LINE DEDENT list . sort ( reverse = True ) NEW_LINE score_1 = 0 NEW_LINE score_2 = 0 NEW_LINE for i in range ( len ( list ) ) : NEW_LINE INDENT if ( list [ i ] % 2 == 1 ) : NEW_LINE INDENT score_1 += list [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT score_2 += list [ i ] NEW_LINE DEDENT DEDENT if ( score_1 == score_2 ) : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT if ( score_1 > score_2 ) : NEW_LINE INDENT return " Player ▁ 1" NEW_LINE DEDENT else : NEW_LINE INDENT return " Player ▁ 2" NEW_LINE DEDENT DEDENT S = "11111101" NEW_LINE print ( gameMax ( S ) ) NEW_LINE