Stepping Numbers | Prints all stepping numbers reachable from num and in range [ n , m ] ; If Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater than m , then return ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Method displays all the stepping numbers in range [ n , m ] ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
def dfs ( n , m , stepNum ) : NEW_LINE INDENT if ( stepNum <= m and stepNum >= n ) : NEW_LINE INDENT print ( stepNum , end = " ▁ " ) NEW_LINE DEDENT if ( stepNum == 0 or stepNum > m ) : NEW_LINE INDENT return NEW_LINE DEDENT lastDigit = stepNum % 10 NEW_LINE stepNumA = stepNum * 10 + ( lastDigit - 1 ) NEW_LINE stepNumB = stepNum * 10 + ( lastDigit + 1 ) NEW_LINE if ( lastDigit == 0 ) : NEW_LINE INDENT dfs ( n , m , stepNumB ) NEW_LINE DEDENT elif ( lastDigit == 9 ) : NEW_LINE INDENT dfs ( n , m , stepNumA ) NEW_LINE DEDENT else : NEW_LINE INDENT dfs ( n , m , stepNumA ) NEW_LINE dfs ( n , m , stepNumB ) NEW_LINE DEDENT DEDENT def displaySteppingNumbers ( n , m ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT dfs ( n , m , i ) NEW_LINE DEDENT DEDENT n , m = 0 , 21 NEW_LINE displaySteppingNumbers ( n , m ) NEW_LINE