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的更多相关文章

  1. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  2. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  3. LeetCode Reverse String

    原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...

  4. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  5. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  6. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  7. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  8. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  9. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

随机推荐

  1. sed 小结

    语法格式1: sed option command file 注意区分 option command, option 以-开始 command 以单?引号包围 ———— 这完全是误会, 我测试好多后发 ...

  2. shell随笔

    一, case的详细用法:   参考文章(http://blog.csdn.net/dreamtdp/article/details/8048720) 语句实例:由用户从键盘输入一个字符,并判断该字符 ...

  3. html页面中iframe导致JavaScript失效

    <body onload=“reset()”> <div id="part1"> some thing here .... <div> < ...

  4. J2SE 8的泛型

    泛型的简单使用 1. 泛型一般用E表示集合中元素;k和v表示Map中的key和value;R表示return值;T/U/S表示任意类型 //(1) 简单单个元素的泛型 Box<String> ...

  5. Yii-CHtmlPurifier- 净化器的使用(yii过滤不良代码)

    1. 在控制器中使用: public function actionCreate() { $model=new News; $purifier = new CHtmlPurifier(); $puri ...

  6. 使用java代码执行linux命令

    前提: java代码是在windows下面写的,要打包放到linux下面运行,并且执行某个脚本. java代码: try { // 起作用的代码其实就下面这一行, 参数是linux中要执行的代码 Ru ...

  7. pandas datafram重命名列名称

    方法一: 直接给column赋值 df.columns=["a", "b"], 所有的column全部重命名 example: import pandas as ...

  8. Jsonlib 属性过滤器

    /** * @title JSON转换属性过滤器 * @description 用于JSON lib的JSON转换 * @author maohuidong * @date 2017-04-06 */ ...

  9. js 日期证有效性验的通用方法

    开发的理念是“为复用而开发,为使用而组装”,代码的复用度将是项目和产品的一个重要的技术指标. var DateTools={ isDate:function(str) { var result = s ...

  10. pyplot文本显示

    pyplot文本显示 pyplot中文字符显示 pyplot默认不支持中文字符,因为默认字体是sans-serif,英文字体不能显示中文 方法1,修改需要输出中文字符的地方 在有中文输出的地方,添加属 ...