首页 > 其他 > 详细

Reverse string using recursion

时间:2017-06-08 09:54:06      阅读:340      评论:0      收藏:0      [点我收藏+]

 

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!

Reverse string using recursion

原文:http://www.cnblogs.com/prmlab/p/6961021.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!