text: Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base case ; For cases n > 2 ; Driver code
code: def CountWays ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return 1 + 1 NEW_LINE DEDENT return CountWays ( n - 1 ) + CountWays ( n - 3 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( CountWays ( n ) ) NEW_LINE DEDENT