Recursively Recurse With Recursion


Exercises for Chapter #9 of A Common-Sense Guide To Data Structures and Algorithms

Get The Book!

Chapter Exercises


Exercise #1

You can technically use recursion to replace any loop. Write a function that prints to the terminal "HELLO" 10 times - but don’t use a loop! Use a recursive function instead.

Exercise #2

Write a recursive function that prints out all even numbers from 2 until 100.

Exercise #3

Fibonacci numbers are numbers that follow this pattern: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... that is, each number is the sum of the two immediate numbers that precede it. Write a recursive function that prints out the list of fibonacci numbers up to 987.

Exercise #4

Write a recursive function that accepts an array of numbers and returns the sum.

Exercise #5

Write a recursive function that reverses a string.

Exercise #6

Write a recursive function that reverses a string.

Exercise #7

Write a recursive function that accepts two numbers (a numerator and denominator), and returns the remainder if you divide the numerator by the denominator. The catch: Do not use the modulo operator!

Exercise #8

Write a recursive binary search function.

Exercise #9

Write a recursive function that accepts two numbers and calculates one by the power of the other. For example, if the numbers were 2 and 5, it would calculate 25. Do not use any built-in power operations provided by your computer language.

Exercise #10

Write a recursive function that returns all anagrams of a string (even if the anagrams aren’t words themselves). For example, the anagrams of cat are cat, cta, act, atc, tac, tca.

Let's Get In Touch!


Join the forum on The Pragmatic Programmers website to join in on the conversation about this book, or email Jay Wengrow, author of the book and this website directly.