zoukankan      html  css  js  c++  java
  • Reverse string using recursion

    On-Site Question 3 - SOLUTION

    Question

    Given a string, write a function that uses recursion to reverse it.

    Requirements

    You MUST use pen and paper or a whiteboard to answer this, no coding allowed!

     

     

    SOLUTION

    Hopefully you remember this problem, you've already seen it! The solution is:



    def reverse(s):
        
        # Base Case
        if len(s) <= 1:
            return s
    
        # Recursion
        return reverse(s[1:]) + s[0]


     

    Notes

    Remember when recursion questions arise, think about the base case and the recursive case. Review the recusion section of the course for review for this problem.

     

    Good Job!

  • 相关阅读:
    Python学习4
    Python学习3
    Python学习2
    表空间
    sqlplus常用设置
    HashMap和LinkedHashMap
    堆栈源码
    观察者模式
    策略模式
    java线性表
  • 原文地址:https://www.cnblogs.com/prmlab/p/6961021.html
Copyright © 2011-2022 走看看