id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

mmiller@cacegypt.org recursion > python_recursion_eg
prev  |  next  |  chance

Python recursion introduction

Recursive code begins with a base case if-statement, which checks for one or more cases which are so simple the answer can be returned immediately.  This is followed by a recursive case which calls the same method with slightly smaller inputs, and then fixes up what it returns. "Smaller" inputs means at least one step towards the base case.  Try writing a simple recursive method that counts the number of "A" in the given string (str). 
First, return the simplest answer.
Next, deal with the very front of the string and count the "A" there, if it exists.
Finally, make a recursive call to your function to deal with the rest of the string.  Be sure you only pass the part after the first character!

python_recursion_eg('A') → 1
python_recursion_eg('AA') → 2
python_recursion_eg('Alphabet') → 1

...Save, Compile, Run (ctrl-enter)

def python_recursion_eg( str ):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 300 Post-solution available

Copyright Nick Parlante 2017 - privacy